4.1
#include <iostream>
using namespace std;
int main()
{
int a[10],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;
}
4.2
#include <iostream>
using namespace std;
int main()
{
int i,math[100],n;
float aver=0.0;
int unpassedcount=0;
int highscorecount=0;
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)
unpassedcount++;
if(math[i]>=90)
highscorecount++;
}
cout<<"平均分:"<<aver<<endl;
cout<<"90分以上人数:"<<highscorecount<<endl;
cout<<"不及格人数:"<< unpassedcount<<endl;
return 0;
}
4.3
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[5],i,j,k,t;
cout<<"please input 5 numbers:"<<endl;
for(i=0;i<5;i++)
cin>>a[i];
cout<<"the number are :";
for(i=0;i<5;i++)
cout<<setw(4)<<a[i];
cout<<endl;
for(i=0;i<4;i++)
for(j=0;j<4-i;j++)
if(a[j]>a[j+1])
{t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
cout<<"the storted numbers are:";
for(i=0;i<5;i++)
cout<<setw(4)<<a[i];
cout<<endl;
return 0;
}
4.4
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[5],i,j,big;
cout<<"please input 5numbers :"<<endl;
for(i=0;i<5;i++)
cin>>a[i];
cout<<"the number are :";
for(i=0;i<5;i++)
cout<<setw(8)<<a[i];
cout<<endl;
big=a[0];
for(i=1;i<5;i++)
if(a[i]>big)
big=a[i];
cout<<"the big number is :"<<big<<endl;
return 0;
}
4.5
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,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;
}
4.6
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,j,a[5][5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i%==0)
a[i][j]=i*5+j+i;
else
a[i][j]=i*5+j+i;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout<<setw(4)<<a[i][j];
cout<<endl;
}
return 0;
}
4.7
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,j,a[2][3],big;
cout<<"please input the number"<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"the number are"<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
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];
}
cout<<setw(4)<<big<<setw (4)<<i<<setw(4)<<j<<endl;
return 0;
}
4.8
#include <iostream>
using namespace std;
int main()
{
char str[50];
cout<<"please input string:"<<endl;
cin.get(str,50);
cout<<"the string is :"<<endl;
cout<<str<<endl;
return 0;
}
4.9
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];
cout<<"请输入一个字符串:"<<endl;
cin.get(str,100);
cout<<"字符串为:"<<str<<endl;
cout<<"字符串"<<str<<"处理后的字符串为:"<<endl;
for(int i=strlen(str)-1;i>=0;i--)
cout<<str[i];
cout<<endl;
return 0;
}
4.10
#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;
}
4.12
#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;
}
4.13
#include <iostream>
using namespace std;
int main()
{
char str[51];
int len=0;
cout<<"请输入一个字符串:"<<endl;
cin.get(str,50);
while(str[len]!='\0')
len++;
cout<<"字符串"<<str<<"的长度:"<<len<<endl;
return 0;
}
习题1
#include <iostream>
using namespace std;
int main()
{
int a[5],i,j,t;
cout<<"please input the numbers:"<<endl;
for(i=0;i<5;i++)
cin>>a[i];
cout<<"the new numbers are:"<<endl;
for(i=0;i<4;i++)
for(j=0;j<4-i;j++)
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
习题3
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,count=0,a[40]={1,1};
cout<<"fibonacci数列为:"<<endl;
for(i=2;i<40;i++)
a[i]=a[i-2]+a[i-1];
for(i=0;i<40;i++)
{
if(i%4==0)
cout<<endl;
cout<<setw(12)<<a[i];
}
cout<<endl;
cout<<"第十六项为:";
cout<<a[15];
for(i=0;i<20;i++)
if(a[i]>=100)
count++;
cout<<"三位数的个数为:"<<count<<endl;
return 0;
}
习题4
#include <iostream>
#include <cstring>
using namespace std;int main(){
char str[100];
cout<<"请输入一个字符串:"<<endl;
cin.get(str,100);
cout<<"字符串"<<str<<"的长度为:"<<strlen(str)<<endl;
return 0;
}
习题5
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];int len;
cout<<"请输入一个字符串:"<<endl;
cin.get(str,100);
len=strlen(str);
cout<<"字符串的长度为:"<<len<<endl;
cout<<"方向输出后的字符串:"<<endl;
for(len;len>=0;len--)
cout<<str[len];
cout<<endl;
return 0;
}
习题6
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100],a[50],b[50];
int i,j=0,k=0;
for(i=0;i<100;i++)
{
while(str[i]!='\0')
{
if(str[i]>='\0'&&str[i]<='\9')
{a[j]=str[i];
j++;}
else
b[k]=str[i];
k++;
}
}
cout<<"处理后的字符串:"<<endl;
for(i=0;i<k;i++)
cout<<b[i];
cout<<"字符串的长度:"<<endl;
cout<<"len="<<strlen(b)<<endl;
return 0;
}
5.6
#include <iostream>
using namespace std;
int max(int u,int v)
{
int w;
w=u>v?u:v;
return w;
}
int main()
{
int a,b,c;
cout<<"please input two numbers;"<<endl;
cin>>a>>b;
c=max(a,b);
cout<<"max is "<<c<<endl;
return 0;
}
5.7
#include <iostream>
using namespace std;
void swap(int u,int v);
int main()
{
int a=4,b=4;
cout<<"a="<<a<<"\tb="<<b<<endl;
swap(a,b);
cout<<"a="<<a<<"\tb="<<b<<endl;
}
void swap(int u,int v)
{
int temp;
temp=u;
u=v;
v=temp;
}
5.9
#include <iostream>
using namespace std;
void swap(int u,int v);
int main()
{
int a=3,b=4;
cout<<"a="<<a<<"\tb="<<b<<endl;
swap(a,b);
cout<<"a="<<a<<"\tb="<<b<<endl;
return 0;
}
void swap(int &u,int &v)
{
int temp;
temp=u;
u=v;
v=temp;
}