题目1:用for语句
#include <iostream>
using namespace std;
int main()
{
int i,a,b,c;
for (i=100;i<=999;i++)
{
a=i/100;
b=i/10-a*10;
c=i-100*a-10*c;
if (i==a*a*a+b*b*b+c*c*c)
cout<<i<<endl;
}
return 0;
}
用while语句:
#include <iostream>
using namespace std;
int main()
{
int i=100,a,b,c;
while (i<=999)
{
a=i/100;
b=i/10-a*10;
c=i-100*a-10*c;
i++;
if (i==a*a*a+b*b*b+c*c*c)
cout<<i<<endl;
}
return 0;
}
题目二:
#include <iostream>
using namespace std;
int main()
{
int i=100,a,b,c;
while (i<=999)
{
a=i/100;
b=i/10-a*10;
c=i-100*a-10*c;
i++;
if (i==a*a*a+b*b*b+c*c*c)
cout<<i<<endl;
}
return 0;
}
第二题:
using namespace std;
int main()
{
int x,y,z;//鸡翁数为x,鸡母数为y,鸡雏数为z
for(x=0;x<=20;x++)
for(y=0;y<=33;y++)
for(z=0;z<=99;z+=3)
{
if(5*x+3*y+z/3==100&&x+y+z==100)
cout<<"鸡翁数为: " <<x<<"鸡母数为: "<<y<<"鸡雏数为: "<<z<<endl;
}
return 0;
}
第三题:
#include <iostream>
using namespace std;
int main()
{
int num,i=0,add=0;//i作为计数
cout << "请输入一个数字:" << endl;
cin>>num;
while (num!=0)
{
add=add+num%10;//求模得个位上数值且相加
num=num/10;//降低一位
i++;
}
cout << "该数由"<<i<<"位组成,各个数位上数之和为:"<<add<<endl;
return 0;
}
求自然对数e
#include <iostream>
using namespace std;
int main()
{
int i=0,value,fac;
while(fac>=10e-6)
{ i++;
fac/=i;
value+=fac;
}
return 0;
}
求圆周率pi
#include <iostream>
using namespace std;
int main()
{
int i=1,value=1,fac=1,j=1;double pi;
while(fac>=1e-6)
{ i+=2;
fac/=i;
value+=fac*(-1)^(j);
j++;
pi=4*value;
}
return 0;
}
第七题
#include <iostream>
using namespace std;
int main ()
{
int i;
cout<<"请输入一个数:"<<endl;
cin>>i;
if(i<10)
cout<<" is less than 10"<<endl;
if(10<=i<100)
cout<<" is10 to 100"<<i<<endl;
if(100<=i<1000)
cout<<" is100 to 1000"<<i<<endl;
if(i>1000)
cout<<" is more than 1000"<<i<<endl;
return 0;
}
第九题
#include <iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while(sum<=1000)
{
sum+=i^2;
i++;
}
cout<<"最大值i="<<--i<<endl;
return 0;
}
9*9乘法表:
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=9;i++)
{for(j=1;j<=i;j++)
cout<<i<<"*"<<j<<"="<<i*j;
cout<<endl;}
return 0;
}
第十题
#include <iostream)
using namespace std;
int main()
{
double i,j,k;int n;
for(i=0,j=1;j<=30;++j)
i+=10;
for(k=0,n=0;n<=29;++n)
k+=2^n;
cout<<"富人所得的钱:"<<i<<endl;
cout<<"穷人所得的钱:"<<k<<endl;
return 0;
}
猴子吃了多少苹果:
#include <iostream>
using namespace std;
int main()
{
int i,sum=1;
for(i=1;i<10;i++)
sum+=(sum+1)*2;
cout<<"sum="<<sum<<endl;
return 0;
}
水塔的高度:
#include <iostream>
using namespace std;
int main()
{
double x,y;
cout<<"请输入坐标(x,y)"<<endl;
cin>>x>>y;
if(((x+2)+(y+2)<=1)
||((x+2)+(y-2)<=1)
||((x-2)+(y+2)<=1)
||((x-2)+(y-2)<=1))
cout<<"10m"<<endl;
else
cout<<"0m"<<endl;
return 0;
}
求s=1!+2!+3!+……+n!
#include <iostream>
using namespace std;
int main()
{
int i,j, sum=0;
for(cin>>i;i>0;i--)
{for(j=i;j>0;j--)
{j*=1;
sum+=j;
}
}
cout<<"sum="<<sum<<endl;
return 0;
}