第六周作业

来源:互联网 发布:淘宝买花种子哪家靠谱 编辑:程序博客网 时间:2024/05/17 10:09
[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int i,j,t;  
  7.     int str[5];  
  8.     cout<<"请输入五个数:"<<endl;  
  9.     for(i=0;i<5;i++)  
  10.         cin>>str[i];  
  11.     cout<<"这五个数的从小到大的顺序是:"<<endl;  
  12.     for(i=0;i<4;i++)  
  13.     for(j=0;j<4-i;j++)  
  14.         if(str[j]>str[j+1])  
  15.         {t=str[j];str[j]=str[j+1];str[j+1]=t;}  
  16.     for(i=0;i<5;i++)  
  17.         cout<<str[i]<<setw(4);  
  18.         cout<<endl;  
  19.   
  20.  return 0;  
  21. }  


习题2

[cpp] view plaincopy
  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]<<endl;  
  35. }  


习题3

[cpp] view plaincopy
  1. #include<iostream>  
  2. using namespace std;  
  3. int function(int n);  
  4. int main()  
  5. {  
  6.     int i,count;  
  7.     count=0;  
  8.     for(i=1;i<=20;i++)  
  9.     {  
  10.         cout<<function(i)<<" ";  
  11.         cout<<endl;  
  12.         if(function(i)>=100&&function(i)<=999)  
  13.             count++;  
  14.     }  
  15.      cout<<"Fibonacci数列前20个数中的三位数个数为:"<<count<<endl;  
  16.      cout<<"该数列第16项数据是:"<<function(16)<<endl;  
  17.       
  18.      return 0;      
  19. }  
  20. int function(int n)  
  21. {  
  22.    if(n==1||n==2)return 1;  
  23.    else return function(n-1)+function(n-2);  
  24. }  


习题4

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {     
  7.       
  8.     char a[100];  
  9.     int i,xiaoxue=0,shuzi=0,daxue=0,kongge=0,qita=0;  
  10.     cout<<"请输入一行字符小于100的文字:";  
  11.     cin.get(a,100);  
  12.     for(i=0;i<100;i++)  
  13.     {  
  14.      if(a[i]>='a'&&a[i]<='z')  
  15.      xiaoxue++;  
  16.      else if  
  17.      (a[i]>='0'&&a[i]<='9')   //因为a被定义为char,,所以应该写成'0' '9'  
  18.      shuzi++;  
  19.      else if  
  20.      (a[i]>='A'&&a[i]<='Z')  
  21.      daxue++;  
  22.      else if  
  23.      (a[i]==' ')  
  24.      kongge++;  
  25.      else if  
  26.      (a[i]=='\0')  
  27.      qita=strlen(a)-(shuzi+xiaoxue+daxue+kongge);  
  28.     }  
  29.      cout<<"数字个数为:"<<shuzi<<endl;  
  30.      cout<<"小写字母个数为:"<<xiaoxue<<endl;  
  31.      cout<<"大写字母个数为:"<<daxue<<endl;  
  32.      cout<<"空格个数为:"<<kongge<<endl;  
  33.      cout<<"其他字符个数为:"<<qita<<endl;  
  34.      return 0;  
  35. }  


习题5

[cpp] view plaincopy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.    char a[100];  
  7.    int i;  
  8.    cout<<"输入一字符串:"<<endl;  
  9.    cin.get(a,100);  
  10.    cout<<"反向输出每一个字符:"<<endl;  
  11.    for(i=strlen(a);i>0;i--)  
  12.        cout<<a[i-1];  
  13.    cout<<"该字符串长度为:"<<strlen(a)<<endl;  
  14.          
  15.  return 0;  
  16.   
  17. }  


习题6

[cpp] view plaincopy
  1. #include<iostream>    
  2. using namespace std;    
  3.     
  4. int main()    
  5. {    
  6.     int i,b,shuzi;  
  7.     shuzi=0;  
  8.     b=0;    
  9.     char a[100];    
  10.     cout<<"输入一个字符串:"<<endl;    
  11.     cin.get(a,100);    
  12.     cout<<"删除了所有数字后的字符串为:"<<endl;    
  13.     for(i=0;i<100;i++)    
  14.         if(a[i]=='\0')    
  15.             break;    
  16.         else if(a[i]>='0'&&a[i]<='9') shuzi++;    
  17.         else    
  18.         cout<<a[i];    
  19.         cout<<"该字符串的长度为:"<<strlen(a)-shuzi<<endl;    
  20.             
  21.     return 0;    
  22.         
  23. }    


习题7

[cpp] view plaincopy
  1. 题一:  
  2. #include<iostream>  
  3. #include<iomanip>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.    int i,j,k,p;  
  8.    p=0;  
  9.    int a[4][5],b[5][3],ji[4][3];  
  10.    cout<<"请输入4x5矩阵的元素:";  
  11.    for(i=0;i<4;i++)  
  12.    for(j=0;j<5;j++)  
  13.    cin>>a[i][j];  
  14.     
  15.    cout<<"请输入5x3矩阵的元素:";  
  16.    for(i=0;i<5;i++)  
  17.    for(j=0;j<3;j++)  
  18.        cin>>b[i][j];  
  19.    for(i=0;i<4;i++)  
  20.    {  
  21.       for(j=0;j<3;j++)  
  22.       for(k=0;k<5;k++)  
  23.       {  
  24.        p+=a[i][k]*b[k][j];  
  25.        ji[i][j]=p;  
  26.       }  
  27.       p=0;   
  28.    }  
  29.    cout<<"两个矩阵的乘积为:"<<endl;  
  30.    for(i=0;i<4;i++)  
  31.    {  
  32.        for(j=0;j<3;j++)  
  33.        cout<<setw(4)<<ji[i][j];  
  34.        cout<<endl;  
  35.    }  
  36.    return 0;  
  37.   
  38. }  


[cpp] view plaincopy
  1. 题二:  
  2. #include<iostream>  
  3. using namespace std;  
  4. void main()  
  5. {  
  6.  int a[32][32],i,j,k,p,n;  
  7.  p=1;  
  8.  while(p==1)  
  9.  {  
  10.   cout<<"Enter n(n=1~25):";  
  11.   cin>>n;  
  12.   if((n!=0)&&(n<=25)&&(n%2!=0))  
  13.    p=0;  
  14.  }  
  15.  for(i=1;i<=n;i++)  
  16.   for(j=1;j<=n;j++)  
  17.    a[i][j]=0;  
  18.  j=n/2+1;  
  19.  a[1][j]=1;  
  20.  for(k=2;k<=n*n;k++)  
  21.  {  
  22.   i=i-1;  
  23.   j=j+1;  
  24.   if((i<1)&&(j>n))  
  25.   {  
  26.    i=i+2;  
  27.    j=j-1;  
  28.   }  
  29.   else   
  30.   {  
  31.    if(i<1)  
  32.     i=n;  
  33.    if(j>n)  
  34.     j=1;  
  35.   }  
  36.   if(a[i][j]==0)  
  37.    a[i][j]=k;  
  38.   else   
  39.   {  
  40.    i=i+2;  
  41.    j=j-1;  
  42.    a[i][j]=k;  
  43.   }  
  44.  }  
  45.  for(i=1;i<=n;i++)  
  46.  {  
  47.   for(j=1;j<=n;j++)  
  48.    cout<<a[i][j]<<" ";  
  49.   cout<<endl;  
  50.  }  

0 0
原创粉丝点击