第五周作业

1

# include <iostream>

using namespace std;

int main()
{
   int a[10];
   int i;
   for(i=0;i<10;i++)
	   a[i]=i*2+2;
   for(i=0;i<10;i++)
   {
      cout<<a[i]<<'\t';
	  if((i+1)%5==0)
		  cout<<endl;
   }


   return 0;

}

2

#include <iostream>

using namespace std;

int main()
{
    int i,math[40],n;
    float aver = 0.0;
    int unpassedcount = 0;
    int highscorecount =0;
    cout << "请输入学生人数: ";
    cin >> n;
    cout << "请输入成绩: ";
    for(i=0;i<n;i++)
    {
        cin >> math[i];
        if(math[i]>100||math[i]<0)
        {
            cout <<"请输入0到100:";
            i--;
            continue;
        }
        aver+=math[i];
    }
    aver/=n;
    for(i=0;i<n;i++)
    {
        if(math[i]<60)unpassedcount++;
        if(math[i]>=90)highscorecount++;
    }
    cout << "平均分为:"<<aver<<endl;
    cout <<"90分以上人数为:"<<highscorecount<<endl;
    cout << "不及格人数为:"<< unpassedcount<<endl;
    return 0;
}

3

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int a[10],i,big;
    cout <<"please input 10 numbers:\n";
    for(i=0;i<10;i++)
        cin>>a[i];
        cout << setw(4)<<a[i];
    cout <<endl;
    big = a[0];
    for(i=1;i<10;i++)
        if(a[i]>big)
        big=a[i];
    cout <<"the big number is: "<<big<<endl;
    return 0;
}


4..

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int a[10];
    int i,j,t;
    cout << "please input 10 number: \n";
    for (i=0;i<10;i++)
        cin >> a[i];
    cout << "the number are: ";
    for (i=0;i<10;i++)
        cout <<setw(4)<<a[i];
    cout << endl;
    for(i=0;i<9;i++)
        for(j=0;j<9;j++)
        if(a[j]>a[j+1])
    {
        t=a[j];a[j]=a[j+1];a[j+1]=t;
    }
    cout << "the sorted numbers are : ";
    for (i=0;i<10;i++)
        cout <<setw(4)<<a[i];
    cout <<endl;
    return 0;
}

5

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int i;
    int j;
    int a[5][5];
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            if(i%2==0)
                a[i][j]=i*5+j+1;
            else
                a[i][4-j]=i*5+j+1;
        }

    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
            cout << setw(4)<<a[i][j];
        cout << endl;
    }
    return 0;
}

6

#include <iostream>

using namespace std;

int main()
{
    char str[50];
    cout<<"Please input strings: ";
    cinget(str.50);
    cout << "The string is: ";
    cout << str << endl;

    return 0;
}


7

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char str[100];
    cout<<"Please input strings: ";
    cin.get(str,100);
    cout << "字符串"<<str<<"的反向字符串为:";
    for(int i=strlen(str)-1;i>=0;i--)
        cout << str[i];
    cout << endl;

    return 0;
}

8

#include <iostream>

using namespace std;

int main()
{
    char s[]="This is C programming test.";
    int i=0,pLen=0,maxpLen = 0, pSeat = 0;
    while(s[i]!='\0')
    {
        while(s[i]!=' '&&s[i]!='\0')
        {
            pLen++;
            i++;
        }
        if(pLen>maxpLen)
        {
            pSeat=i-pLen;
            maxpLen=pLen;
        }
        while(s[i]==' ')
            i++;
        pLen=0;
    }
    cout <<"最长的单词 :";
    for(i=0;i<maxpLen;i++)
        cout << s[pSeat+i];
    cout <<endl;


    return 0;
}

9

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char str[50];
    cout <<"Please input a string: ";
    cin.get(str,50);
    cout << "The length of string "<<str<<" is "<<strlen(str)<<endl;
    return 0;
}

习题

#include <iostream>

using namespace std;

int main()
{
    int arr[5],temp,i,j;
    cout <<"Please enter five number: "<<endl;
    for(i=0;i<5;i++)
        cin>>arr[i];
    for(j=0;j<5;j++)
    for(i=4;i>=0;i--)
    {
        if(arr[i]<arr[i+1])
        {
            temp=arr[i];
            arr[i]=arr[i+1];
            arr[i+1]=temp;
        }
    }
    for(i=0;i<5;i++)
    {
        cout <<arr[i];
    }
    cout << endl;
    return 0;
}

2

#include <iostream>

using namespace std;

int main()
{
    int arr[2][3],i,j,m,n;
    int temp=0;
    cout << "请输入6个任意整数: "<<endl;
    for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        cin >> arr[i][j];
    for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {
            if(arr[i][j]>temp)
            {
                temp=arr[i][j];
                m=i;
                n=j;
            }
        }
    cout <<"该数组最大的数是: "<<arr[m][n]<<" 下标为: "<<m<<","<<n;
    cout << endl;
    return 0;
}

3

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int i,j=0;
    int f[40]={1,1};
    for(i=2;i<40;i++)
        f[i]=f[i-2]+f[i-1];
    for(i=0;i<20;i++)
    {

        if(f[i]>100)
        j++;
    }
    cout <<"前20个数中有 "<<j<<"个三位数,第16项为:"<<f[15];
    cout <<endl;
    return 0;
}

