5.1筛选法求100以内素数
#include <iostream>
#include <iomanip> //set系列控制符
using namespace std;
#include <math.h>
int main()
{
int i,j,n,a[101];
for (i=1;i<=100;i++)
a[i]=i;
a[1]=0;
for (i=2;i<sqrt(100);i++) //非素数置零
for (j=i+1;j<=100;j++)
{if(a[i]!=0 && a[j]!=0)
if (a[j]%a[i]==0)
a[j]=0; }
cout<<endl;
for (i=1,n=0;i<=100;i++) //非零值输出
{if (a[i]!=0)
{cout<<setw(5)<<a[i]<<" ";
n++;}
if(n==10) //每十个数换行
{cout<<endl;
n=0;}
}
cout<<endl;
return 0;
}
5.2选择法对10个整数排序
#include <iostream>
using namespace std;
//#include <math.h>
int main()
{int i,j,min,temp,a[11];
cout<<"enter data:"<<endl;
for (i=1;i<=10;i++)
{cout<<"a["<<i<<"]=";
cin>>a[i]; //输入10个数
}
cout<<endl<<"The original numbers:"<<endl;
for (i=1;i<=10;i++)
cout<<a[i]<<" "; // 输出这10个数
cout<<endl;
/*对10个数从小到大排序*/
for (i=1;i<=9;i++)
{
min=i;
for (j=i+1;j<=10;j++)
if (a[min]>a[j]) min=j;
//将a[i]~a[10]中最小者与a[i] 对换
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
// 输出已排好序的10个数
cout<<endl<<"The sorted numbers:"<<endl;
for (i=1;i<=10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
5.3求一个3*3矩阵对角线元素之和
#include <iostream>
using namespace std;
int main()
{int a[3][3],sum=0;
int i,j;
cout<<"enter data:"<<endl;;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>a[i][j];
for (i=0;i<3;i++)
sum=sum+a[i][i];
cout<<"sum="<<sum<<endl;
return 0;
}
5.4有一个已排好的数组,今输入一个数,按原来的排序插入数组中。
#include <iostream>
using namespace std;
int main()
{
int a[11]={1,4,6,9,13,16,19,28,40,100};
int num,i,j;
cout<<"array a:"<<endl;
for (i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"insert data:";
cin>>num;
if (num>a[9])
a[10]=num;
else
{for (i=0;i<10;i++)
{if (a[i]>num)
{
for (j=9;j>=i;j--)
a[j+1]=a[j];
a[i]=num;
break;
}
}
}
cout<<"Now, array a:"<<endl;
for (i=0;i<11;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
5.5将一个数组中的值按逆序重新存放
#include <iostream>
using namespace std;
int main()
{ const int n=5;
int a[n],i,temp;
cout<<"enter array a:"<<endl;
for (i=0;i<n;i++)
cin>>a[i];
cout<<"array :"<<endl;
for (i=0;i<n;i++)
cout<<a[i]<<" ";
for (i=0;i<n/2;i++) //循环的作用是将对称的元素的值互换
{ temp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=temp;
}
cout<<endl<<"Now array :"<<endl;
for (i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
5.6杨辉三角
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{const int n=11;
int i,j,a[n][n];
for (i=1;i<n;i++)
{a[i][i]=1;
a[i][1]=1;
}
for (i=3;i<n;i++)
for (j=2;j<=i-1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for (i=1;i<n;i++)
{for (j=1;j<=i;j++)
cout<<setw(5)<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
return 0;
}
5.7找出一个二维数组中的鞍点,即元素该行最大,该列最小(可能没有)
#include <iostream>
//#include <iomanip>
using namespace std;
int main()
{
const int n=4,m=5; //假设数组四行五列
int i,j,a[n][m],max,maxj;
bool flag;
for (i=0;i<n;i++)
for (j=0;j<m;j++)
cin>>a[i][j];
for (i=0;i<n;i++)
{
//找出第i行最大的数
max=a[i][0];maxj=0;
for (j=0;j<m;j++)
if(a[i][j]>max)
{max=a[i][j];maxj=j;}
//假设是鞍点
flag=true;
//行最大数与列元素比,max不是最小,则不是鞍点
for (int k=0;k<n;k++)
if (max>a[k][maxj])
{flag=false;continue;}
if(flag)
{
cout<<"a["<<i<<"]["<<maxj<<"]="<<max<<endl;
break;
}
}
//如果flag为假则鞍点不存在
if(!flag)
cout<<"It does not exist!"<<endl;
return 0;
}
5.8有15个数按由大到小的顺序存放在一个数组中,输入一个数,折半查找法找出该数是第几个元素。
如果数不存在则打印无此数
#include <iostream>
using namespace std;
int main()
{ const int n=15;
int i,number,top,bott,mid,loca,a[n];
bool flag=true,sign;
char c;
//输入15个数按顺序 由大到小
cout<<"enter data:"<<endl;
cin>>a[0];
i=1;
while(i<n)
{cin>>a[i];
if (a[i]<=a[i-1])
i++;
else
cout<<"enter this data again:";
}
cout<<endl;
//输出数
for (i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
//给这个程序一个可执行的循环 无限次执行
while(flag)
{
cout<<"input number to look for:";
cin>>number;
sign=false;
top=0; //top是查找区间的起始位置
bott=n-1; //bott是查找区间的最末位置
if ((number<a[n-1])||(number>a[0])) //要查的数不在查找区间内
loca=-1; // 不存在
else
{
while ((!sign) && (top<=bott))
{mid=(bott+top)/2;
if (number==a[mid])
{
loca=mid;
cout<<"Find "<<number<<", its position is "<<loca+1<<endl;
sign=true;
}
else if (number<a[mid])
top=mid+1;
else
bott=mid-1;
}
}
//没找到
if(!sign||loca==-1)
cout<<number<<" has not found."<<endl;
//循环
cout<<"continu or not(Y/N)?";
cin>>c;
if (c=='N'||c=='n')
flag=false;
}
return 0;
}
5.9有一篇文章,共有三行文字,每行80字符。统计英文大写字母,小写字母,数字,空格,其他的个数
#include <iostream>
using namespace std;
int main()
{int i,j,upper,lower,digit,space,other;
char text[3][80];
upper=lower=digit=space=other=0;
for (i=0;i<3;i++)
{cout<<"please input line "<<i+1<<endl;
gets(text[i]);
for (j=0;j<80 && text[i][j]!='\0';j++)
{if (text[i][j]>='A'&& text[i][j]<='Z')
upper++;
else if (text[i][j]>='a' && text[i][j]<='z')
lower++;
else if (text[i][j]>='0' && text[i][j]<='9')
digit++;
else if (text[i][j]==' ')
space++;
else
other++;
}
}
cout<<"upper case:"<<upper<<endl;
cout<<"lower case:"<<lower<<endl;
cout<<"digit :"<<digit<<endl;
cout<<"space :"<<space<<endl;
cout<<"other :"<<other<<endl;
return 0;
}
5.10打印以下图案字符数组方法,string方法
#include <iostream>
using namespace std;
int main()
{ char a[5]={'*','*','*','*','*'};
int i,j,k;
char space=' ';
for (i=0;i<5;i++) // 输出5行
{ cout<<endl; // 输出每行前先换行
cout<<" "; // 每行前面留4个空格
for (j=1;j<=i;j++)
cout<<space; // 每行再留一个空格
for (k=0;k<5;k++)
cout<<a[k]; // 每行输出5个*号
}
cout<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{ string stars="*****";
int i,j;
for (i=0;i<5;i++) // 输出5行
{ cout<<" "; // 每行前面留4个空格
for (j=1;j<=i;j++)
cout<<" "; // 每行再插入i个空格
cout<<stars<<endl; // 输出5个*号
}
return 0;
}
5.11电文 。第一个字母变成第二十六个字母。
编程将密码译回原文
#include <iostream>
using namespace std;
int main()
{int j,n;
char ch[80],tran[80];
cout<<"input cipher code:";
gets(ch);
cout<<"cipher code:"<<ch<<endl;
j=0;
while (ch[j]!='\0')
{ if ((ch[j]>='A') && (ch[j]<='Z'))
tran[j]=155-ch[j];
else if ((ch[j]>='a') && (ch[j]<='z'))
tran[j]=219-ch[j];
else
tran[j]=ch[j];
j++;
}
n=j;
cout<<"original text:";
for (j=0;j<n;j++)
putchar(tran[j]);
cout<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{int j;
string ch="I will visit China next week.",tran;
tran=ch;
cout<<"cipher code:"<<ch<<endl;
j=0;
while (j<=ch.size())
{ if ((ch[j]>='A') && (ch[j]<='Z'))
tran[j]=155-ch[j];
else if ((ch[j]>='a') && (ch[j]<='z'))
tran[j]=219-ch[j];
else
tran[j]=ch[j];
j++;
}
cout<<"original text:";
cout<<tran<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{int j;
string ch=" ";
char *p=&ch[0]; //定义字符指针并使之指向ch的首字符
cout<<"input cipher code:";
gets(p); //从键盘读入一行字符
cout<<"cipher code:"<<ch<<endl;
j=0;
while (j<=ch.size())
{ if ((ch[j]>='A') && (ch[j]<='Z'))
ch[j]=155-ch[j];
else if ((ch[j]>='a') && (ch[j]<='z'))
ch[j]=219-ch[j];
j++;
}
cout<<"original text:";
cout<<ch<<endl;
return 0;
}
5.12将两个字符串连接起来,取代第一个字符串
5.12.1 用字符数组,不用strcat
#include <iostream>
#include <string>
using namespace std;
int main()
{char s1[80],s2[40];
int i=0,j=0;
cout<<"input string1:";
cin>>s1;
cout<<"input string2:";
cin>>s2;
while (s1[i]!='\0')
i++;
while(s2[j]!='\0')
s1[i++]=s2[j++];
s1[i]='\0';
cout<<"The new string is:"<<s1<<endl;
return 0;
}
5.12.2用strcat函数
#include <iostream>
#include <string.h>
using namespace std;
int main()
{char s1[80],s2[40];
cout<<"input string1:";
cin>>s1;
cout<<"input string2:";
cin>>s2;
strcat(s1,s2);
cout<<"The new string is:"<<s1<<endl;
return 0;
}
5.12.3用string方法定义字符串变量
#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1="week",s2="end";
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
s1=s1+s2;
cout<<"The new string is:"<<s1<<endl;
return 0;
}
5.13输入n个字符串,按字母由小到大排列输出
#include <iostream>
#include <string>
using namespace std;
int main()
{ const int n=5;
int i,j;
string str[n],temp;
cout<<"please input strings:"<<endl;
for(i=0;i<n;i++)
cin>>str[i];
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(str[j]>str[j+1])
{temp=str[j];str[j]=str[j+1];str[j+1]=temp;}
cout<<endl<<"sorted strings:"<<endl;
for(i=0;i<n;i++)
cout<<str[i]<<endl;
return 0;
}
5.14输入n个字符串,把其中以A打头的字符串取出
#include<iostream>
#include<string>
using namespace std;
int main()
{
const int n=5; //设5个字符串
string str[100];
int i;
cout<<"please input code"<<endl;
for(i=0;i<n;i++)
cin>>str[i]; //给str赋值
for(i=0;i<n;i++)
if(str[i][0]=='A') //一维字符串数组 可以这样调用其中的字符
cout<<"结果:"<<str[i]<<endl;
return 0;
}
5.15输入一个字符串,把其中字符按逆序输出。
5.15.1字符数组
#include <iostream>
using namespace std;
int main()
{ const int n=10; //设一共10个字符
int i;
char a[n],temp;
cout<<"please input a string:";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n/2;i++)
{temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}
for(i=0;i<n;i++)
cout<<a[i];
cout<<endl;
return 0;
}
5.15.2用string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
int i,n;
char temp;
cout<<"please input a string:";
cin>>a;
n=a.size();
for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=temp;
}
cout<<a<<endl;
return 0;
}
5.16输入10个学生的姓名、学号和成绩,将其中不及格者的姓名、学号和成绩输出
#include<iostream>
#include<string>
using namespace std;
const int n=10;
string name[n];
int num[n],score[n];
int main()
{
int i;
for(i=0;i<n;i++)
{
cout<<"input name,number and score of student "<<i+1<<" : " <<endl;
cin>>name[i]>>num[i]>>score[i];
}
cout<<endl<<"The list of failed:"<<endl;
for(i=0;i<n;i++)
if(score[i]<60)
cout<<name[i]<<" "<<num[i]<<" "<<score[i]<<endl;
return 0;
}
阿这 错的地方我改过了
这答案应该都是对的