摘要: 计划:陌生人每天给你十万元,而你第一天只需要给我一分钱,第二天我仍然给你十万 元,你给我两分钱,第三天我仍然给你十万元,你给我四分钱........你每天给我的钱是前一天的两倍,直到满一个月(30天).求他们互相给对方多少钱?代码实现: 1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 void main() 5 { 6 float a,b,s=0; 7 a=100000*30; 8 for(int i=1;i<=30;i++) 9 {10 b=0.01*pow(2,i-1);11 s+ 阅读全文
posted @ 2012-04-08 13:53 iamvirus 阅读(980) 评论(0) 推荐(0) 编辑
摘要: 来自百度知道:int getchar(void); getchar()从控制台读取一个字符,并回显。当程序调用getchar时,程序就等着用户按键。用户输入的字符被存放在键盘缓冲区中,直到用户按回车为止(回车字符也放在缓冲区中)。当用户键入回车之后,getchar才开始从stdio流中每次读入一个字符。getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕。如用户在按回车之前输入了不只一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取。也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直... 阅读全文
posted @ 2012-04-08 13:32 iamvirus 阅读(752) 评论(0) 推荐(0) 编辑
摘要: 代码实现: 1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 void main() 5 { 6 char ch; 7 int letter=0,number=0,space=0,other=0; 8 while((ch=getchar())!='\n') 9 if('A'<=ch&&ch<='Z'||'a'<=ch&&ch<='z') letter++; 阅读全文
posted @ 2012-04-08 12:27 iamvirus 阅读(2253) 评论(0) 推荐(0) 编辑
摘要: 用do ...whie实现: 1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int i=1,s=0; 6 do{ 7 s+=i; 8 i++; 9 }10 while(s<=1000);11 cout<<"求得最大的自然数 "<<i<<endl;12 }运行结果: 阅读全文
posted @ 2012-04-08 08:33 iamvirus 阅读(308) 评论(0) 推荐(0) 编辑
摘要: 例如:当a=2,n=4时,Sn=2+22+222+2222.a和n由键盘输入.代码实现: 1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 void main() 5 { 6 int i,a,n,s=0,sum=0; 7 cout<<"请输入a的值 ";cin>>a; 8 cout<<"请输入n的值 ";cin>>n; 9 for(i=1;i<=n;i++)10 {s+=a*pow(10,i-1);1 阅读全文
posted @ 2012-04-08 07:44 iamvirus 阅读(1074) 评论(0) 推荐(0) 编辑
摘要: 所谓"水仙花数"是指一个三位数,其各位数字之和的立方等于该数本身,例如:153是水仙花数,因为153=13+53+33代码实现:一一列举for循环嵌套 1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int a,b,c,m,n; 6 for(a=1;a<=9;a++) 7 for(b=1;b<=9;b++) 8 for(c=1;c<=9;c++) 9 {10 m=a*a*a+b*b*b+c*c*c;11 n=100*a+10*b+c;12 if(m==n)13 co 阅读全文
posted @ 2012-04-07 23:51 iamvirus 阅读(3521) 评论(0) 推荐(0) 编辑
摘要: 所谓完全数是指一个数恰好等于它所有因子之和,例如,6是完全数,因为6的因子为1 2 3,而6=1+2+3.#include<iostream>using namespace std;void main(){ int i,j,sum; for(i=1;i<=1000;i++) { sum=0; for(j=1;j<=i/2;j++) if(i%j==0) sum+=j; if(sum==i) cout<<i<<endl; }} 运行结果: 阅读全文
posted @ 2012-04-07 23:30 iamvirus 阅读(9227) 评论(0) 推荐(0) 编辑
摘要: 初步实现代码:#include<iostream>#include<iomanip>using namespace std;void main(){ int i,j; for(i=1;i<=5;i++) { cout<<setw(7-i); for(j=1;j<=2*i-1;j++) cout<<'*'; cout<<endl; }}运行结果如图:题型变换为输入行数,然后出现金字塔形状 阅读全文
posted @ 2012-04-07 22:27 iamvirus 阅读(1014) 评论(0) 推荐(0) 编辑
摘要: 所谓回文数是从左到右读与从右到左读都是一样的数!例如7887,23432都是回文数.不用数组方法: 1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int i,x,y,r; 6 y=0; 7 cout<<"请输入数 "<<endl;cin>>x; 8 i=x; 9 while(i!=0)10 {11 r=i%10;12 i=i/10;13 y=y*10+r;14 }15 if(y==x)16 cout<<x<<" 阅读全文
posted @ 2012-04-07 15:34 iamvirus 阅读(11334) 评论(0) 推荐(0) 编辑
摘要: 代码实现: 1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int m,n,r,x; 6 cout<<"请输入正整数m ";cin>>m; 7 cout<<"请输入正整数n ";cin>>n; 8 x=m*n; 9 while(n!=0)10 {11 r=m%n;12 m=n;13 n=r;14 }15 cout<<"最大公约数 "<<m<<" 最小公 阅读全文
posted @ 2012-04-07 14:43 iamvirus 阅读(3950) 评论(0) 推荐(0) 编辑