用*//*把每一小段分隔开,需要请自取
/*
//求出100到999以内的所有"水仙花数"
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t,fi,se,th;
for(t=100;t<=999;t++)
{
if(pow((t/100),3)+pow(((t/10)%10),3)+pow((t%10),3)==t)
cout<<t<<"\t";
}
}
*//*
//预设密码6位,三次输入机会
#include<iostream>
using namespace std;
int main()
{
cout<<"请输入六位数密码"<<endl;
const int code=142857;
int icode,fault=0;
for(int i=1;i<=3;i++)
{
cin>>icode;
if(icode==code)
{
fault=fault-3;
cout<<"欢迎使用财会报表程序"<<endl;
break;
}
else
{
cout<<"密码错误!"<<endl;
fault++;
}
}
if(fault>0)
cout<<"拒绝使用财会报表软件";
}
*//*
//把键盘连续读取的多个字符串连接成一个字符串
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string S,s,s1=" ";
while(cin>>s)
{
S=S+s+s1;
}
cout<<S<<endl;
}
*//*
//读取保存在idata.txt中的若干个整数,计算它们的和
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifile("idata.txt");
int sum=0,num;
while(ifile>>num)
sum+=num;
cout<<sum;
}
*//*
//计算从键盘输入的若干个整数的和
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
while(cin>>n)
{
sum+=n;
}
cout<<"The sum is : "<<sum<<endl;
return 0;
}
*//*
//浮点数不能==
#include<iostream>
using namespace std;
int main()
{
float a=0.1,b=0.0;
for (int i=0;i<10;i++)
b=b+0.01;
cout<<b<<endl;
if(a==b)
cout<<"相等"<<endl;
else
cout<<"不相等"<<endl;
}
*//*
//计算1-10**9内两个数之间不含数字3,7的数字数量
#include<iostream>
using namespace std;
int main()
{
int i,amount=0,p,x,fault=0;
int first_num,last_num;
cout<<"请输入起始数字和终止数字"<<endl;
cin>>first_num>>last_num;
for(i=first_num;i<=last_num;i++)
{
x=i;
for(int j=1;j<=9;j++)
{
p=x%10;
if(p==3) fault++;
else if(p==7) fault++;
else x=x/10;
}
if(fault==0)amount++;
fault=0;
}
cout<<amount<<endl;
}
*//*
//找出100到500之间哪些数各位数字之和为5
#include <iostream>
using namespace std;
int main()
{
int tested_num,k;
cin>>k;
int bai,shi,ge,sum;
for(tested_num=100;tested_num<=500;tested_num++)
{
bai=(tested_num-tested_num%100)/100;
shi=(tested_num-bai*100-(tested_num-bai*100)%10)/10;
ge=tested_num-bai*100-shi*10;
sum=bai+shi+ge;
if(sum==k)
cout<<tested_num<<endl;
bai=0;
shi=0;
ge=0;
sum=0;
}
}
*//*
//使用for,while,do-while打印一个图形
#include<iostream>
using namespace std;
int i=1;
int main()
{
int j=9,q=0,p=1;
for(i=1;i<=5;i++)
{
for(p=1;p<=q+1;p++) {cout<<" ";}
for(p=1;p<=j;p++) {cout<<"* ";}
cout<<"\n";
j=j-2; q=q+1;
}
cout<<" "<<endl;
int x=1,y=0,z=9;
while(x<=5)
{
i=1; while(i<=y+1) {cout<<" "; i++;}
i=1; while(i<=z) {cout<<"* "; i++;}
x++ ; z=z-2 ; y=y+1;
cout<<" "<<endl;
}
cout<<" "<<endl;
int d=1,e=0,f=9;
do
{
i=1; do{cout<<" "; i++;} while(i<=e+1);
i=1; do{cout<<"* "; i++;} while(i<=f);
d++ ; e++ ; f--; f-- ;
cout<<" "<<endl;
}
while(d<=5) ;
}
*//*
//编写欧几里得算法并进行测试
#include<iostream>
using namespace std;
int main()
{
int m,n,trans=0;
cout<<"求最大公约数,请输入两个整数"<<endl;
cin>>m>>n;
if(m<n){trans=m;m=n;n=trans;}
do{trans=m%n;m=n;n=trans;}
while(m&n==0);
cout<<n;
}
*//*
//任意输入四位数的年份,判断该年是否是闰年
#include<iostream>
using namespace std;
int main()
{
int year;
cout<<"判断是否为闰年,请输入一个四位数年份"<<endl;
cin>>year;
if(year%100==0)
{ if(year%400==0)cout<<"是闰年"<<endl;
else cout<<"是平年"<<endl; }
else
{ if(year%4==0)cout<<"是闰年"<<endl;
else cout<<"是平年"<<endl; }
}
*//*
//编程求1!+2!+...+15!
#include<iostream>
using namespace std;
int main()
{
int m,m_fac=1,n,i,sum=0;
cout<<"求1!+2!+...+n!,请输入n"<<endl;
cin>>n;
for(m=1;m<=n;m++)
{
for(i=1;i<=m;i++)
{m_fac=m_fac * i;}
sum=sum+m_fac;
m_fac=1;
}
cout<<"结果为"<<sum<<endl;
}
*//*
//输入三个整数,从小到大输出;从大到小输出;先输出最大值再输出最小值
#include"iostream"
using namespace std;
int main()
{
int a,b,c,max,mid,min;
cout<<"请输入三个整数"<<endl;
cin>>a>>b>>c;
if(a>=b&&a>=c)
{ max=a;
if(b>=c){mid=b;min=c;}
else {mid=c;min=b;} }
else
{ if(b>=c)
{ max=b;
if(a>=c){mid=a;min=c;}
else {mid=c;min=a;} }
else
{ max=c;
if(a>=b){mid=a;min=b;}
else {mid=b;min=a;} }
}
cout<<"从小到大输出:"<<"\t"<<min<<"\t"<<mid<<"\t"<<max<<endl;
cout<<"从大到小输出:"<<"\t"<<max<<"\t"<<mid<<"\t"<<min<<endl;
cout<<"先输出大再小:"<<"\t"<<max<<"\t"<<min<<endl;
}
*//*
//3.3
#include<iostream>
using namespace std;
int main()
{
int x=4;
do{cout<<(x-=3)<<"\n"; } //输出x-3的值并把x-3赋值给x
while(!(--x)); //如果x-1=0就返回
}
*//*
//输入一串字符,分别统计出空格,数字和英文字母的个数
#include<iostream>
using namespace std;
int main()
{
int space_num = 0,digital_num = 0,character_num = 0;
char c;
while((c=cin.get())!=10)
{
if(c==' ')
space_num++;
else if((c>='0')&&(c<='9'))
digital_num++;
else if(((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z')))
character_num++;
}
cout<<"空格数 "<<space_num<<endl;
cout<<"数字数 "<<digital_num<<endl;
cout<<"字母数 "<<character_num<<endl;
}
*//*
//编写一个判断整数Num是否为素数的程序
#include<iostream>
using namespace std;
int main()
{
int Num,n,i;
cout<<"请输入一个整数"<<endl;
cin>>Num;
n=0.5 * Num + 1;
for(i=2;i<=n;i++)
{
if(Num%i==0)
{
cout<<"不是素数,能被"<<i<<"整除"<<endl;
break;
}
}
if(i>=n) //如果要在for函数外使用变量i,就要预先定义
{
cout<<"是素数"<<endl;
}
}
*//*
//循环结构的嵌套-打印国际象棋棋盘图形
#include<iostream>
using namespace std;
int main()
{
for(int j=0;j<8;j++)
{
for(int i=0;i<8;i++)
{
if((i+j)%2) cout<<"黑";
else cout<<" ";
}
cout<<"\n";
}
return 0;
}
*//*
//if语句的嵌套
#include<iostream>
using namespace std;
int main()
{
float x=0;
int y=0;
cin>>x;
if(x>0)
y=1;
else
{
if(x==0)
y=0;
else
y=-1;
}
cout<<y;
}
*//*
//随机取五种颜色的球,统计每种球出现次数
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int m1(0),m2(0),m3(0),m4(0),m5(0); //表示球出现次数
float p1,p2,p3,p4,p5,n0; //表示球出现概率
int m,n; //m指实时次数,n指总次数
cin>>n;
srand((unsigned)time(NULL)); //生成随机数
int color=rand()%6;
for(m=1;m<=n;m++)
{switch(color)
{case 1:
m1++;
break;
case 2:
m2++;
break;
case 3:
m3++;
break;
case 4:
m4++;
break;
case 5:
m5++;
break;
}
color=rand()%6;
}
n0=n; p1=m1/n0; p2=m2/n0; p3=m3/n0; p4=m4/n0; p5=m5/n0;
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<m5<<endl;
cout<<p1<<"\t"<<p2<<"\t"<<p3<<"\t"<<p4<<"\t"<<p5<<endl;
}
*//*
//计算1~100之间不包括三的整数和
#include<iostream>
using namespace std;
int main()
{
int i,sum=0;
for(i=1;i<=100;i++)
{
if(i%3==0)continue;
sum=sum+i;
}
cout<<"1~100之间不包括三的整数和为"<<sum<<endl;
}
*//*
//计算1~10的阶乘
#include<iostream>
using namespace std;
int main()
{
int n=0;
unsigned long factorial(1);
for(;++n<=10;)
{
factorial*=n;
cout<<n<<"!="<<factorial<<endl;
}
}
*//*
//用for编程计算数列A=n*n的平均数
#include<iostream>
using namespace std;
int main()
{
float mean,sum=0,n;
cout<<"计算数列A=n*n的平均数\n请输入n"<<endl;
cin>>n;
for(int k=1;k<=n;k++)
sum=sum+k*k;
if(n>0)
{
mean=sum/n;
cout<<mean<<endl;
}
else
{
cout<<"语法错误"<<endl;
}
}
*//*
//用do-while编程计算数列A=n*n的平均数
do
{
sum=sum+k*k;
k++;
}
while(k<=n);
if(n>0)
{
mean=sum/n;
cout<<mean<<endl;
}
else
{
cout<<"语法错误"<<endl;
}
}
*//*
//用while编程计算数列A=n*n的平均数
#include<iostream>
using namespace std;
int main()
{
float k=1,sum=0,n;
cout<<"计算数列A=n*n的平均数\n请输入n"<<endl;
cin>>n;
while(k<=n)
{
sum=sum+k*k;
k++;
}
float mean;
mean=sum/n;
cout<<mean<<endl;
}
*//*
//用switch语句判断考试成绩情况
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
switch(x/10)
{
case 10:
case 9:
cout<<"优秀"<<endl;
break;
case 8:
cout<<"良好"<<endl;
break;
case 7:
cout<<"及格"<<endl;
break;
case 6:
cout<<"赢"<<endl;
break;
default:
cout<<"寄"<<endl;
}
}
*//*
//正确运用if-else
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if(x>0)
{
if(x<50)
cout<<"x is <50"<<endl;
}
else
cout<<"x is<=0"<<endl;
}
*//*
//if判断考试成绩等级
#include<iostream>
using namespace std;
int main()
{
double grade=60;
while(grade<=100)
{
cout<<"请输入考试成绩"<<endl;
cin>>grade;
if((grade>=90)&&(grade<=100))
cout<<"特大赢\n\n";
else if((grade<90)&&(grade>=80))
cout<<"大赢\n\n";
else if ((grade<80)&&(grade>=70))
cout<<"中赢\n\n";
else if((grade<70)&&(grade>=60))
cout<<"大赢特赢\n\n";
else if((grade>100))
cout<<"唐\n"<<endl;
else
cout<<"lose\n\n";
}
}
*/