#include <iostream> #include <time.h> #include <math.h> #include <set> #include <list> using namespace std; int main() { double start = clock(),end(0); int count = 0; for(int a = 0; a<=1; a++)//200 { for(int b = 0; b<=2; b++)//100 { for(int c = 0; c<=4 ;c++)//50 { for(int d = 0; d<=10 ;d++)//20 { for(int e = 0; e<=20 ;e++)//10 { for(int f = 0; f<=40 ;f++)//5 { for(int g = 0; g<=100 ;g++)//2 { for(int h = 0; h<=200 ;h++)//1 { if(h*1+g*2+f*5+e*10+d*20+c*50+b*100+a*200 == 200) { count++; cout<<a<<","<<b<<","<<c<<","<<d<<","<<e<<","<<f<<","<<g<<","<<h<<endl;}}}}}}}}} cout << endl << "answer is "<< count <<endl; end = clock(); double result = (end - start)/1000; cout.setf(ios::fixed); cout <<endl<< "3.00GHz's run time = " << result << "s."<<endl; return 0; } 144s #include <iostream> #include <time.h> #include <math.h> #include <set> #include <list> using namespace std; int main() { double start = clock(),end(0); int times[201] = {0}; for(int n =-1; n <= 200;) { if(n<21) ++n; if(n==21) n+=9; int cnt = 0; for(int a = 0; a<=200; a++) { for(int b = 0; b<=200; b++) { for(int c = 0; c<=200; c++) { if(n == a + 2*b + 5*c) { cnt++; } } } } times[n] = cnt; //cout << "n = "<< n << " , cnt = " << cnt<<endl; if(n>21) n+=10; } int sum = 0; for(int x =0,y=20; x!=210, y!=-1; x+=10,y--) { //cout << "times["<<x<<"] = "<<times[x]<< " * times["<<y<<"] = "<<times[y]<<endl; sum += times[x] * times[y]; } for(int x =0,y=10; x!=110, y!=-1; x+=10,y--) { //cout << "times["<<x<<"] = "<<times[x]<< " * times["<<y<<"] = "<<times[y]<<endl; sum += times[x] * times[y]; } cout << "sum = " << sum+2 << endl; end = clock(); double result = (end - start)/1000; cout.setf(ios::fixed); cout <<endl<< "3.00GHz's run time = " << result << "s."<<endl; return 0; } 1s int count=0; int a, b, c, d, e, f, g; int m =200; for( a=m ; a>=0 ; a -= 200 ) for( b=a ; b>=0 ; b -= 100 ) for( c=b ; c>=0 ; c -= 50 ) for( d=c ; d>=0 ; d -= 20 ) for( e=d ; e>=0 ; e -= 10 ) for( f=e ; f>=0 ; f -= 5 ) for( g=f ; g>=0 ; g -= 2 ) count++; cout << count; 0s