谭浩强C++

一笑奈何 提交于 2020-02-10 17:58:00

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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!