您的位置:首页 > 其它

第六周作业 例题

2014-06-16 22:16 309 查看
例题1:

1:

[cpp] view
plaincopy





#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++) //输出所有的数组元素,没行显示5个数组元素

{

cout<<a[i]<<'\t';

if((i+1)%5==0)

cout<<endl;

}

return 0;

}

2:

[cpp] view
plaincopy





#include<iostream>

using namespace std;

int main()

{

int a[10];

int i;

for(i=0;i<10;i++)

{

a[i]=i*2+2;

cout<<a[i]<<'\t';

if((i+1)%5==0)

cout<<endl;

}

return 0;

}

//一个FOR循环就能实现了。

例题2:

[cpp] view
plaincopy





#include<iostream>

using namespace std;

int main()

{

int i,math[40],n;

float aver=0.0; //平均分

int unpassedcout = 0; // 不及格学生人数

int highscorecout =0; // 90分以上学生人数

cout<<"请输入学生人数:";

cin>>n;

cout<<"请输入学生成绩:";

for(i=0;i<n;i++)

{

cin>>math[i];

aver+=math[i];

}

aver/=n;

for(i=0;i<n;i++)

{

if(math[i]<60) unpassedcout++;

if(math[i]>90) highscorecout++;

}

cout<<"平均分为:"<<aver<<endl;

cout<<"90分以上人数为:"<<highscorecout<<endl;

cout<<"不及格人数为:"<<unpassedcout<<endl;

return 0;

}

例题3:

[cpp] view
plaincopy





#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<<"the numbers are:";

for(i=0;i<10;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;

}

例题3:

[cpp] view
plaincopy





#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int a[10];

int i,j,t;

cout<<"please input 10 numbers:\n";

for(i=0;i<10;i++)

cin>>a[i]; //输入数组元素

cout<<"the numbers 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-i;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;

}

例题4:

[cpp] view
plaincopy





#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int i;

int f[40]={1,1};

for(i=2;i<40;i++)

f[i]=f[i-2]+f[i-1];

for(i=0;i<40;i++)

{

if(i%4==0)

cout<<endl;

cout<<setw(12)<<f[i];

}

cout<<endl;

return 0;

}

例题5:

[cpp] view
plaincopy





#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int i,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:

[cpp] view
plaincopy





#include<iostream>

using namespace std;

int main()

{

char str[50];

cout<<"Please input strings:";

cin.get(str,50); //默认结束符是Enter

cout<<"The strings is:";

cout<<str<<endl;

return 0;

}

例题7:

[cpp] view
plaincopy





#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int a[2][3];

int i,j,big;

cout<<"请输入二行三列二维数组的元素值:"<<endl;

for(i=0;i<2;i++)

for(j=0;j<3;j++)

{

cout<<"a["<<i<<"]"<<"["<<j<<"]=";

cin>>a[i][j];

}

cout<<"该二维数组为:";

for(i=0;i<2;i++)

for(j=0;j<3;j++)

{

if(j%3==0)

cout<<endl;

cout<<setw(4)<<a[i][j];

}

cout<<endl;

big=a[0][0];

for(i=0;i<2;i++)

for(j=0;j<3;j++)

if(a[i][j]>=big)

big=a[i][j];

for(i=0;i<2;i++)

for(j=0;j<3;j++)

if(a[i][j]==big)

cout<<"该数组中最大元素为:"<<"a["<<i<<"]"<<"["<<j<<"]="<<a[i][j];

}

例题8:

[cpp] view
plaincopy





#include<iostream>

#include<iomanip>

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;

}

例题9:

[cpp] view
plaincopy





#include<iostream>

#include<cstring>

using namespace std;

int main()

{

char str[100];

cout<<"请输入一个字符串:";

cin.get(str,100);

cout<<"字符串"<<str<<"的反向字符串为:";

for(int i=strlen(str)-1;i>=0;i--)

cout<<str[i];

cout<<endl;

return 0;

}

例题10:

[cpp] view
plaincopy





#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;

}

例题11:

[cpp] view
plaincopy





#include<iostream>

#include<iomanip>

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;

}

例题12:

[cpp] view
plaincopy





#include<iostream>

#include<cstring>

using namespace std;

int main()

{

char str[10];

cout<<"请输入字符串,直到输入 hello 后程序结束:"<<endl;

do{

cin>>str;

}while(strcmp(str,"hello")!=0);

return 0;

}

例题13:

[cpp] view
plaincopy





#include<iostream>

using namespace std;

int main()

{

char str[50];

int len=0;

cout<<"请输入一个字符串:";

cin.get(str,50);

while(str[len] !='\0')

len++;

cout<<"字符串"<<str<<"的长度"<<len<<endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: