第六周作业

来源:互联网 发布:php mysql类 编辑:程序博客网 时间:2024/06/09 20:13

课本例题

例题4.2

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

例题4.6

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

例题4.10

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


 

课后习题

习题1

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int a[5],i,j,t;  
  7.     cout<<"请输入5个数:";  
  8.     cout<<"输出5个数为:";  
  9.     for(i=0;i<5;i++)  
  10.         cin>>a[i];  
  11.     cout<<"输出5个数为:";  
  12.     for(i=0;i<5;i++)  
  13.         cout<<setw(4)<<a[i];  
  14.     cout<<endl;  
  15.     for(i=0;i<4;i++)  
  16.     for(j=0;j<4-i;j++)  
  17.         if(a[j]<a[j+1])  
  18.         { t=a[j];a[j]=a[j+1];a[j+1]=t;}  
  19.     cout<<"5个数从大到小排列为:";  
  20.     for(i=0;i<5;i++)  
  21.         cout<<setw(4)<<a[i];  
  22.     cout<<endl;  
  23.     return 0;  
  24. }  


习题2

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


习题3

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int f[20]={1,1},i,j,count=0;  
  6.     for(i=2;i<20;i++)  
  7.         f[i]=f[i-2]+f[i-1];  
  8.     for(i=0;i<20;i++)  
  9.         if((f[i]>99)&&(f[i]<1000))  
  10.             count++;  
  11.     cout<<"Fibonacci数组前20个数有"<<count<<"个3位数"<<endl;  
  12.     cout<<"该数列中第十六项为:"<<f[15]<<endl;  
  13.     return 0;  
  14. }  


习题4



 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<cstring>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char str[50];  
  7.     int len,i,capitalletter=0,smallletter=0,space=0,number=0,otherstring=0;  
  8.     cout<<"请输入一行文字:";  
  9.     cin.get(str,50);  
  10.     len=strlen(str);  
  11.     for(i=0;i<len;i++)  
  12.         if(str[i]>='A'&&str[i]<='Z') capitalletter++;  
  13.         else if(str[i]>='a'&&str[i]<='z') smallletter++;  
  14.         else if(str[i]>='0'&&str[i]<='9') number++;  
  15.         else if(str[i]==' ') space++;  
  16.         else otherstring++;  
  17.     cout<<"大写字母的个数:"<<capitalletter<<endl;  
  18.     cout<<"小写字母的个数:"<<smallletter<<endl;  
  19.     cout<<"空格的个数:"<<space<<endl;  
  20.     cout<<"数字的个数:"<<number<<endl;  
  21.     cout<<"其他字符的个数:"<<otherstring<<endl;  
  22.     return 0;  
  23. }  

习题5


 

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