习题2-1
#include<iostream> #include<math.h> using namespace std; int main(){ int x,n; while(cin>>x){ n=0; while(x){ x/=10; n++; } cout<<n<<endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ int x,a,b,c; for(x=100;x<1000;x++){ a=x/100; b=(x/10)%10; c=x%10; if(x==a*a*a+b*b*b+c*c*c) cout<<x<<endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ int x,y,z,sum; while(cin>>x>>y>>z){ for(sum=10;sum<=100;sum++){ if(sum%3==x&&sum%5==y&&sum%7==z){ cout<<sum<<endl; break; } } if(sum==101) cout<<"No answer"<<endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ int n; while(cin>>n){ for(int i=n;i>0;i--){ for(int j=0;j<n-i;j++){ cout<<" "; } for(int j=0;j<2*i-1;j++){ cout<<"#"; } cout<<endl; } } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ int n,i,m; freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); while(cin>>n){ int a[n],N=0; for(i=0;i<n;i++){ cin>>a[i]; } cin>>m; for(int j=0;j<n;j++){ if(a[i]<m) N++; } cout<<N<<endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ int n; while(cin>>n){ float sum=0; for(int i=1;i<=n;i++){ sum+=(float)1.0/i; } cout.precision(3); cout<<fixed<<sum<<endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ double n=1,sum=0; for(int i=1;fabs(n)>1e-6;i++){ n=pow(-1,i-1)*1.0/(2*i-1); sum+=n; } cout.precision(7); cout<<sum<<endl; return 0; }习题2-8
#include<iostream> #include<math.h> using namespace std; int main(){ int m,n; double x=0; while(cin>>n>>m){ for(int i=n;i<=m;i++){ if(n>sqrt(pow(10,5))) { x=0.00001;break; } if(i*i>pow(10,5)) break; x+=1.0/(i*i); } cout.precision(5); cout<<fixed<<x<<endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main(){ int a,b,c; double x=0; while(cin>>a>>b>>c){ x=(double)a/b; cout.precision(c); cout<<fixed<<x<<endl; } return 0; }
#include<iostream> #include<set> using namespace std; int ins(set<int> &s,int x){ for(int i=0;i<3;i++){ s.insert(x%10); x/=10; } return 1; } int main(){ int x,y,z; set<int> s; for(x=123;x<333;x++){ s.clear(); y=2*x; z=3*x; ins(s,x); ins(s,y); ins(s,z); if(s.size()==9) cout<<x<<" "<<y<<" "<<z<<endl; } return 0; }