3-15
#include <iostream>
using namespace std;
int main()
{
int m,n,p,r,temp;
cout<<"Please enter m:";cin>>m;
cout<<endl<<"Please enter n:";cin>>n;
p=m*n;
if(m<n){
temp=m;m=n;n=temp;
}
while(n!=0){
r=m%n;m=n;n=r;
}
cout<<"最小公倍数为:"<<p/m<<endl<<"最大公约数为:"<<m;
return 0;
}
3-16
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
char c;int letters=0,space=0,digital=0,others=0;
cout<<"Please enter :";
while((c=getchar())!='\n'){
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digital++;
else
others++;
}
cout<<"letters="<<letters<<endl<<
"space="<<space<<endl<<
"digital="<<digital<<endl<<
"others="<<others<<endl;
return 0;
}
3-17
#include <iostream>
using namespace std;
int main()
{
int sum=0,n,i,a,temp=0;
cout<<"Please enter a,n :";
cin>>a>>n;
for(i=1;i<=n;i++){
temp=temp+a;
sum+=temp;
a=a*10;
}
cout<<"The result is "<<sum<<endl;
return 0;
}
3-18
#include <iostream>
using namespace std;
int main()
{
int sum=0,n,i,temp=1;
cout<<"Please enter n :";
cin>>n;
for(i=1;i<=n;i++){
temp=temp*i;
sum+=temp;
}
cout<<"The result is "<<sum<<endl;
return 0;
}
3-19
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int n,ge,shi,bai;
cout<<"Please enter n :";cin>>n;
bai=n/100;
shi=n%100/10;
ge=n%100%10;
if(n==bai*bai*bai+shi*shi*shi+ge*ge*ge){
cout<<"Yes!"<<endl;
}else{
cout<<"No!"<<endl;
}
return 0;
}
3-20
#include <iostream>
#define N 1000
using namespace std;
int main()
{
int sum,a,i;
for(a=1;a<=N;a++){
sum=0;
for(i=1;i<=a/2;i++){
if(a%i==0)
sum+=i;
}
if(sum==a){
cout<<a<<",its factors are:";
for(i=1;i<=a/2;i++){
if(a%i==0)
cout<<i<<",";
}
cout<<endl;
}
}
return 0;
}
3-21
#include <iostream>
#define N 20
using namespace std;
int main()
{
int i,t;
double a=2,b=1,sum=0;
for(i=1;i<=N;i++){
sum+=a/b;
t=a;a=a+b;b=t;
}
cout<<"Sum="<<sum<<endl;
return 0;
}
3-22
#include <iostream>
using namespace std;
int main()
{
int a2,a1=1;
for(int i=1;i<=9;i++){
a2=(a1+1)*2;
a1=a2;
}
cout<<"Sum="<<a2<<endl;
return 0;
}
3-23
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float a,x0,x1;
cout<<"Please enter a positive number:";
cin>>a;
x1=a;
do{
x0=x1;
x1=(x0+(a/x0))/2;
}while(fabs(x0-x1)>=1e-5);
cout<<x1<<endl;
return 0;
}
3-24
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
cout<<"Please enter a positive number:";
cin>>n;
for(i=1;i<=n;i=i+2){
for(j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
for(i=n-2;i>0;i=i-2){
for(j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
return 0;
}
3-25
#include <iostream>
using namespace std;
int main()
{
char i,j,k;//a的对手i,b的对手j,c的对手k。
for(i='X';i<='Z';i++){
for(j='X';j<'Z';j++){
if(i!=j){
for(k='X';k<'Z';k++)
if(i!=k&&j!=k)
if(i!='X'&&k=='Y')
cout<<"A--"<<i<<" B--"<<j<<" C--"<<k<<endl;
}
}
}
return 0;
}
4-1
#include <iostream>
using namespace std;
int Great_Com_Div(int m,int n);
int Least_Com_Mul(int m,int n,int r);
int main()
{
int m,n;
cout<<"Please enter m and n:" ;
cin>>m>>n;
cout<<"最大公约数为"<<Great_Com_Div(m,n)<<endl;
cout<<"最小公倍数为"<<Least_Com_Mul(m,n,Great_Com_Div(m,n))<<endl;
return 0;
}
//最大公约数
int Great_Com_Div(int m,int n){
int temp,r;
if(m<n){
temp=m;m=n;n=temp;
}
while((r=m%n)!=0){
m=n;
n=r;
}
return n;
}
//最小公倍数
int Least_Com_Mul(int m,int n,int r){
return m*n/r;
}
4-2
#include <iostream>
#include<cmath>
using namespace std;
void Greater_than_zero(float a,float b);
void Smaller_than_zero(float a,float b);
void Equal_to_zero(float a,float b);
float a,b,c;
float disc,x1,x2,m,n;
int main()
{
cout<<"Please enter a ,b,c:" ;
cin>>a>>b>>c;
disc=b*b-4*a*c;
if(disc>0){
Greater_than_zero( a, b);
cout<<"x1="<<x1<<",x2="<<x2<<endl;
}
else if(disc<0){
Smaller_than_zero( a, b);
cout<<"x1="<<m<<"+"<<n<<"i"<<endl;
cout<<"x2="<<m<<"-"<<n<<"i"<<endl;
}
else{
Equal_to_zero( a, b);
cout<<"x1=x2="<<x1<<endl;
}
return 0;
}
//大于0
void Greater_than_zero(float a,float b){
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
}
//小于0
void Smaller_than_zero(float a,float b){
m=-b/(2*a);
n=sqrt(-disc)/(2*a);
}
//等于0
void Equal_to_zero(float a,float b){
x1=x2=-b/(2*a);
}
4-3
#include <iostream>
#include<cmath>
using namespace std;
int Primer(int n);
int main()
{
int n;
cout<<"Please enter n:" ;
cin>>n;
if(Primer(n))
cout<<n<<" is a prime!"<<endl;
else
cout<<n<<" is not a prime!"<<endl;
return 0;
}
int Primer(int n){
int m=sqrt(n),i;
int flag=1;//素数标志,flag-1是素数,0不是
for(i=2;i<=m;i++){
if(n%i==0)
flag=0;;
}
return flag;
}
4-4
#include <iostream>
using namespace std;
int fac(int n);
int main()
{
int a,b,c,sum=0;
cout<<"Please enter a,b,c:" ;
cin>>a>>b>>c;
sum=fac(a)+fac(b)+fac(c);
cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<endl;
return 0;
}
int fac(int n){
if(n==1)
return 1;
return n*fac(n-1);
}
4-5
#include <iostream>
#include<cmath>
using namespace std;
double my_sinh(double x);
int main()
{
double x;
cout<<"Please enter x:" ;
cin>>x;
cout<<"sinh("<<x<<")="<<my_sinh(x)<<endl;
return 0;
}
double my_sinh(double x){
return (exp(x)-exp(-x))/2;
}
4-6
#include <iostream>
#include<cmath>
using namespace std;
double New_ite_met(double a,double b,double c,double d);
int main()
{
double a,b,c,d;
cout<<"Please enter a,b,c,d:" ;
cin>>a>>b>>c>>d;
cout<<"Result="<<New_ite_met(a,b,c,d)<<endl;
return 0;
}
double New_ite_met(double a,double b,double c,double d){
double x0=1,x1,f,f1;
do{
x0=x1;
f=a*x0*x0*x0+b*x0*x0+c*x0+d;
f1=3*a*x0*x0+2*b*x0+c;
x1=x0-f/f1;
}while(fabs(x0-x1)>=1e-5);
return x1;
}
4-7
#include <iostream>
#include<cmath>
using namespace std;
void gotbaha(int n);
int main()
{
int x;
cout<<"Please enter an even number not less than 6 :" ;
cin>>x;
gotbaha(x);
return 0;
}
int Prime(int x){
int flag=1,i,m=sqrt(x);
for(i=2;i<=m;i++){
if(x%i==0)
flag=0;
}
return flag;
}
void gotbaha(int n){
int a=n/2,i;
for(i=3;i<=a;i+=2){
if(Prime(i)){
if(Prime(n-i)){
cout<<n<<"="<<i<<"+"<<n-i<<endl;
}
}
}
}
4-8
#include <iostream>
#include<cmath>
using namespace std;
int P(int x,int n);
int main()
{
int x,n;
cout<<"Please enter x,n :" ;
cin>>x>>n;
cout<<"P"<<n<<"("<<x<<")="<<P(x,n);
return 0;
}
int P(int x,int n){
if(n==0)
return 1;
else if(n==1){
return x;
}
else if(n>=1){
return ((2*n-1)*x-P(x,n-1)-(n-1)*P(x,n-2))/2;
}
}
4-9
#include <iostream>
#include<cmath>
using namespace std;
void Hanoi(int n,char one ,char two,char three);
int main()
{
int n;
cout<<"Please enter n :" ;
cin>>n;
Hanoi(n,'A','B','C');
return 0;
}
void Move(char x,char y){
cout<<x<<"--"<<y<<endl;
}
void Hanoi(int n,char one ,char two,char three){
if(n==1){
Move(one,three);
}
else {
Hanoi(n-1,one,three,two);
Move(one,three);
Hanoi(n-1,two,one,three);
}
}
4-10
#include <iostream>
#include<cmath>
using namespace std;
void Convert(int m);
int main()
{
int n;
cout<<"Please enter n :" ;
cin>>n;
Convert(abs(n));
return 0;
}
void Convert(int m){
char c;
if((m/10)!=0){//说明较高位还有数
Convert(m/10);
}
c=m%10+'0';
cout<<c;
}
4-11
#include <iostream>
using namespace std;
int Sum_squ(int n);
int main()
{
int n,sum=0;
cout<<"Please enter n :" ;
cin>>n;
cout<<"Sum="<<Sum_squ( n)<<endl;
return 0;
}
int Sum_squ(int n){
int sum=0;
if(n==1)
return 1;
sum=n*n+Sum_squ(n-1);
return sum;
}
4-12
#include <iostream>
#include<cmath>
using namespace std;
#define S(a,b,c) (a+b+c)/2
#define Area(s,a,b,c) sqrt(s*(s-a)*(s-b)*(s-c))
int Sum_squ(int n);
int main()
{
int a,b,c,temp;
cout<<"Please enter the length of the three sides of a triangle :" ;
cin>>a>>b>>c;
if(a+b>c&&a+c>b&&b+c>a&&abs(a-b)<c||abs(a-c)<b||abs(b-c)<a){
cout<<Area(S(a,b,c),a,b,c);
}
return 0;
}
5-1
#include <iostream>
#include<iomanip>
using namespace std;
int Get_prime();
//素数判断数组p[N+1],素数的倍数不是素数
#define Max 100
int a[Max+1];
int main(){
int cnt= Get_prime();
for(int i=1;i<cnt;i++){
cout<<setw(5)<<a[i]<<" ";
if(i%5==0)
cout<<endl;
}
return 0;
}
int Get_prime()
{
int p[Max+1];//素数判断数组
int i,j,cnt=1;
for(i=0;i<=Max;i++)
p[i]=1;
p[0]=0;p[1]=0;//0,1 不是素数
for(i=2;i<=Max;i++){
if(p[i]==1){
a[cnt++]=i;
for(j=i;j<=Max;j=j+i){//素数的倍数不是素数
p[j]=0;
}
}
}
return cnt;
}
5-2
#include <iostream>
using namespace std;
void Select_sort(int a[],int n);
int main(){
int a[11],i,j;
cout<<"Please enter 10 numbers:";
for(i=0;i<10;i++)
cin>>a[i];
Select_sort(a,10);
cout<<"The sorted numbers:";
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void Select_sort(int a[],int n){
int i,j,k,temp;
for(i=0;i<n;i++){
k=i;
for(j=i+1;j<n;j++)
if(a[k]>a[j])
k=j;
temp=a[k];a[k]=a[i];a[i] =temp;
}
}
5-3
#include <iostream>
using namespace std;
int main(){
int a[3][3],sum=0,i,j;
cout<<"Please enter the numbers:";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(i=0;i<3;i++)
sum+=a[i][i];
cout<<"Sum="<<sum;
return 0;
}
5-4
#include <iostream>
using namespace std;
int main(){
int a[11],num,i,j,k;
cout<<"Please enter a sorted array:";
for(i=0;i<10;i++)
cin>>a[i];
cout<<"Please enter the value ypu want to insert:";
cin>>num;
if(a[9]<=num)
a[10]=num;
else{
for(i=0;i<10;i++){
if(a[i]<=num&&a[i+1]>=num){
for(j=10;j>i+1;j--)
a[j]=a[j-1];
a[i+1]=num;
break;
}
}
}
for(i=0;i<11;i++)
cout<<a[i]<<" ";
return 0;
}
//2 3 4 5 6 7 8 34 37 78
5-5
#include <iostream>
#define N 10
using namespace std;
int main(){
int a[N],i,temp;
cout<<"Please enter an array:";
for(i=0;i<N;i++)
cin>>a[i];
cout<<"The reversed result is:";
for(i=0;i<N/2;i++){
temp=a[i];
a[i]=a[N-1-i];
a[N-1-i]=temp;
}
for(i=0;i<N;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
//2 3 4 5 6 7 8 34 37 78
5-6
#include <iostream>
#include<iomanip>
#define N 10
using namespace std;
int main(){
int a[N][N],i,j;
for(i=0;i<N;i++){
a[i][0]=1;
a[i][i]=1;
}
for(i=2;i<N;i++){
for(j=1;j<i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i<N;i++){
for(j=0;j<=i;j++)
cout<<setw(5)<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
return 0;
}
5-7
#include <iostream>
#define M 10
#define N 10
using namespace std;
int main(){
int a[M][N],b[N];//鞍点数组
int i,j,k,s,m,n,max_m,min_m,cnt=0;
cout<<"Please enter the number of rows(m) and columns(n):";
cin>>m>>n;
for(i=0;i<m;i++){
for(j=0;j<n;j++)
cin>>a[i][j];
}
for(i=0;i<m;i++){//打印数组
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
for(i=0;i<m;i++){
max_m=a[i][0];
for(j=0;j<n;j++)//找第i行最大值
if(a[i][j]>max_m)
max_m=a[i][j];
s=0;
for(j=0;j<n;j++)//存入鞍点数组
if(a[i][j]==max_m){
b[s]=j;
s++;
}
min_m=max_m;
for(j=0;j<s;j++){
for(k=0;k<m;k++)
if(min_m>a[k][b[j]])
break;
if(k==m){
cout<<"鞍点为a[" << i<<"]["<<b[j] <<"]="<<a[i][b[j]]<<endl;
cnt++;
}
}
}
if(cnt==0)
cout<<"无鞍点。"<<endl;
return 0;
}
5-8
#include <iostream>
using namespace std;
#define N 15
int find(int m,int a[],int left,int right);
int main(){
int a[N],i,m;
cout<<"Please enter a sorted array:";
for(i=0;i<N;i++){
cin>>a[i];
}
cout<<"Please enter the number you want to find:";
cin>>m;
cout<<"该数是元素中的第"<<find(m,a,0,N-1)<<"个数。" <<endl;
return 0;
}
int find(int m,int a[],int left,int right){
int mid=(left+right)/2;
if(m<a[right]||m>a[left])
cout<<"无此数。"<<endl;
else if(m>a[mid]&&m<a[left])
find(m,a,left,mid-1);
else if(m<a[mid]&&m>a[right])
find(m,a,mid+1,right);
else if(m==a[mid])
return mid+1;
}
//15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
5-9
#include <iostream>
using namespace std;
int leap(int y);
int main(){
int y,m,d,i,j,sum=0;
int dt[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"Please enter year,month,day:";
cin>>y>>m>>d;
for(i=1;i<m;i++)
sum+=dt[i];
sum+=d;
if(!leap(y)&&m>2)
sum+=1;
cout<<"该日期为今年的第"<<sum<<"天。"<<endl;
return 0;
}
int leap(int y){
if((y%4==0&&y%100==0)||y%400==0)
return 1;
return 0;
}
5-10
//统计大小写字母数
#include <iostream>
#include<stdio.h>
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-11
字符数组方法
#include <iostream>
using namespace std;
int main(){
char a[5]={'*','*','*','*','*'};
for(int i=0;i<5;i++){
for(int j=0;j<=i;j++)
cout<<" ";
for(int k=0;k<5;k++)
cout<<a[k];
cout<<endl;
}
return 0;
}
String方法
#include <iostream>
#include<string>
using namespace std;
int main(){
string str="*****";
for(int i=0;i<5;i++){
for(int j=0;j<=i;j++)
cout<<" ";
cout<<str<<endl;
}
return 0;
}
5-12
#include <iostream>
#include<stdio.h>
#define N 10
using namespace std;
int main(){
char a[N];
int i=0;
cout<<"Please enter the password:";
gets(a);
while(a[i]!='\0'){
if(a[i]>='A'&&a[i]<='Z')
a[i]=155-a[i];
else if(a[i]>='a'&&a[i]<='z')
a[i]=219-a[i];
else
a[i]=a[i];
i++;
}
cout<<"The original is ";
for(i=0;i<N;i++)
cout<<a[i];
return 0;
}
5-13
字符数组
#include <iostream>
#include<cstring>
#include<stdio.h>
#define N 1000
using namespace std;
char* my_strcat(char str1[], char str2[]);
int main(){
char str1[N],str2[N];
while(gets(str1)&&gets(str2))
cout<<"The new string is : "<<my_strcat(str1,str2)<<endl;
return 0;
}
char* my_strcat(char str1[], char str2[]){
int l1=strlen(str1),l2=strlen(str2);
for(int i=0;i<l2;i++)
str1[l1+i]=str2[i];
str1[l1+l2]='\0';
return str1;
}
Strcat
#include <iostream>
#include<cstring>
#include<stdio.h>
#define N 1000
using namespace std;
int main(){
char str1[N],str2[N];
while(gets(str1)&&gets(str2))
cout<<"The new string is : "<<strcat(str1,str2)<<endl;
return 0;
}
String
#include <iostream>
#include<string>
using namespace std;
int main(){
string str1,str2;
cout<<"Please enter str1:";
cin>>str1;
cout<<"Please enter str2:";
cin>>str2;
cout<<"The new string is : "<<str1+str2<<endl;
return 0;
}
5-14
#include <iostream>
#include<string>
#define N 10
using namespace std;
int main(){
string str[N],temp;
int i,j,n;
cout<<"Please enter n:";
cin>>n;
for(i=0;i<n;i++)
cin>>str[i];
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(str[j]>str[j+1]){
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
cout<<"Sorted strings:"<<endl;
for(i=0;i<n;i++)
cout<<str[i]<<endl;
return 0;
}
5-15
#include <iostream>
#include<string>
#define N 10
using namespace std;
int main(){
string str[N],temp;
int i,j,n;
cout<<"Please enter n:";
cin>>n;
for(i=0;i<n;i++)
cin>>str[i];
cout<<"Strings starting with A are:"<<endl;
for(i=0;i<n;i++)
if(str[i][0]=='A')
cout<<str[i]<<endl;
return 0;
}
5-16
字符数组
#include <iostream>
#include<cstring>
using namespace std;
#define N 10
int main(){
char str[N];
int i,j;
cout<<"Please enter str:";
for(i=0;str[i]!='\0';i++)
cin>>str[i];
cout<<"The result of reverse order output is:";
for(i=strlen(str)-1;i>=0;i--)
cout<<str[i];
cout<<endl;
return 0;
}
String
#include <iostream>
#include<string>
using namespace std;
int main(){
string str;
int i,j;
cout<<"Please enter str:";
cin>>str;
cout<<"The result of reverse order output is:";
for(i=0;i<str.size();i++)
cout<<str[str.size()-1-i];
cout<<endl;
return 0;
}
5-17
#include <iostream>
using namespace std;
#define N 10
int main(){
string name[N],number[N];
int score[N];
int i,n;
cout<<"Please enter the total number(n):";cin>>n;
for(i=0;i<n;i++){
cout<<endl<<"Please enter information for the "<<i+1<<"th student:";
cin>>name[i]>>number[i]>>score[i];
}
cout<<"Failing students have:"<<endl;
for(i=0;i<n;i++)
if(score[i]<60)
cout<<name[i]<<" "<<number[i]<<" "<<score[i]<<endl;
return 0;
}