水仙花数参考程序:
#include<iostream>
using namespace std;
int main()
{
int ge,shi,bai,i;
for(i=100;i<=999;i++)
{
bai=i/100;
shi=(i/10)%10;
ge=i%10;
if(bai*bai*bai+shi*shi*shi+ge*ge*ge==i)
cout<<i<<" ";
}
return 0;
}
小于n的最大素数(纯暴力):
#include<iostream>
using namespace std;
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=n-1;i>=2;i--)
{
for(j=2;j<=i-1;j++)
if(i%j==0) break;
if(j==i) {printf("%d",i);break;}
}
}
优化:
#include<iostream>
using namespace std;
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=n-1;i>=2;i--)
{
for(j=2;j*j<=i;j++)
if(i%j==0) break;
if(j*j>i) {printf("%d",i);break;}
}
}
百钱买百鸡(三重循环):
#include<iostream>
#include<iomanip>//设置域宽需要引入头文件 <iomanip>
using namespace std;
int main()
{
int gongji,muji,xiaoji;//设置int型变量gongji,muji,xiaoji
cout<<"公鸡母鸡小鸡的数量分别为:"<<endl;
for(gongji=1;gongji<=33;gongji++) //建立公鸡的循环枚举
for(muji=1;muji<=50;muji++) //建立母鸡的循环枚举
for(xiaoji=1;xiaoji<=100;xiaoji++)//建立小鸡的循环枚举
if((gongji+muji+xiaoji==100)&&(gongji*3+muji*2+xiaoji/3.0==100))
cout<<setw(5)<<gongji<<setw(5)<<muji<<setw(5)<<xiaoji<<endl;
//设定百钱买百鸡的条件,符合的输出出来
return 0;
}
百钱买百鸡(二重循环):
#include<iostream>
#include<iomanip>//设置域宽需要引入头文件 <iomanip>
using namespace std;
int main()
{
int gongji,muji,xiaoji;//设置int型变量gongji,muji,xiaoji
cout<<"公鸡母鸡小鸡的数量分别为:"<<endl;
for(gongji=1;gongji<=33;gongji++) //建立公鸡的循环枚举
for(muji=1;muji<=50;muji++) //建立母鸡的循环枚举
{
xiaoji=100-gongji-muji; //小鸡的数量用表达式赋值
if(gongji*3+muji*2+xiaoji/3.0==100)
cout<<setw(5)<<gongji<<setw(5)<<muji<<setw(5)<<xiaoji<<endl;
}
return 0;
}
标签:std,main,int,namespace,语法,枚举,using,GESP4,include
From: https://blog.csdn.net/weixin_60445850/article/details/143652718