第六周作业 例题

例题1:

1:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int a[10];  
  7.     int i;  
  8.     for(i=0;i<10;i++)  //给所有的数组元素赋初值  
  9.         a[i]=i*2+2;  
  10.     for(i=0;i<10;i++)  //输出所有的数组元素,没行显示5个数组元素  
  11.     {  
  12.         cout<<a[i]<<'\t';  
  13.         if((i+1)%5==0)  
  14.             cout<<endl;  
  15.       
  16.       
  17.     }  
  18.   
  19.     return 0;  
  20.   
  21. }  
2:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int a[10];  
  7.     int i;  
  8.       
  9.     for(i=0;i<10;i++)    
  10.     {  
  11.         a[i]=i*2+2;      
  12.         cout<<a[i]<<'\t';  
  13.         if((i+1)%5==0)  
  14.             cout<<endl;  
  15.       
  16.       
  17.     }  
  18.   
  19.     return 0;  
  20. }  
  21. //一个FOR循环就能实现了。  

例题2:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int i,math[40],n;  
  7.     float aver=0.0;     //平均分  
  8.     int unpassedcout = 0;  // 不及格学生人数  
  9.     int highscorecout =0;  // 90分以上学生人数  
  10.     cout<<"请输入学生人数:";  
  11.     cin>>n;  
  12.     cout<<"请输入学生成绩:";  
  13.     for(i=0;i<n;i++)  
  14.     {  
  15.         cin>>math[i];  
  16.         aver+=math[i];  
  17.     }  
  18.     aver/=n;  
  19.     for(i=0;i<n;i++)  
  20.     {  
  21.         if(math[i]<60) unpassedcout++;  
  22.         if(math[i]>90) highscorecout++;    
  23.     }  
  24.   
  25.     cout<<"平均分为:"<<aver<<endl;  
  26.     cout<<"90分以上人数为:"<<highscorecout<<endl;  
  27.     cout<<"不及格人数为:"<<unpassedcout<<endl;  
  28.   
  29.     return 0;  
  30.   
  31. }  

例题3:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     int a[10],i,big;  
  8.     cout<<"please input 10 numbers:\n";  
  9.     for(i=0;i<10;i++)  
  10.         cin>>a[i];  
  11.     cout<<"the numbers are:";  
  12.     for(i=0;i<10;i++)  
  13.         cout<<setw(4)<<a[i];  
  14.     cout<<endl;  
  15.     big=a[0];  
  16.     for(i=1;i<10;i++)  
  17.         if(a[i]>big)  
  18.           big=a[i];  
  19.     cout<<"the big number is :"<<big<<endl;  
  20.   
  21.     return 0;  
  22. }  

例题3:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     int a[10];  
  8.     int i,j,t;  
  9.     cout<<"please input 10 numbers:\n";  
  10.     for(i=0;i<10;i++)  
  11.         cin>>a[i];    //输入数组元素  
  12.     cout<<"the numbers are:";  
  13.     for(i=0;i<10;i++)  
  14.         cout<<setw(4)<<a[i];  
  15.     cout<<endl;  
  16.     for(i=0;i<9;i++)  
  17.     for(j=0;j<9-i;j++)  
  18.         if(a[j]>a[j+1])  
  19.         {  
  20.           t=a[j];a[j]=a[j+1];a[j+1]=t;  
  21.         }  
  22.     cout<<"the sorted numbers are:";  
  23.     for(i=0;i<10;i++)  
  24.         cout<<setw(4)<<a[i];  //输出数组元素  
  25.     cout<<endl;  
  26.   
  27.     return 0;  
  28.         
  29. }  

例题4:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     int i;  
  8.     int f[40]={1,1};  
  9.     for(i=2;i<40;i++)  
  10.         f[i]=f[i-2]+f[i-1];  
  11.     for(i=0;i<40;i++)  
  12.     {  
  13.             if(i%4==0)  
  14.             cout<<endl;  
  15.             cout<<setw(12)<<f[i];  
  16.     }  
  17.     cout<<endl;  
  18.     return 0;  
  19.       
  20. }  

例题5:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.      int i,j;  
  8.      int a[5][5];  
  9.      for(i=0;i<5;i++)  
  10.      {  
  11.          for(j=0;j<5;j++)  
  12.          {  
  13.            if(i%2==0)  
  14.              a[i][j]=i*5+j+1;  
  15.            else  
  16.              a[i][4-j]=i*5+j+1;  
  17.   
  18.          }  
  19.      }  
  20.      for(i=0;i<5;i++)  
  21.      {  
  22.        for(j=0;j<5;j++)  
  23.            cout<<setw(4)<<a[i][j];  
  24.        cout<<endl;  
  25.        
  26.        
  27.      }  
  28.      return 0;  
  29. }  

例题6:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     char str[50];  
  7.     cout<<"Please input strings:";  
  8.     cin.get(str,50);            //默认结束符是Enter  
  9.     cout<<"The strings is:";  
  10.     cout<<str<<endl;  
  11.   
  12.     return 0;  
  13.   
  14. }  

例题7:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     int a[2][3];  
  8.     int i,j,big;  
  9.     cout<<"请输入二行三列二维数组的元素值:"<<endl;  
  10.             
  11.     for(i=0;i<2;i++)  
  12.     for(j=0;j<3;j++)  
  13.     {   
  14.         cout<<"a["<<i<<"]"<<"["<<j<<"]=";  
  15.         cin>>a[i][j];  
  16.     }  
  17.     cout<<"该二维数组为:";  
  18.     for(i=0;i<2;i++)  
  19.     for(j=0;j<3;j++)  
  20.     {     
  21.         if(j%3==0)  
  22.         cout<<endl;  
  23.         cout<<setw(4)<<a[i][j];  
  24.     }  
  25.     cout<<endl;  
  26.     big=a[0][0];  
  27.     for(i=0;i<2;i++)  
  28.     for(j=0;j<3;j++)  
  29.         if(a[i][j]>=big)  
  30.             big=a[i][j];  
  31.     for(i=0;i<2;i++)  
  32.     for(j=0;j<3;j++)  
  33.         if(a[i][j]==big)  
  34.     cout<<"该数组中最大元素为:"<<"a["<<i<<"]"<<"["<<j<<"]="<<a[i][j];  
  35. }  


例题8:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     char str[50];  
  9.     cout<<"Please input a string:";  
  10.     cin.get(str,50);  
  11.     cout<<"The length of string"<<str<<"is"<<strlen(str)<<endl;  
  12.   
  13.     return 0;  
  14.   
  15.   
  16. }  
例题9:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<cstring>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.    char str[100];  
  8.    cout<<"请输入一个字符串:";  
  9.    cin.get(str,100);  
  10.    cout<<"字符串"<<str<<"的反向字符串为:";  
  11.    for(int i=strlen(str)-1;i>=0;i--)  
  12.        cout<<str[i];  
  13.    cout<<endl;  
  14.   
  15.    return 0;  
  16.   
  17.   
  18. }  

例题10:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>    
  2.     
  3. using namespace std;    
  4.     
  5. int main()    
  6. {    
  7.     char s[]="This is C programming test.";    
  8.     int i=0,pLen=0,maxpLen = 0, pSeat = 0;    
  9.     while(s[i]!='\0')    
  10.     {    
  11.         while(s[i]!=' '&&s[i]!='\0')    
  12.         {    
  13.             pLen++;    
  14.             i++;    
  15.         }    
  16.         if(pLen>maxpLen)    
  17.         {    
  18.             pSeat=i-pLen;    
  19.             maxpLen=pLen;    
  20.         }    
  21.         while(s[i]==' ')    
  22.             i++;    
  23.         pLen=0;    
  24.     }    
  25.     cout <<"最长的单词 :";    
  26.     for(i=0;i<maxpLen;i++)    
  27.         cout << s[pSeat+i];    
  28.     cout <<endl;    
  29.     
  30.     
  31.     return 0;    
  32. }    

例题11:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     char str[50];  
  9.     cout<<"Please input a string:";  
  10.     cin.get(str,50);  
  11.     cout<<"The length of string"<<str<<"is"<<strlen(str)<<endl;  
  12.   
  13.     return 0;  
  14.   
  15.   
  16. }  

例题12:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. #include<cstring>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     char str[10];  
  8.     cout<<"请输入字符串,直到输入 hello 后程序结束:"<<endl;  
  9.     do{  
  10.         cin>>str;  
  11.       
  12.     }while(strcmp(str,"hello")!=0);  
  13.   
  14.         return 0;  
  15.   
  16.   
  17. }  

例题13:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.      char str[50];  
  7.      int len=0;  
  8.      cout<<"请输入一个字符串:";  
  9.      cin.get(str,50);  
  10.      while(str[len] !='\0')  
  11.          len++;  
  12.      cout<<"字符串"<<str<<"的长度"<<len<<endl;  
  13.   
  14.      return 0;  
  15.   
  16. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值