标签:优化 sum 优秀 main ble core namespace color ret
优秀的算法:循环次数少,占用内存小
例如,判断n是否为素数,需要循环n-2次[2,n)。其实只需要[2,n/2)就可以。
计算三个数的平均数:double 8个字节,float 4个字节
#include <iostream> using namespace std; int main() { double score1,score2,score3,sum=0; //占用4×8=32字节 cin >> score1; cin >> score2; cin >> score3; sum = score1 + score2 + score3; cout << sum / 3 << endl; return 0; }
优化后
#include <iostream> using namespace std; int main() { float score,sum=0; //占用2×4=8字节 cin >> score; sum += score; cin >> score; sum += score; cin >> score; sum += score; cout << sum / 3 << endl; return 0; }
标签:优化 sum 优秀 main ble core namespace color ret
原文地址:https://www.cnblogs.com/xixixing/p/10061356.html