第一题
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b,sum=0; 6 cin>>a>>b; 7 for(int i=a;i<=b;i++) 8 sum+=i; 9 cout<<sum; 10 return 0; 11 }
第二题
1 #include<iostream> 2 #include<array> 3 using namespace std; 4 int main() 5 { 6 array<long double,100>arr; 7 arr[0]=arr[1]=1l; 8 for(int i=2;i<20;i++) 9 { 10 arr[i]=i*arr[i-1]; 11 cout<<i<<"!="<<arr[i]<<endl; 12 } 13 return 0; 14 }
第三题
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 double sum=0,a; 6 cout<<"enter the number"; 7 cin>>a; 8 for(;a!=0;) 9 { 10 sum+=a; 11 cin>>a; 12 } 13 cout<<sum; 14 return 0; 15 }
第四题
1 #include<iostream> 2 using namespace std; 3 const float a=100.0; 4 float daphne(int n) 5 { 6 float da; 7 da=a+a*0.1*n; 8 return da; 9 } 10 11 float cleo(int n) 12 { 13 float cl=a*(1+0.05); 14 for(int i=1;i<n;i++) 15 { 16 cl=cl*(1+0.05); 17 } 18 return cl; 19 } 20 21 int main() 22 { 23 float b,c; 24 int i; 25 for(i=1;;i++) 26 { 27 b=daphne(i); 28 c=cleo(i); 29 if(c>b)break; 30 } 31 cout<<"year="<<i<<endl; 32 cout<<"dephne="<<b<<endl; 33 cout<<"cleo="<<c<<endl; 34 return 0; 35 36 }
37 网上下载版 38 #include <iostream> 39 using namespace std; 40 void main() 41 { 42 double Dsum=100; 43 double Csum=100; 44 int i=0; 45 while (Csum<=Dsum) 46 { 47 Dsum+=10; 48 Csum+=Csum*0.05; 49 i++; 50 } 51 cout<<"After "<<i<<" year, Csum is bigger than Dsum"<<endl; 52 cout<<"Dsum="<<Dsum<<" Csum="<<Csum; 53 cin.get(); 54 }
第五题
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char*mon[12]={"1","2","3","4","5","6","7","8","9","10","11","12"}; 6 int num[12]; 7 int sum=0; 8 for(int i=0;i<12;i++) 9 { 10 cout<<"enter the number of the No."<<mon[i]<<" monthe ."; 11 cin>>num[i]; 12 sum+=num[i]; 13 } 14 cout<<sum; 15 return 0; 16 }
第六题
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char*mon[3][12]={"1","2","3","4","5","6","7","8","9","10","11","12","1","2","3","4","5","6","7","8","9","10","11","12","1","2","3","4","5","6","7","8","9","10","11","12"}; 6 int num[3][12]; 7 int sum[4]={0}; 8 for(int j=0;j<3;j++) 9 { 10 for(int i=0;i<12;i++) 11 { 12 cout<<"enter the number of the No."<<mon[j][i]<<" monthe ."; 13 cin>>num[j][i]; 14 sum[j]+=num[j][i]; 15 } 16 } 17 cout<<sum[0]<<","<<sum[1]<<","<<sum[2]<<","<<sum[0]+sum[1]+sum[2]<<","<<endl;; 18 return 0; 19 }
第七题
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 struct car 5 { 6 string make; 7 int year; 8 }; 9 int main() 10 { 11 int n; 12 cout<<"how may do you wish to catalog? "; 13 cin>>n; 14 car*ps=new car[n]; 15 for(int i=0;i<n;i++) 16 { 17 cin.get(); 18 cout<<"Car #"<<i+1<<endl; 19 cout<<"please enter the make: "; 20 getline(cin,ps[i].make); 21 cout<<"please enter the year made: "; 22 cin>>ps[i].year; 23 } 24 cout<<"here is your collection"<<endl; 25 for(int i=0;i<n;i++) 26 { 27 cout<<ps[i].year<<" "<<ps[i].make<<endl; 28 } 29 return 0; 30 }
第八题
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 char word[20]; 7 int i=0; 8 cout<<"enter words(to stop, type the word done):"<<endl; 9 cin>>word; 10 while(strcmp(word,"done")!=0) 11 { 12 i++; 13 cin>>word; 14 } 15 cout<<"you entered a total of "<<i<<" words."<<endl; 16 return 0; 17 }
首先输入整个字符串,cin读取第一个单词,就是空格之前的,空格之后的被保留。
继续cin输入时,在读取保留的部分的第一个单词。
第九题
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 string word; 7 cout<<"enter words(to stop, type the word done):"<<endl; 8 cin>>word; 9 int count=0; 10 for(;word!="done";) 11 { 12 count++; 13 cin>>word; 14 } 15 cout<<"you entered a total of "<<count<<" words."<<endl; 16 return 0; 17 }
第十题
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int i; 6 cout<<"enter number of rows "; 7 cin>>i; 8 for(int j=0;j<i;j++) 9 { 10 int k=4-j,m=j+1; 11 for(int n=0;n<k;n++) 12 {cout<<".";} 13 for(int n=0;n<m;n++) 14 {cout<<"*";} 15 cout<<endl; 16 } 17 return 0; 18 }