#include <iostream> #include<stdio.h> using namespace std; int main() { int num=100; for(;num<=999;num++) { int n1=num%10; int n3=num/100; int n2=(num/10)%10; if(num==n1*n1*n1+n2*n2*n2+n3*n3*n3) printf("%d ",num); } printf("\n"); return 0; }
习题2-1
#include<iostream> #include<stdio.h> int main() { int a,b,c; //以下循环在输入小于两个数时会自动跳过制表空格和回车 //在输入大于三个数时会把多余的数留在缓冲区中,在键入回车时结束 //在输入三个数时会在键入回车是结束,标准输入中 //另外在输入不符合格式化字符串中的格式时也会退出 while(scanf("%d%d%d",&a,&b,&c)==3) { int i; for( i=10;i<=100;i++) { if(i%3==a && i%5==b && i%7==c) { printf("%d\n",i); break; } } if(i>100) printf("No answer\n"); } }
习题2-2
#include<iostream> #include<stdio.h> void printfsy(int n); void printfsp(int n); int main() { int n; scanf("%d",&n); for(int i=n,j=0;i>=1;i--,j++) { int num=i*2-1; printfsp(j); printfsy(num); printf("\n"); } } void printfsy(int n) { for(int i=1;i<=n;i++) printf("#"); } void printfsp(int n) { for(int i=1;i<=n;i++) printf(" "); }
2-3
#include<iostream> #include <stdio.h> int main() { int n,m; while(scanf("%d%d",&n,&m)==2 && (n|m)!=0) { double s=0.0; //n*n 对于int会溢出,对long也会溢出,所以要用long long //另外The compiler looks at the literal value 655360*655360 //without considering the variable that you‘re assigning it // to/initializing it with. You‘ve written it as an int typed literal, // and it won‘t fit in an int.Use 655360LL*655360LL for(;n<=m;n++) { //long long mul=(long long)n*(long long)n; s+=1.0/n/n; } printf("%.5f\n",s); } return 0; }
2-4所谓陷阱就是溢出注意注释部分的内容
#include<iostream> #include <stdio.h> int main() { for(int i=123;i<=987;i++) { if(i*3<=987) { int sn=i; int mn=i*2; int ln=i*3; int sn_s=sn%10; int sn_m=(sn/10)%10; int sn_l=sn/100; int mn_s=mn%10; int mn_m=(mn/10)%10; int mn_l=mn/100; int ln_s=ln%10; int ln_m=(ln/10)%10; int ln_l=ln/100; if(ln_l*ln_m*ln_s*mn_l*mn_m*mn_s*sn_m*sn_l*sn_s==362880 && ln_l+ln_m+ln_s+mn_l+mn_m+mn_s+sn_m+sn_l+sn_s==45) printf("%d %d %d\n",sn,mn,ln); } } return 0; }
2-5
时间: 2024-11-05 19:43:08