您的位置:首页 > 其它

第六周作业

2014-04-18 08:49 351 查看
例题4.1

01.#include<iostream>
02.using namespace std;
03.int main()
04.{
05.    int a[10];
06.    int i;
07.    for(i=0;i<10;i++)
08.        a[i]=i*2+2;
09.    for(i=0;i<10;i++)
10.    {
11.        cout<<a[i]<<endl;
12.        cout<<endl;
13.    }
14.    return 0;
15.}


例4.2

01.#include <iostream>
02.using namespace std;
03.
04.int main()
05.{
06.    int i,math[40],n;
07.    float aver=0.0;
08.    int unpassedcount=0;
09.    int passedcount=0;
10.    int highscorecount=0;
11.    cout<<"请输入学生人数:";
12.    cin>>n;
13.    cout<<"请输入成绩:"<<endl;
14.    for(i=0;i<n;i++)
15.    {
16.        cin>>math[i];
17.        aver+=math[i];
18.    }
19.    aver/=n;
20.    for(i=0;i<n;i++)
21.    {
22.        if(math[i]<60)
23.            unpassedcount++;
24.        else
25.            passedcount++;
26.        if(math[i]>=90) highscorecount++;
27.    }
28.    cout<<"平均分为:"<<aver<<endl;
29.    cout<<"90分以上的人数为:"<<highscorecount<<endl;
30.    cout<<"及格人数为:"<<passedcount<<endl;
31.    cout<<"不及格人数为:"<<unpassedcount<<endl;
32.
33.    return 0;
34.}


例4.3

01.#include <iostream>
02.#include <iomanip>
03.using namespace std;
04.
05.int main()
06.{
07.    int a[10],i,big,small;
08.    cout<<"请输入10个数:\n";
09.    for(i=0;i<10;i++)
10.        cin>>a[i];
11.    cout<<"这些数是:";
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<<"最大的数为:"<<big<<endl;
20.    small=a[0];
21.    for(i=1;i<10;i++)
22.        if(a[i]<small)
23.            small=a[i];
24.    cout<<"最小的数为:"<<small<<endl;
25.
26.    return 0;
27.}


例4.4

01.#include <iostream>
02.#include <iomanip>
03.using namespace std;
04.
05.int main()
06.{
07.    int a[10];
08.    int i,j,t;
09.    cout<<"请输入10个数字:\n";
10.    for(i=0;i<10;i++)
11.        cin>>a[i];
12.    cout<<"这些数字为:";
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+1]>a[j])
19.            {
20.                t=a[j+1];
21.                a[j+1]=a[j];
22.                a[j]=t;
23.            }
24.    cout<<"从大到小为:";
25.    for(i=0;i<10;i++)
26.        cout<<setw(4)<<a[i];
27.    cout<<endl;
28.
29.    return 0;
30.}


例4.5

01.#include <iostream>
02.#include <iomanip>
03.using namespace std;
04.
05.int main()
06.{
07.    int i;
08.    int f[40]={1,1};
09.    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.
19.    return 0;
20.}


例4.6

01.#include <iostream>
02.#include <iomanip>
03.using namespace std;
04.
05.int main()
06.{
07.    int i,j;
08.    int a[20][5];
09.    for(i=0;i<20;i++)
10.        for(j=0;j<5;j++)
11.            if(i%2==0)
12.                a[i][j]=i*5+j+1;
13.            else
14.                a[i][4-j]=i*5+j+1;
15.    for(i=0;i<20;i++)
16.    {
17.        for(j=0;j<5;j++)
18.            cout<<setw(4)<<a[i][j];
19.        cout<<endl;
20.    }
21.
22.    return 0;
23.}


例4.7

01.#include <iostream>
02.#include <iomanip>
03.using namespace std;
04.
05.int main()
06.{
07.    int a[4][4],i,j;
08.    cout<<"请输入2行3列二维数组的元素值:"<<endl;
09.    for(i=0;i<4;i++)
10.        for(j=0;j<4;j++)
11.        {
12.            cout<<"a["<<i<<"]["<<j<<"]=";
13.            cin>>a[i][j];
14.        }
15.    cout<<"\n该二维数组为:"<<endl;
16.    for(i=0;i<4;i++)
17.    {
18.        for(j=0;j<4;j++)
19.            cout<<setw(6)<<a[i][j];
20.        cout<<endl;
21.    }
22.    int row=0,column=0,max=a[0][0];
23.    for(i=0;i<4;i++)
24.        for(j=0;j<4;j++)
25.            if(max<a[i][j])
26.            {
27.                max=a[i][j];
28.                row=i;
29.                column=j;
30.            }
31.    cout<<"\n该数组中最大的元素值为:"<<"a["<<row<<"]["<<column<<"]="<<a[row][column]<<endl;
32.    int row1=0,column1=0,min=a[0][0];
33.    for(i=0;i<4;i++)
34.        for(j=0;j<4;j++)
35.            if(min>a[i][j])
36.            {
37.                min=a[i][j];
38.                row1=i;
39.                column1=j;
40.            }
41.    cout<<"\n该数组中最小的元素值为:"<<"a["<<row1<<"]["<<column1<<"]="<<a[row1][column1]<<endl;
42.
43.
44.    return 0;
45.}


