C++第四次上机作业
2016-04-26 21:25
169 查看
项目4(输出完数) #include <iostream> #include<Cmath> using namespace std; int main( ) { int i,k,j; cout<<"2-1000之间的完数有:"; for(i=2;i<=1000;i++) { k=1; for(j=2;j<sqrt(i);j++) if(i%j==0) k+=(j+i/j); if(i==k) cout<<i<<" "; } cout<<endl; return 0; }
【项目7:穷举法解决组合问题】 (1)百钱百鸡问题: #include <iostream> using namespace std; int main() { int x, y, z, count=0; cout<<"一共有下面几种组合:"<<endl; for(x=0;x<=20;x++) for(y=0;y<=33;y++) for(z=0;z<=100;z++) if(5*x+3*y+z/3.0==100&&x+y+z==100) { ++count; cout<<count<<"、"<<"鸡翁"<<x<<"只,"<<"鸡母"<<y<<"只,"<<"鸡雏"<<z<<"只。"<<endl; } return 0; }
项目3:乘法口诀表 #include <iostream> using namespace std; int main () { int i, j; for (i=1;i<=9;i++) { for ( j =1 ;j<=9;j++) { cout <<"\t"<<i<<"×"<<j<<"="<<i*j; if (j==i) {cout <<endl ; break;} } } return 0; }