4

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    char ch[50];
    cout << "请输入一行文字:"<<endl;
    cin.get(ch,50);
    int i;
    int up=0,low=0,blank=0,other=0,num=0;
    for(i=0;ch[i]!='\0';i++)
    {
        if(isupper(ch[i]))
            up++;
        else if(islower(ch[i]))
            low++;
        else if (ch[i]==' ')
            blank++;
        else if(isdigit(ch[i]))
            num++;
        else
            other++;
    }
    cout << "文字中有 "<<up<<" 个大写字母, "<<low<<"个小写字母,"<<blank<<"个空格,"
    <<num<<"个数字以及 "<<other<<"个其他字符。"<<endl;
    return 0;
}

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
  char ch[50];
  int i,num=0;
  cout << "请输入一行字符串:"<<endl;
  cin.get(ch,50);
  cout <<"反向后的字符串为:";
  for(i=strlen(ch)-1;i>=0;i--)
  {
      cout <<ch[i];
  }
  cout <<endl;
  cout << "该字符串长度为:"<<strlen(ch)<<endl;
}

#include <iostream>
using namespace std;
int main()
{
   int arr1[4][5],arr2[5][3],arr3[4][3];
   int i,j,k;
   cout<<"请输入4*5矩阵的数:"<<endl<<endl<<endl;
   for(i=0;i<4;i++)
   {
      for(j=0;j<5;j++)
      {
          cout<<"请输入第#"<<i+1<<"行"<<"第#"<<j+1<<"个数:"<<endl;
           cin>>arr1[i][j];
      }
   }
   cout<<"请输入5*3矩阵的数:"<<endl<<endl<<endl;
   for(i=0;i<5;i++)
   {
       for(j=0;j<3;j++)
       {
           cout<<"请输入第#"<<i+1<<"行"<<"第#"<<j+1<<"个数:"<<endl;
           cin>>arr2[i][j];
       }
   }
   cout<<"4*5的矩阵为:"<<endl;
   for(i=0;i<4;i++)
       {
           for(j=0;j<5;j++)
              cout<<arr1[i][j]<<"  ";
           cout<<endl;
       }
   cout<<"5*3的矩阵为:"<<endl;
   for(i=0;i<5;i++)
       {
           for(j=0;j<3;j++)
              cout<<arr2[i][j]<<"  ";
           cout<<endl;
       }
   for(i=0;i<4;i++)
       for(k=0;k<3;k++)
           {
               j=0;
               arr3[i][k]=arr1[i][j]*arr2[j][k]+arr1[i][j+1]*arr2[j+1][k]+arr1[i][j+2]*arr2[j+2][k];
           }
   cout<<"结果是:"<<endl;
   for(i=0;i<4;i++){
       for(j=0;j<3;j++)
       cout <<arr3[i][j]<<" ";
       cout <<endl;
       }
   return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
#define S 100
void odd(int N,int index);
void uodd_1(int N);
void uodd_2(int N);
int arr[S][S];
int ox,oy;
int i,j;
int main ()
{
   int N;
   cout<<"请输入一个大于3的整数N: ";
   cin >> N;
   if(N%2==1)
   {
       ox=oy=0;
       odd(N,0);
   }

   else
   {
      if(N%4==0)
		  uodd_1(N);
	  else uodd_2(N);
   }
   cout << "魔方阵:"<<endl;
   for(i=0;i<N;i++)
   {
	   for(j=0;j<N;j++)
		   cout <<arr[i][j]<<"    ";
	   cout<<endl;
   }

   return 0;
}

void odd(int N,int index)
{
   int x,y,temp;
   y=N/2;
   x=0;
   for(temp=index+1;temp<=index+pow(N,2);temp++)
   {
      arr[ox+x][oy+y]=temp;
      if(temp%N==0)x++;
      else x--,y++;
      x=(x%N+N)%N;
      y=(y%N+N)%N;
   }



}

void uodd_1(int N)
{

   int temp=1,add;
   for(i=0;i<N;i++)
	   for(j=0;j<N;j++)
	   {
		   arr[i][j]=temp;
	       temp++;
	   }
   add=N*N+1;
   j=0;
   for(i=0;i<N;i++)
   {
       arr[i][j]=add-arr[i][j];
	   j++;
   }
   j=N-1;
   for(i=0;i<N;i++)
   {
       arr[i][j]=add-arr[i][j];
	   j--;
   }


}

void uodd_2(int N)
{
   int m,n,temp;
   ox=oy=0;
   odd(N/2,pow(N/2,2)*0);
   ox=oy=N/2;
   odd(N/2,pow(N/2,2)*1);
   ox=0;
   oy=N/2;
   odd(N/2,pow(N/2,2)*2);
   ox=N/2;
   oy=0;
   odd(N/2,pow(N/2,2)*3);
   m=(N-2)/4;
   for(i=0;i<N/2;i++)
   {
       for(j=0;j<m;j++)
       {
           n=(i==N/4)?N/4+j:j;
           temp=arr[i][n];
           arr[i][n]=arr[i+N/2][n];
           arr[i+N/2][n]=temp;
       }
       for(j=0;j<m-1;j++)
        {
            n=N/2+N/4+j;
            temp=arr[i][n];
            arr[i][n]=arr[i+n/2][n];
            arr[i+N/2][n]=temp;
        }
   }

}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值