例4.8

01.#include <iostream>
02.using namespace std;
03.
04.int main()
05.{
06.    char str[50];
07.    cout<<"Please input strings:";
08.    cin.get(str,50);
09.    cout<<"The string is:";
10.    cout<<str<<endl;
11.
12.    return 0;
13.}


例4.9

01.#include <iostream>
02.#include <cstring>
03.using namespace std;
04.
05.int main()
06.{
07.    char str[100];
08.    cout<<"请输入一个字符串:";
09.    cin.get(str,100);
10.    for(int j=0;j<100;j++)
11.        if(str[j]==0)
12.            cout<<"一共有"<<j<<"个字符!"<<endl;
13.    cout<<"字符串"<<str<<"的反向字符串为:";
14.    for(int i=strlen(str)-1;i>=0;i--)
15.        cout<<str[i];
16.    cout<<endl;
17.
18.
19.    return 0;
20.}
01.例4.10 #include <iostream>
02.using namespace std;
03.
04.int main()
05.{
06.    char s[]="This is Cprogramming test.";
07.    int i=0,plen=0,maxlen=0,pseat=0;
08.    while(s[i]!='\0')
09.    {
10.        while(s[i]!=' '&&s[i]!='\0')
11.        {
12.            plen++;
13.            i++;
14.        }
15.        if(plen>maxlen)
16.        {
17.            pseat=i-plen;
18.            maxlen=plen;
19.        }
20.        while(s[i]==' ')
21.            i++;
22.        plen=0;
23.    }
24.    cout<<"最长的单词为:";
25.    for(i=0;i<maxlen;i++)
26.        cout<<s[pseat+i];
27.    cout<<endl;
28.
29.
30.
31.    return 0;
32.}
01.例4.11 #include <iostream>
02.#include <cstring>
03.using namespace std;
04.
05.int main()
06.{
07.    char str[50];
08.    cout<<"请输入一组字符串";
09.    cin
4000
.get(str,50);
10.    cout<<"该字符串"<<str<<"的长度为:"<<strlen(str)<<endl;
11.
12.
13.    return 0;
14.}


例4.12

01.#include <iostream>
02.#include <cstring>
03.using namespace std;
04.
05.int main()
06.{
07.    char str[10];
08.    cout<<"请输入字符串,直到输入hellow后程序结束";
09.    do
10.    {
11.        cin>>str;
12.    }while(strcmp(str,"hellow")!=0);
13.
14.    return 0;
15.}


例4.13

01.#include <iostream>
02.using namespace std;
03.
04.int main()
05.{
06.    char str[50];
07.    int len=0;
08.    cout<<"请输入一个字符串:";
09.    cin.get(str,50);
10.    while(str[len]!=0)
11.        len++;
12.    cout<<"字符串"<<str<<"的长度为:"<<len<<endl;
13.
14.    return 0;
15.}


 

 

3. 编程定义一个4X5矩阵和5X3矩阵,并计算它们的积。

#include<iostream>
using namespace std;
int main()
{
int a[4][5],b[5][3],c[4][3];
int i,j,k;
cout<<"请输入矩阵a[4][5]"<<endl;
for(i=0;i<4;i++)
for(j=0;j<5;j++)
cin>>a[i][j];
cout<<"请输入矩阵b[5][3]"<<endl;
for(i=0;i<5;i++)
for(j=0;j<3;j++)
cin>>b[i][j];

for(i=0;i<4;i++)
for(j=0;j<3;j++)
c[i][j]=0;

for(i=0;i<4;i++)
for(k=0;k<3;k++)
for(j=0;j<5;j++)
c[i][k]+=a[i][j]*b[j][k];
cout<<"ab的积组成的新矩阵为:"<<endl;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}

4.编程计算S
=1!+21+3!+...n!。要求定义两个函数,一个计算n!,一个计算s
,在后一个函数中调用前一个函数。然后在主程序中输入数n的值,然后调用定义函数输出结果。

#include "stdafx.h"
#include<iostream>
using namespace std;
int s(int n)
{int i,s=1;
for(i=1;i<=n;i++)
s=s*i;
return s;}
int fun(int n)
{int i=1,sn=0;
for(i=1;i<=n;i++)
sn=sn+s(i);
return sn;}

int main()
{int n;
cout<<"计算S
=1!+2!+3!+...n!;请输入n:";
cin>>n;
cout<<n<<"!="<<s(n)<<endl
<<"S
=1!+2!+3!+..."<<n<<"!="<<fun(n)<<endl;

return 0;
}
5.编写一个函数,输入一个十六进制数,输出相应的十进制数。
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{ int i;
cout<<"请输入一个十六进制数:";
cin>>hex>>i;
cout<<"该十六进制数对应的十进制数为:"<<i<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: