# C++上机考试试题解析

## 0x1 求日期是该年的第几天

Input year, month, day:1981 3 1

#include<iostream> using namespace std; int main() { cout<<"Input year, month, day:"; int a,b,c,temp=0; int days,i; cin>>a; cin>>b; cin>>c; int month[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; if(a%4==0&&a%100!=0||a%400==0) month[1]=29; for(i=0; i<b-1; i++) temp+=month[i]; days=temp+c; cout<<"Days of year:"<<days<<endl; return 0; }

1234567891011121314151617181920

#include<iostream>using namespace std;int main(){cout<<"Input year, month, day:";    int a,b,c,temp=0;    int days,i;    cin>>a;    cin>>b;    cin>>c;    int month[12]= {31,28,31,30,31,30,31,31,30,31,30,31};    if(a%4==0&&a%100!=0||a%400==0)        month[1]=29;    for(i=0; i<b-1; i++)        temp+=month[i];    days=temp+c;cout<<"Days of year:"<<days<<endl;    return 0;}

## 0x2 判断输入字符个数

#include <iostream> #include <cstdio> using namespace std; int main() { int uppercase=0; int lowercase=0; int digits=0; int spacebars=0; int other=0; int i=0; char *p; char s[100]; cout<<"Input a string:"; while((s[i]=getchar())!='\n') { i++; } p=&s[0]; while(*p!='\n') { if(('A'<=*p)&&(*p<='Z')) { ++uppercase; } else if(('a'<=*p)&&(*p<='z')) { ++lowercase; } else if(*p==' ') { ++spacebars; } else if((*p<='9')&&(*p>='0')) { ++digits; } else { ++other; } p++; } cout<<"Number of uppercase letters is :"<<uppercase<<endl; cout<<"Number of lowercase letters is :"<<lowercase<<endl; cout<<"Number of spacebars is :"<<spacebars<<endl; cout<<"Number of digits is :"<<digits<<endl; cout<<"Number of other characters is :"<<other<<endl; return 0; }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354

#include <iostream>#include <cstdio> using namespace std; int main(){    int uppercase=0;    int lowercase=0;    int digits=0;    int spacebars=0;    int other=0;    int i=0;    char *p;    char s[100];    cout<<"Input a string:";    while((s[i]=getchar())!='\n')    {        i++;    }    p=&s[0];    while(*p!='\n')    {        if(('A'<=*p)&&(*p<='Z'))        {            ++uppercase;        }        else if(('a'<=*p)&&(*p<='z'))        {            ++lowercase;        }        else if(*p==' ')        {            ++spacebars;        }        else if((*p<='9')&&(*p>='0'))        {            ++digits;        }        else        {            ++other;        }        p++;    }    cout<<"Number of uppercase letters is :"<<uppercase<<endl;    cout<<"Number of lowercase letters is :"<<lowercase<<endl;    cout<<"Number of spacebars is :"<<spacebars<<endl;    cout<<"Number of digits is :"<<digits<<endl;    cout<<"Number of other characters is :"<<other<<endl;    return 0;}

## 0x3 顺序循环移动

#include<stdio.h> void mov(int *,int,int); int main(void) { int m,n,i,a[80],*p; printf("Input n,m:"); scanf("%d%d",&n,&m); for(p =a,i =0; i <n; i ++) scanf("%d",p++); mov(a,n,m); printf("After move:"); for(i =0; i <n; i ++) printf("%5d",a[i]); return 0; } void mov(int*x,int n,int m) { int i,j,k; for(i =0; i <m; i ++) { k=x[n-1]; for(j =n -1; j >0; j --) x[j] =x[j -1]; x[0] =k; } }

123456789101112131415161718192021222324252627

#include<stdio.h>void mov(int *,int,int);int main(void){    int m,n,i,a[80],*p;    printf("Input n,m:");    scanf("%d%d",&n,&m);    for(p =a,i =0; i <n; i ++)        scanf("%d",p++);    mov(a,n,m);    printf("After move:");    for(i =0; i <n; i ++)        printf("%5d",a[i]);    return 0;}void mov(int*x,int n,int m){    int i,j,k;    for(i =0; i <m; i ++)    {        k=x[n-1];        for(j =n -1; j >0; j --)            x[j] =x[j -1];        x[0] =k;    }}

## 0x4 换算一笔钱

#include <iostream> using namespace std; int main() { int n; int x,y,z;//x是1分，y是2分，z是5分(硬币个数) int num=0;//用于保存有多少种方法 cout<<"Input money: "; cin>>n; for(x=1;x<=n;x++) for(y=1;y<=n/2;y++) for(z=1;z<=n/5;z++) { if(x+2*y+5*z==n) { cout<<"1分"<<x<<"个，2分"<<y<<"个，5分"<<z<<"个n"; num++; } } cout<<"一共有"<<num<<"种方法"<<endl; }

123456789101112131415161718192021222324

#include <iostream> using namespace std; int main(){    int n;    int x,y,z;//x是1分，y是2分，z是5分(硬币个数)    int num=0;//用于保存有多少种方法    cout<<"Input money: ";    cin>>n;    for(x=1;x<=n;x++)    for(y=1;y<=n/2;y++)    for(z=1;z<=n/5;z++)    {        if(x+2*y+5*z==n)        {            cout<<"1分"<<x<<"个，2分"<<y<<"个，5分"<<z<<"个n";            num++;        }    }   cout<<"一共有"<<num<<"种方法"<<endl;}

## 0x5 加密一个四位正整数

#include <iostream> using namespace std; int main() { int m; int a,b,c,d; cout<<"Enter a number:"; cin>>m; a=(m/1000+9)%10; b=(m%1000/100+9)%10; c=(m%100/10+9)%10; d=(m%10+9)%10; cout<<"The encrypted number is "<<c<<d<<a<<b<<endl; return 0; }

12345678910111213141516

#include <iostream>using namespace std;int main(){    int m;    int a,b,c,d;    cout<<"Enter a number:";    cin>>m;    a=(m/1000+9)%10;    b=(m%1000/100+9)%10;    c=(m%100/10+9)%10;    d=(m%10+9)%10;    cout<<"The encrypted number is "<<c<<d<<a<<b<<endl;    return 0;}

## 0x6 同构数

#include <iostream> using namespace std; int main() { int a; cin>>a; int b=a*a-a; int c=a,d=b; while((c=c/10)!=0) { if(d%10!=0) { cout<<a<<" no,"<<a<<"*"<<a<<"="<<a*a; return 0; } d=d/10; } if(d%10!=0) cout<<a<<" no,"<<a<<"*"<<a<<"="<<a*a; else cout<<a<<" yes,"<<a<<"*"<<a<<"="<<a*a; return 0; }

12345678910111213141516171819202122232425

#include <iostream>using namespace std;int main(){    int a;    cin>>a;    int b=a*a-a;    int c=a,d=b;    while((c=c/10)!=0)    {        if(d%10!=0)        {            cout<<a<<" no,"<<a<<"*"<<a<<"="<<a*a;            return 0;        }        d=d/10;    }    if(d%10!=0)        cout<<a<<" no,"<<a<<"*"<<a<<"="<<a*a;    else         cout<<a<<" yes,"<<a<<"*"<<a<<"="<<a*a;     return 0;}

## 0x7 奇数为取出

#include <iostream> using namespace std; void function(int a) { if(a==0) return; function(a/10); if(a%2!=0&&a>0) cout<<a%10; else if(a%2!=0&&a<0) cout<<-a%10; } int main() { int a; cin>>a; if(a>0) function(a); else { cout<<"-"; function(a); } }

123456789101112131415161718192021222324252627

#include <iostream> using namespace std;void function(int a){    if(a==0)        return;    function(a/10);    if(a%2!=0&&a>0)        cout<<a%10;    else if(a%2!=0&&a<0)        cout<<-a%10;} int main(){    int a;    cin>>a;    if(a>0)        function(a);    else    {        cout<<"-";        function(a);    }}

## 0x8 交换最大、最小值

#include <iostream> using namespace std; int main() { int i,n; int a[10]; cout<<"Input n: "; cin>>n; cout<<"Input "<<n<<" integers: ";; for(i=0; i<n; i++) { cin>>a[i]; } int s,p,j; s=a[0]; p=0; for(j=1; j<n; j++) { if(s<a[j]) { s=a[j]; p=j; } } a[p]=a[n-1]; a[n-1]=s; s=a[0]; p=0; for(j=1; j<n; j++) { if(s>a[j]) { s=a[j]; p=j; } } a[p]=a[0]; a[0]=s; cout<<"After swapped: "; for(i=0; i<n; i++) { cout<<a[i]<<" "; } return 0; }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748

#include <iostream> using namespace std; int main(){    int i,n;    int a[10];    cout<<"Input n: ";    cin>>n;    cout<<"Input "<<n<<" integers: ";;    for(i=0; i<n; i++)    {        cin>>a[i];    }    int s,p,j;    s=a[0];    p=0;    for(j=1; j<n; j++)    {        if(s<a[j])        {            s=a[j];            p=j;        }    }    a[p]=a[n-1];    a[n-1]=s;    s=a[0];    p=0;    for(j=1; j<n; j++)    {        if(s>a[j])        {            s=a[j];            p=j;        }    }    a[p]=a[0];    a[0]=s;    cout<<"After swapped: ";    for(i=0; i<n; i++)    {        cout<<a[i]<<" ";    }    return 0;}

## 0x9 阶乘之和

#include <iostream> using namespace std; int fact(int n) { int i,t=1; for(i=1; i<=n; i++) { t*=i; } return t; } int main() { int m,n; cout<<"Enter m:"; cin>>m; cout<<"Enter n:"; cin>>n; cout<<m<<"!+"<<n<<"!="<<fact(m)+fact(n); return 0; }

1234567891011121314151617181920212223

#include <iostream>using namespace std; int fact(int n){    int i,t=1;    for(i=1; i<=n; i++)    {        t*=i;    }    return t;}int main(){    int m,n;    cout<<"Enter m:";    cin>>m;    cout<<"Enter n:";    cin>>n;    cout<<m<<"!+"<<n<<"!="<<fact(m)+fact(n);    return 0;}

## 0x10 阶乘之和

#include <iostream> #include <iomanip> using namespace std; double fact(int n,int m) { double i,t=0.0; for(i=m; i<=n; i++) { t+=(1/i); } return t; } int main() { int m,n; double s; cout<<"Enter m:"; cin>>m; cout<<"Enter n:"; cin>>n; s=fact(n,m); cout<<"sum="<<fixed<<setprecision(3)<<s; return 0; }

1234567891011121314151617181920212223242526

#include <iostream>#include <iomanip>using namespace std; double fact(int n,int m){    double i,t=0.0;    for(i=m; i<=n; i++)    {        t+=(1/i);    }    return t;}int main(){    int m,n;    double s;    cout<<"Enter m:";    cin>>m;    cout<<"Enter n:";    cin>>n;    s=fact(n,m);    cout<<"sum="<<fixed<<setprecision(3)<<s;    return 0;}

## 0x11 合并字符串(gets有问题)

#include <iostream> #include <stdio.h> #include <cstring> using namespace std; int main() { char a[100],b[100],c[100]; int i=0,j=0,k; gets(a); gets(b); for(i=0,j=0,k=0; a[i]!=0&&b[j]!=0;) { if(a[i]>b[j]) { c[k]=b[j]; j++; } else { c[k]=a[i]; i++; } k++; } if(a[i]=='\0') { for(; b[j]!=0; j++,k++) c[k]=b[j]; } else { for(; a[i]!=0; i++,k++) c[k]=a[i]; } c[k]='\0'; printf("%s",c); printf("\n"); return 0; }

123456789101112131415161718192021222324252627282930313233343536373839404142

#include <iostream>#include <stdio.h>#include <cstring> using namespace std;int main(){    char a[100],b[100],c[100];    int i=0,j=0,k;    gets(a);    gets(b);    for(i=0,j=0,k=0; a[i]!=0&&b[j]!=0;)    {        if(a[i]>b[j])        {            c[k]=b[j];            j++;        }        else        {            c[k]=a[i];            i++;        }        k++;    }    if(a[i]=='\0')    {        for(; b[j]!=0; j++,k++)            c[k]=b[j];    }    else    {        for(; a[i]!=0; i++,k++)            c[k]=a[i];    }    c[k]='\0';    printf("%s",c);    printf("\n");    return 0;}

## 0x12 存款到期利息

#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int money,year; double rate,interest; cout<<"Enter money,year and rate: "; cin>>money>>year>>rate; interest=money*pow((1+rate),year)-money; cout << "interest=" <<fixed<<setprecision(2)<<interest<<endl; return 0; }

12345678910111213141516

#include <iostream>#include <cmath>#include <iomanip>using namespace std; int main(){    int money,year;    double rate,interest;    cout<<"Enter money,year and rate: ";    cin>>money>>year>>rate;    interest=money*pow((1+rate),year)-money;    cout << "interest=" <<fixed<<setprecision(2)<<interest<<endl;    return 0;}

## 0x13 两组整数的交集

#include <iostream> using namespace std; int main() { int a[1000]; int b[1000];int o=0,d=0,m=0; int c[1000]; for(int i=0;;i++) { cin>>a[i];o++; if(a[i]==-1) break; } for(int i=0;;i++) { cin>>b[i];d++; if(b[i]==-1) break; } for(int i=0;i<o-1;i++) { for(int n=0;n<d-12;n++) if(a[i]==b[n]) { c[m]=a[i]; m++; } } for(int i=0;i<m;i++) { for(int h=i;h<m;h++) { if(c[i]>c[h]) { int k=c[i]; c[i]=c[h]; c[h]=k; } } } for(int i=0;i<m;i++) cout<<c[i]<<" "; return 0; }

12345678910111213141516171819202122232425262728293031323334353637383940414243444546

#include <iostream> using namespace std; int main(){    int a[1000];    int b[1000];int o=0,d=0,m=0;    int c[1000];    for(int i=0;;i++)    {        cin>>a[i];o++;        if(a[i]==-1) break;    }     for(int i=0;;i++)    {        cin>>b[i];d++;        if(b[i]==-1) break;    }    for(int i=0;i<o-1;i++)    {        for(int n=0;n<d-12;n++)            if(a[i]==b[n])        {            c[m]=a[i];            m++;        }     }    for(int i=0;i<m;i++)        {           for(int h=i;h<m;h++)           {               if(c[i]>c[h])               {                   int k=c[i];                  c[i]=c[h];                  c[h]=k;               }           }        }        for(int i=0;i<m;i++)            cout<<c[i]<<" ";    return 0;}

#include <stdio.h> #include <stdlib.h> int main() { int num1[20], num2[20], num3[20], num4[20]; int i,j,j1=0,j2=0,k1=0,k2=0,tmp,flag=0; char a=0; while(a!= '\n') { scanf("%d",&num1[j1++]); a= getchar(); } a=0; while(a != '\n') { scanf("%d", &num2[j2++]); a= getchar(); } for (i = 0; i <j1; i++) { for (j = 0; j<j2; j++) { if (num1[i] == num2[j]) { flag=1; } } if(flag) { num3[k1++]=num1[i]; } else { num4[k2++]=num1[i]; } flag=0; } for (i = 0; i <j2; i++) { for (j = 0; j <j1; j++) { if (num2[i] == num1[j]) { flag=1; } } if(!flag) { num4[k2++]=num2[i]; } flag=0; } for (i = 0; i < (k2-1); i++) { for (j = i+1; j < k2; j++) { if (num4[i] > num4[j]) { tmp = num4[i]; num4[i] = num4[j]; num4[j] = tmp; } } } for (i = 0; i < k2; i++) { printf("%d ", num4[i]); } printf ("\n"); for (i = 0; i < (k1-1); i++) { for (j = i+1; j < k1; j++) { if (num3[i] > num3[j]) { tmp = num3[i]; num3[i] = num3[j]; num3[j] = tmp; } } } for (i = 0; i < k1; i++) { printf("%d ", num3[i]); } return 0; }

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697

#include <stdio.h>#include <stdlib.h> int main(){    int num1[20], num2[20], num3[20], num4[20];    int i,j,j1=0,j2=0,k1=0,k2=0,tmp,flag=0;    char a=0;    while(a!= '\n')    {        scanf("%d",&num1[j1++]);        a= getchar();    }    a=0;    while(a != '\n')    {        scanf("%d", &num2[j2++]);        a= getchar();    }      for (i = 0; i <j1; i++)    {        for (j = 0; j<j2; j++)        {            if (num1[i] == num2[j])            {                flag=1;            }        }        if(flag)        {            num3[k1++]=num1[i];         }        else        {            num4[k2++]=num1[i];         }        flag=0;    }    for (i = 0; i <j2; i++)    {        for (j = 0; j <j1; j++)        {            if (num2[i] == num1[j])            {                flag=1;            }        }        if(!flag)        {            num4[k2++]=num2[i];         }        flag=0;    }    for (i = 0; i < (k2-1); i++)    {        for (j = i+1; j < k2; j++)        {            if (num4[i] > num4[j])            {                tmp = num4[i];                num4[i] = num4[j];                num4[j] = tmp;            }        }    }    for (i = 0; i < k2; i++)    {         printf("%d ", num4[i]);    }    printf ("\n");     for (i = 0; i < (k1-1); i++)    {        for (j = i+1; j < k1; j++)        {            if (num3[i] > num3[j])            {                tmp = num3[i];                num3[i] = num3[j];                num3[j] = tmp;            }        }    }    for (i = 0; i < k1; i++)    {         printf("%d ", num3[i]);    }    return 0;}

## 0x14 阶乘和数（正序）

#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str[10]; int i, j, sum = 0, num[10], tmp = 0; gets(str); printf("%s,", str); for (i = 0; i<strlen(str); i++) { num[i] = str[i] - '0'; } for (i = strlen(str) - 1; i >= 0; i--) { j = 1; if (num[i]>1) { while (num[i]--) { j = j * (num[i] + 1); } sum = sum + j; } else { sum = sum + 1; } if (i == 0) { printf("%d=%d\n", sum - tmp, sum); tmp = atoi(str); if (tmp == sum) { puts("Yes"); } else { puts("No"); } } else { printf("%d+", sum - tmp); } tmp = sum; } return 0; }

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){    char str[10];    int i, j, sum = 0, num[10], tmp = 0;    gets(str);    printf("%s,", str);    for (i = 0; i<strlen(str); i++)    {        num[i] = str[i] - '0';    }    for (i = strlen(str) - 1; i >= 0; i--)    {        j = 1;        if (num[i]>1)        {            while (num[i]--)            {                j = j * (num[i] + 1);            }            sum = sum + j;        }        else        {            sum = sum + 1;        }        if (i == 0)        {            printf("%d=%d\n", sum - tmp, sum);             tmp = atoi(str);            if (tmp == sum)            {                puts("Yes");            }            else            {                puts("No");            }        }        else        {            printf("%d+", sum - tmp);        }        tmp = sum;    }     return 0;}

## 0x15 求符合要求的自然数对

#include <iostream> #include <cmath> using namespace std; //求最大公约数。 int func(int a,int b) { int i,j,c; if(a>b) i=a,j=b; else i=b,j=a; c=i%j; while(c!=0) { i=j; j=c; c=i%j; } return 0; } int main() { int x,y; int a,b; for(x=1; x<667; x++) { y=667-x; a=func(x,y); b = x*y/a; if(fabs(((double)b)/a-120) < 1e-9) { printf("%d %d\n",x,y); } } return 0; }

12345678910111213141516171819202122232425262728293031323334353637

#include <iostream>#include <cmath>using namespace std;//求最大公约数。int func(int a,int b){    int i,j,c;    if(a>b)        i=a,j=b;    else        i=b,j=a;    c=i%j;    while(c!=0)    {        i=j;        j=c;        c=i%j;    }    return 0;}int main(){    int x,y;    int a,b;    for(x=1; x<667; x++)    {        y=667-x;        a=func(x,y);        b = x*y/a;        if(fabs(((double)b)/a-120) < 1e-9)        {            printf("%d %d\n",x,y);        }    }    return 0;}

## 0x16 数组的均值

#include <iostream> #include <iomanip> using namespace std; int main() { int a[10]; int n,sum=0; cin>>n; for(int i=0; i<n; i++) { cin>>a[i]; sum+=a[i]; } double avg; avg=(float)sum/n; cout<<fixed<<setprecision(2)<<avg<<endl; return 0; }

12345678910111213141516171819

#include <iostream>#include <iomanip>using namespace std;int main(){    int a[10];    int n,sum=0;    cin>>n;    for(int i=0; i<n; i++)    {        cin>>a[i];        sum+=a[i];    }    double avg;    avg=(float)sum/n;    cout<<fixed<<setprecision(2)<<avg<<endl;    return 0;}

## 0x17 打印数字菱形图案

#include <iostream> using namespace std; int main() { int i,j; int n; cin>>n; for(i=0; i<n; i++) { for(j=0; j<2*n-2*i; j++) cout<<" "; for(j=1; j<=i+1; j++) cout<<j<<" "; for(j=j-2; j>0; --j) cout<<j<<" "; cout<<endl; } for(i=n-1; i>0; i--) { for(j=0; j<=2*n-2*i+1; j++) cout<<" "; for(j=1; j<i+1; j++) cout<<j<<" "; for(j=j-2; j>0; --j) cout<<j<<" "; cout<<endl; } return 0; }

123456789101112131415161718192021222324252627282930

#include <iostream>using namespace std;int main(){    int i,j;    int n;    cin>>n;    for(i=0; i<n; i++)    {        for(j=0; j<2*n-2*i; j++)            cout<<" ";        for(j=1; j<=i+1; j++)            cout<<j<<" ";        for(j=j-2; j>0; --j)            cout<<j<<" ";        cout<<endl;    }    for(i=n-1; i>0; i--)    {        for(j=0; j<=2*n-2*i+1; j++)            cout<<" ";        for(j=1; j<i+1; j++)            cout<<j<<" ";        for(j=j-2; j>0; --j)            cout<<j<<" ";        cout<<endl;    }    return 0;}

## 0x18 报数3的人退出圈子

#include<stdio.h> #include<stdlib.h> int main() { int i=0,j=0; int a[10000]= {0}; int n; printf("Input n(n must be a natural number less than 10000):"); scanf("%d",&n); if(n>10000||n<=0) { printf("%d is out of range of valid values.\n",n); exit(1); } for(j=0; j<n; j++) a[j]=1; int k=0; int sum=0; do { k=k+a[i%n]; if(k==3) { a[i%n]=0; k=0; sum=0; } i++; for(j=0; j<n; j++) sum=sum+a[j]; } while(sum!=0); printf("Last No. is:%d\n",(i-1)%n+1); return 0; }

123456789101112131415161718192021222324252627282930313233343536

#include<stdio.h>#include<stdlib.h>int main(){    int i=0,j=0;    int a[10000]= {0};    int n;    printf("Input n(n must be a natural number less than 10000):");    scanf("%d",&n);    if(n>10000||n<=0)    {        printf("%d is out of range of valid values.\n",n);        exit(1);    }    for(j=0; j<n; j++)        a[j]=1;    int k=0;    int sum=0;    do    {        k=k+a[i%n];        if(k==3)        {            a[i%n]=0;            k=0;            sum=0;        }        i++;        for(j=0; j<n; j++)            sum=sum+a[j];    }    while(sum!=0);    printf("Last No. is:%d\n",(i-1)%n+1);    return 0;}

## 0x19 计算旅途时间

#include <iostream> using namespace std; int main() { int a,b,c,d,e,f; cout<<"Enter time1:"; cin>>a; cout<<"Enter time2:"; cin>>b; c=a/100; d=b/100; e=a%100; f=b%100; if(f>=e) cout<<"The train journey time is "<<d-c<<" hours "<<f-e<<" minutes"<<endl; else cout<<"The train journey time is "<<d-c-1<<" hours "<<f+60-e<<" minutes"<<endl; return 0; }

1234567891011121314151617181920

#include <iostream>using namespace std;int main(){    int a,b,c,d,e,f;    cout<<"Enter time1:";    cin>>a;    cout<<"Enter time2:";    cin>>b;    c=a/100;    d=b/100;    e=a%100;    f=b%100;    if(f>=e)        cout<<"The train journey time is "<<d-c<<" hours "<<f-e<<" minutes"<<endl;    else        cout<<"The train journey time is "<<d-c-1<<" hours "<<f+60-e<<" minutes"<<endl;    return 0;}

## 0x20 打印W形图案

#include <stdio.h> int main() { int n=0; int i=0; scanf( "%d", &n ); for( i=0; i<n; i++ ) { int width=0 ; int j=0 ; width=2*(n-i-1)-1 ; for ( j=0; j<i; j++ ) printf(" " ); printf("*"); //左边 for ( j=0; j<width; j++ ) printf(" " ); //两*间距 if ( i < n-1 ) printf("*"); //中间* for ( j=0; j<2*i-1; j++ ) //中间两*间距 printf(" " ); if ( i> 0 && i< n-1 ) //非头尾，有四* printf("*"); for ( j=0; j<width; j++ ) printf(" " ); printf("*"); //右边 printf("\n"); } return 0; }

12345678910111213141516171819202122232425262728293031

#include <stdio.h>int main(){    int n=0;    int i=0;    scanf( "%d", &n );    for( i=0; i<n; i++ )    {        int width=0 ;        int j=0 ;        width=2*(n-i-1)-1 ;        for ( j=0; j<i; j++ )            printf(" " );        printf("*"); //左边        for ( j=0; j<width; j++ )            printf(" " ); //两*间距        if ( i < n-1 )            printf("*"); //中间*        for ( j=0; j<2*i-1; j++ ) //中间两*间距            printf(" " );        if ( i> 0 && i< n-1 ) //非头尾，有四*            printf("*");        for ( j=0; j<width; j++ )            printf(" " );        printf("*"); //右边        printf("\n");    }    return 0;}

## 0x21 求平均分

#include <stdio.h> int main() { int math, eng, comp; float average; printf("math="); scanf("%d", &math); printf("eng="); scanf("%d", &eng); printf("comp="); scanf("%d", &comp); average = (math + eng + comp) / 3.0; printf("average=%.2f\n", average); }

123456789101112131415

#include <stdio.h>int main(){    int math, eng, comp;    float average;    printf("math=");    scanf("%d", &math);    printf("eng=");    scanf("%d", &eng);    printf("comp=");    scanf("%d", &comp);    average = (math + eng + comp) / 3.0;    printf("average=%.2f\n", average);}

## 0x22 前一秒和后一秒

#include <stdio.h> int main() { char time[8]; int h=0,m=0,s=0; scanf("%s",time); h=(time[0]-'0') * 10 + time[1]-'0'; m=(time[3]-'0') * 10 + time[4]-'0'; s=(time[6]-'0') * 10 + time[7]-'0'; int h1,h2,m1,m2,s1,s2; int flag=0; s1 = (s+60-1)%60; if(s-1<0) { flag=1; } m1 = (m+60-flag)%60; flag=0; if(m-1<0) { flag=1; } h1 = (h+24-flag)%24; printf("%02d:%02d:%02d\n",h1,m1,s1); s2 = (s+1)%60; flag =0; if(s+1>59) { flag=1; } m2 = (m+flag)%60; flag=0; if(m+1>59) { flag=1; } h2 = (h+flag)%24; printf("%02d:%02d:%02d",h2,m2,s2); }

12345678910111213141516171819202122232425262728293031323334353637383940414243

#include <stdio.h>int main(){    char time[8];    int h=0,m=0,s=0;    scanf("%s",time);     h=(time[0]-'0') * 10 + time[1]-'0';    m=(time[3]-'0') * 10 + time[4]-'0';    s=(time[6]-'0') * 10 + time[7]-'0';    int h1,h2,m1,m2,s1,s2;    int flag=0;    s1 = (s+60-1)%60;    if(s-1<0)    {        flag=1;    }     m1 = (m+60-flag)%60;    flag=0;    if(m-1<0)    {        flag=1;    }    h1 = (h+24-flag)%24;    printf("%02d:%02d:%02d\n",h1,m1,s1);    s2 = (s+1)%60;    flag =0;    if(s+1>59)    {        flag=1;    }     m2 = (m+flag)%60;    flag=0;    if(m+1>59)    {        flag=1;    }    h2 = (h+flag)%24;    printf("%02d:%02d:%02d",h2,m2,s2);}

## 0x23 闰年、季节和月天数

#include <stdio.h> #include <stdlib.h> int main() { int y,m,leap; scanf("%d %d",&y,&m); leap=(y%4==0 && y%100!=0)||(y%400==0); printf(leap?"Leap year,":"Common year,"); if(m/3==1) printf("Spring,"); else if(m/3==2) printf("Summer,"); else if(m/3==3) printf("Fall,"); else printf("Winter,"); if(m==2) printf("%d\n",28+leap); else if(m==4||m==6||m==9||m==11) printf("%d",30); else printf("%d",31); printf("\n"); }

12345678910111213141516171819202122232425

#include <stdio.h>#include <stdlib.h>int main(){    int y,m,leap;    scanf("%d %d",&y,&m);    leap=(y%4==0 && y%100!=0)||(y%400==0);    printf(leap?"Leap year,":"Common year,");    if(m/3==1)        printf("Spring,");    else if(m/3==2)        printf("Summer,");    else if(m/3==3)        printf("Fall,");    else        printf("Winter,");    if(m==2)        printf("%d\n",28+leap);    else if(m==4||m==6||m==9||m==11)        printf("%d",30);    else        printf("%d",31);    printf("\n");}

## 0x24 三角形周长和面积

#include <iostream> #include<iomanip> #include<cmath> using namespace std; int main() { double a,b,c,s; cout<<"Enter 3 sides of the triangle: "; cin>>a; cin>>b; cin>>c; s=(a+b+c)/2; if(a+b>c&&a+c>b&&b+c>a) cout<<"area="<<fixed<<setprecision(2)<<sqrt(s*(s-a)*(s-b)*(s-c))<<"; perimeter="<<a+b+c; else cout<<"These sides do not correspond to a valid triangle"; return 0; }

1234567891011121314151617181920

#include <iostream>#include<iomanip>#include<cmath>using namespace std; int main(){    double a,b,c,s;    cout<<"Enter 3 sides of the triangle: ";    cin>>a;    cin>>b;    cin>>c;    s=(a+b+c)/2;    if(a+b>c&&a+c>b&&b+c>a)        cout<<"area="<<fixed<<setprecision(2)<<sqrt(s*(s-a)*(s-b)*(s-c))<<"; perimeter="<<a+b+c;    else        cout<<"These sides do not correspond to a valid triangle";    return 0;}

## 0x25 皮球落地

#include <stdio.h> int main() { int n,cnt=0; double distence,h; scanf("%lf %d", &h, &n); distence = h; cnt=1; do { h=0.5*h;//第一次落地弹起距离 distence += 2*h;//第二次落地空间划过的距离 cnt++; } while(cnt<n); //循环结束时，此时distence是第n次落地空间划过的距离，但是h是第n-1次落地弹起的距离 h=0.5*h;//第n次落地弹起的距离 printf("distence = %.1f, h = %.1f\n", distence, h); return 0; }

12345678910111213141516171819202122232425

#include <stdio.h> int main(){    int n,cnt=0;    double distence,h;     scanf("%lf %d", &h, &n);     distence = h;    cnt=1;    do    {        h=0.5*h;//第一次落地弹起距离        distence += 2*h;//第二次落地空间划过的距离        cnt++;    }    while(cnt<n); //循环结束时，此时distence是第n次落地空间划过的距离，但是h是第n-1次落地弹起的距离     h=0.5*h;//第n次落地弹起的距离    printf("distence = %.1f, h = %.1f\n", distence, h);     return 0;}

## 0x26 n个a之和

#include <iostream> using namespace std; int main() { int a,n,s=0; cout<<"Input a, n: "; cin>>a>>n; while(n>=0) { s=a*n+s; a=a*10; n--; } cout<<"s="<<s<< endl; return 0; }

12345678910111213141516171819

#include <iostream> using namespace std; int main(){    int a,n,s=0;    cout<<"Input a, n: ";    cin>>a>>n;    while(n>=0)    {        s=a*n+s;        a=a*10;        n--;    }    cout<<"s="<<s<< endl;    return 0;}

## 0x27 分类统计字符个数

#include <iostream> using namespace std; int main() { char c; int up=0,lo=0,sp=0,nu=0,other=0; cout<<"Input a string:"; while(cin.get(c)) { if(c=='\n') break; if(c>='A' && c<='Z') up++; else if(c>='a' && c<='z') lo++; else if(c>='0'&&c<='9') nu++; else if(c==' ') sp++; else other++; } cout<<"Number of uppercase letters is :"<<up<<endl; cout<<"Number of lowercase letters is :"<<lo<<endl; cout<<"Number of spacebars is :"<<sp<<endl; cout<<"Number of digits is :"<<nu<<endl; cout<<"Number of other characters is :"<<other<<endl; return 0; }

123456789101112131415161718192021222324252627282930

#include <iostream>using namespace std;int main(){    char c;    int up=0,lo=0,sp=0,nu=0,other=0;    cout<<"Input a string:";        while(cin.get(c))    {        if(c=='\n')            break;        if(c>='A' && c<='Z')            up++;        else if(c>='a' && c<='z')            lo++;        else if(c>='0'&&c<='9')            nu++;        else if(c==' ')            sp++;        else            other++;    }    cout<<"Number of uppercase letters is :"<<up<<endl;    cout<<"Number of lowercase letters is :"<<lo<<endl;    cout<<"Number of spacebars is :"<<sp<<endl;    cout<<"Number of digits is :"<<nu<<endl;    cout<<"Number of other characters is :"<<other<<endl;    return 0;}

## 0x28 用对应字母替换字符串中的大写字母

A → Z

B → Y

C → X

D → W

X → C

Y → B

Z → A

#include <iostream> #include <cstdio> using namespace std; int main() { char s[81],c; int i=0; cout<<"Input a string:"; gets(s); cout<<"After replaced:"; while((c=s[i++])!='\0') { if (c>='A' && c<='Z') c='Z'-c+'A'; cout<<c; } cout<<endl; return 0; }

12345678910111213141516171819202122

#include <iostream>#include <cstdio> using namespace std; int main(){    char s[81],c;    int i=0;    cout<<"Input a string:";    gets(s);    cout<<"After replaced:";    while((c=s[i++])!='\0')    {        if (c>='A' && c<='Z')            c='Z'-c+'A';        cout<<c;    }    cout<<endl;    return 0;}

#include<iostream> #include <stdio.h> using namespace std; int main() { int i; char str[80]; cout<<"Input a string:"; i=0; while((str[i]=getchar())!='\n') i++; str[i]='\0'; cout<<"After replaced:"; for(i=0; str[i]!='\0'; i++) { if(str[i]>='A'&&str[i]<='Z') str[i]='A'+'Z'-str[i]; putchar(str[i]); } return 0; }

123456789101112131415161718192021222324

#include<iostream>#include <stdio.h>using namespace std;int main(){    int i;    char str[80];     cout<<"Input a string:";    i=0;    while((str[i]=getchar())!='\n')        i++;    str[i]='\0';    cout<<"After replaced:";    for(i=0; str[i]!='\0'; i++)    {        if(str[i]>='A'&&str[i]<='Z')            str[i]='A'+'Z'-str[i];        putchar(str[i]);    }    return 0;}

## 0x29 温度转换

#include<stdio.h> #include<stdlib.h> main() { float c,a=5,b=9,f; scanf("%f",&f); c=(a/b)*(f-32); printf("%f",c); }

1234567891011121314

#include<stdio.h>#include<stdlib.h>main(){    float c,a=5,b=9,f;     scanf("%f",&f);     c=(a/b)*(f-32);     printf("%f",c); }

## 0x30 递增或递减输出

#include<iostream> using namespace std; int main() { int m,n; cin>>m>>n; if(m>n) { for(int i=m; i>=n; i--) cout<<i<<endl; } else for(int i=m; i<=n; i++) cout<<i<<endl; return 0; }

12345678910111213141516171819

#include<iostream>using namespace std;int main(){    int m,n;    cin>>m>>n;    if(m>n)    {         for(int i=m; i>=n; i--)            cout<<i<<endl;    }    else        for(int i=m; i<=n; i++)            cout<<i<<endl;     return 0;}

## 0x31 分数化简

#include<iostream> using namespace std; int func(int a,int b) { int i,j,c; if(a>b) i=a,j=b; else i=b,j=a; c=i%j; while(c!=0) { i=j; j=c; c=i%j; } return 0; } int main() { int m,n; int a,b,c; cin>>m>>n; c=func(m,n); a=m/c;b=n/c; cout<<a<<" "<<b; }

123456789101112131415161718192021222324252627282930

#include<iostream>using namespace std; int func(int a,int b){    int i,j,c;    if(a>b)        i=a,j=b;    else        i=b,j=a;    c=i%j;    while(c!=0)    {        i=j;        j=c;        c=i%j;    }    return 0;} int main(){    int m,n;    int a,b,c;    cin>>m>>n;    c=func(m,n);    a=m/c;b=n/c;    cout<<a<<" "<<b;}

##0x32 分数化简 编写程序，从控制台读入一个分数的分子和分母（分数无符号，并且分子小于分母，其大小不会超过int数据类型的表示范围），输出化简后分子和分母不含公约数的分数。

#include <iostream> using namespace std; //求最大公约数 int gcd(int n,int m) { int temp,r; //把大的数放在n里面 if(n<m) { temp=n; n=m; m=temp; } while(m!=0) { r=n%m; n=m; m=r; } return n; } int main() { int a,b;//a是分子 b是分母 cout<<"please input a and b:"; cin>>a>>b; cout<<a/gcd(a,b)<<"/"<<b/gcd(a,b); return 0; }

123456789101112131415161718192021222324252627282930

#include <iostream>using namespace std;//求最大公约数int  gcd(int n,int m){    int temp,r;    //把大的数放在n里面    if(n<m)    {        temp=n;        n=m;        m=temp;    }    while(m!=0)    {        r=n%m;        n=m;        m=r;    }    return n;}int main(){    int a,b;//a是分子 b是分母    cout<<"please input a and b:";    cin>>a>>b;    cout<<a/gcd(a,b)<<"/"<<b/gcd(a,b);    return 0;}

##0x32 输出自然数和 计算并输出k以内最大的10个能被13或17整除的自然数之和。k的值从键盘输入。

#include <iostream> using namespace std; int main() { int k; cin>>k; int a[10]= {0}; int j=0; int sum=0; for(int i=k; i>=0; i--) { if(i%13==0||i%17==0) { if(j>9) { break; } a[j]=i; j++; } } for(int m=0; m<=9; m++) { sum=sum+a[m]; } cout<<sum<<endl; return 0; }

1234567891011121314151617181920212223242526272829

#include <iostream>using namespace std;int main(){    int k;    cin>>k;    int a[10]= {0};    int j=0;    int sum=0;    for(int i=k; i>=0; i--)    {        if(i%13==0||i%17==0)        {            if(j>9)            {                break;            }            a[j]=i;            j++;        }    }    for(int m=0; m<=9; m++)    {        sum=sum+a[m];    }    cout<<sum<<endl;    return 0;}

##0x33 输出实数和 已知一个数列的前三项分别为0，0，1， 以后的各项都是其相邻的前三项之和,计算并输出该数列前n项的平方根之和sum，默认6位小数。

#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int a1=0; int a2=0; int a3=1; int a4; double sum=1; int n; cin>>n; for(int i=4; i<=n; i++) { a4=a1+a2+a3; sum=sum+sqrt(a4); a1=a2; a2=a3; a3=a4; } cout<<fixed<<setprecision(6)<<sum<<endl; return 0; }

1234567891011121314151617181920212223242526

#include <iostream>#include <iomanip>#include <cmath>using namespace std; int main(){    int a1=0;    int a2=0;    int a3=1;    int a4;    double sum=1;    int n;    cin>>n;    for(int i=4; i<=n; i++)    {        a4=a1+a2+a3;        sum=sum+sqrt(a4);        a1=a2;        a2=a3;        a3=a4;    }    cout<<fixed<<setprecision(6)<<sum<<endl;    return 0;}

## 0x34 判断是否互质

#include <iostream> using namespace std; int main() { int m; int n; cin>>m>>n; int t=m>n?n:m; int i=2; for (i; i<=t; i++) { if(i<t&&m%i==0&&n%i==0) { cout<<"NO"<<endl; break; } if(i==t) { cout<<"YES"<<endl; } } return 0; }

1234567891011121314151617181920212223242526

#include <iostream> using namespace std; int main(){    int m;    int n;    cin>>m>>n;    int t=m>n?n:m;    int i=2;    for (i; i<=t; i++)    {        if(i<t&&m%i==0&&n%i==0)        {            cout<<"NO"<<endl;            break;        }        if(i==t)        {            cout<<"YES"<<endl;        }    }    return 0;}

##0x35 求出素数 从键盘输入一个大于2的正整数n，求解并输出大小最接近n的素数（不包括n）。

#include <iostream> #include <cmath> using namespace std; //判断素数 int isprime(int x) { int i; if(x<2) return 0; for(i=2; i<sqrt(x); i++) if(x%i==0) return 0; return 1; } int main() { int i,a,b,n; cin>>n; for(a=n+1; !isprime(a); a++); //不是素数就跳过，是素数就结束循环。 for(b=n-1; !isprime(b); b--); if(a-n<n-b) cout<<a<<endl; else if(a-n==n-b) cout<<a<<" "<<b<<endl; else cout<<b<<endl; return 0; }

1234567891011121314151617181920212223242526272829303132

#include <iostream>#include <cmath> using namespace std; //判断素数int isprime(int x){    int i;    if(x<2)        return 0;    for(i=2; i<sqrt(x); i++)        if(x%i==0)            return 0;    return 1;} int main(){    int i,a,b,n;    cin>>n;    for(a=n+1; !isprime(a); a++); //不是素数就跳过，是素数就结束循环。    for(b=n-1; !isprime(b); b--);    if(a-n<n-b)        cout<<a<<endl;    else if(a-n==n-b)        cout<<a<<" "<<b<<endl;    else        cout<<b<<endl;    return 0;}

## 0x36 求公式近似值

#include <iostream> #include <iomanip> using namespace std; int main() { double ex=1; double sum=1; double x; int n; cin>>x>>n; for(int i=1; i<=n; i++) { sum=sum*x/i; ex=ex+sum; } cout<<fixed<<setprecision(6)<<ex<<endl; return 0; }

12345678910111213141516171819

#include <iostream>#include <iomanip>using namespace std;int main(){    double ex=1;    double sum=1;    double x;    int n;    cin>>x>>n;    for(int i=1; i<=n; i++)    {        sum=sum*x/i;        ex=ex+sum;    }    cout<<fixed<<setprecision(6)<<ex<<endl;    return 0;}

##0x37 转换整数 编写一个程序，当用户输入一个小数（正值浮点数）后，将小数转化为最近的整数输出（四舍五入）。

#include <iostream> using namespace std; int main() { double a; int b; while (cin>>a) { b=a+0.5; cout <<b<<endl; } return 0; }

1234567891011121314

#include <iostream>using namespace std;int main(){    double a;    int b;    while (cin>>a)    {        b=a+0.5;        cout <<b<<endl;    }    return 0;}

##0x38 正整数的打印 给出一个不多于5位的正整数，要求： 1.求出它是几位数。 2.分别打印出每一位数字。 3.按照逆序打印出每一位数字。

#include <iostream> #include <cmath> using namespace std; int main() { int a; cin>>a; int b; int n=0; b=log10(a)+1; cout<<b<<endl; cout<<a<<endl; while (a) { n=n*10+a%10; a=a/10; } cout <<n<<endl; return 0; }

12345678910111213141516171819202122

#include <iostream>#include <cmath>using namespace std; int main(){    int a;    cin>>a;    int b;    int n=0;    b=log10(a)+1;    cout<<b<<endl;    cout<<a<<endl;    while (a)    {        n=n*10+a%10;        a=a/10;    }    cout <<n<<endl;    return 0;}

##0x39 找最大最小整数 编写一个程序，用户输入若干整数，试找出其中的最大数和最小数。

#include <iostream> using namespace std; int main() { int m; int n; cin>>m; int nmax=1000000; int nmin=0; for(int i=1; i<=m; i++) { cin>>n; if(n<nmax) { nmax=n; } if(n>nmin) { nmin=n; } } cout<<nmin<<" "<<nmax<<endl; return 0; }

12345678910111213141516171819202122232425262728

#include <iostream> using namespace std; int main(){    int m;    int n;    cin>>m;    int nmax=1000000;    int nmin=0;     for(int i=1; i<=m; i++)    {        cin>>n;        if(n<nmax)        {            nmax=n;        }        if(n>nmin)        {            nmin=n;        }    }    cout<<nmin<<" "<<nmax<<endl;    return 0;}

##0x40 求A,B 输入三位数字N，求两位数AB（其中个位数字为B，十位数字为A，且有0 < A < B <= 9）。使得下列等式成立： AB x BA = N 其中BA是把AB中个、十位数字交换所得的两位数。 编写程序，接收控制台输入的三位整数N，求解A，B并输出。 如果没有解则输出 No Answer。

#include <iostream> using namespace std; int main() { int N; cin>>N; int A; int B; A=N/10%10;//十位 B=N%10;//个位 int a; a=A*10+B; int b; b=B*10+A; for(int i=1; i<=9; i++) //b { for(int j=1; j<i; j++) { a=j*10+i; b=i*10+j; if(a*b==N) { cout<<a<<endl; break; } else { continue; } }//a } return 0; }

12345678910111213141516171819202122232425262728293031323334353637

#include <iostream> using namespace std; int main(){    int N;    cin>>N;    int A;    int B;    A=N/10%10;//十位    B=N%10;//个位    int a;    a=A*10+B;    int b;    b=B*10+A;    for(int i=1; i<=9; i++) //b    {        for(int j=1; j<i; j++)        {            a=j*10+i;            b=i*10+j;            if(a*b==N)            {                cout<<a<<endl;                break;             }            else            {                continue;            }        }//a    }    return 0;}

##0x41 回文数 控制台输入两个整数a和b（必有a<b），以空格分隔。 输出有若干行，每行有一个a和b之间的回文数。输出各行上的数字不重复，且从小至大依次按序输出。

#include <iostream> using namespace std; int main() { int a; int b; cin>>a>>b; for(int i=a; i<=b; i++) { if(i<10) { cout<<i<<endl; } if(10<=i&&i<100) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } if(100<=i&&i<1000) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } if(1000<=i&&i<10000) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } if(10000<=i&&i<100000) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } } return 0; }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475

#include <iostream> using namespace std; int main(){    int a;    int b;    cin>>a>>b;    for(int i=a; i<=b; i++)    {        if(i<10)        {            cout<<i<<endl;        }        if(10<=i&&i<100)        {            int x=0;            int y=i;            while(y)            {                x=x*10+y%10;                y=y/10;            }            if(x==i)            {                cout<<x<<endl;            }        }        if(100<=i&&i<1000)        {            int x=0;            int y=i;            while(y)            {                x=x*10+y%10;                y=y/10;            }            if(x==i)            {                cout<<x<<endl;            }        }        if(1000<=i&&i<10000)        {            int x=0;            int y=i;            while(y)            {                x=x*10+y%10;                y=y/10;            }            if(x==i)            {                cout<<x<<endl;            }        }        if(10000<=i&&i<100000)        {            int x=0;            int y=i;            while(y)            {                x=x*10+y%10;                y=y/10;            }            if(x==i)            {                cout<<x<<endl;            }        }    }    return 0;}

##0x42 计算平均值 从键盘输入三个整数，分别存入x,y,z三个整型变量中，计算并输出三个数的和以及平均值。

#include <iostream> #include <iomanip> using namespace std; int main() { int x,y,z; cin>>x>>y>>z; double sum; double avg; sum=x+y+z; avg=sum/3; cout<<sum<<endl; cout<<fixed<<setprecision(2)<<avg<<endl; return 0; }

1234567891011121314151617

#include <iostream>#include <iomanip>using namespace std; int main(){    int x,y,z;    cin>>x>>y>>z;    double sum;    double avg;    sum=x+y+z;    avg=sum/3;    cout<<sum<<endl;    cout<<fixed<<setprecision(2)<<avg<<endl;    return 0;}

##0x43 求和 编写一个程序，求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)。 输入一个正整数n，根据求s公式计算s并输出。

#include <iostream> using namespace std; int main() { int a; cin>>a; int sum=0; for(int i=1; i<=a; i++) { sum=i*(a+1-i)+sum; } cout<<sum; return 0; }

1234567891011121314151617

#include <iostream> using namespace std; int main(){    int a;    cin>>a;    int sum=0;    for(int i=1; i<=a; i++)    {        sum=i*(a+1-i)+sum;    }    cout<<sum;    return 0;}

##0x44 前驱、后继字符 从键盘输入一个字符，求出它的前驱和后继字符（按照ASCII码值排序），并按照从小到大的顺序输出这三个字符和对应的ASCII值。

#include <iostream> using namespace std; int main() { char a; cin>>a; int b=a; int c=b-1; int d=b+1; char e=c; char f=d; cout<<e<<" "<<a<<" "<<f<<endl; cout<<c<<" "<<b<<" "<<d; return 0; }

123456789101112131415161718

#include <iostream> using namespace std; int main(){    char a;    cin>>a;    int b=a;    int c=b-1;    int d=b+1;    char e=c;    char f=d;    cout<<e<<" "<<a<<" "<<f<<endl;    cout<<c<<" "<<b<<" "<<d;    return 0;}

##0x45 能否被3,5,7整除 编程，从键盘输入一个整数，判断它能否被3,5,7整除，并输出以下信息之一： ⑴ Can be divisible by 3,5,7. 即能同时被3,5,7整除； ⑵ Can be divisible by ?,?. (其中?为3,5,7之一) 即能被其中两个数(要指出哪两个)整除； ⑶ Can be divisible by ?. (其中?为3,5,7之一) 即能被其中一个数(要指出哪一个)整除； ⑷ Can not be divisible by 3,5,7. 即不能被3,5,7任一个整除。

#include <iostream> using namespace std; int main() { int m; cin>>m; if(m%3==0&&m%5==0&&m%7==0) { cout<<"Can be divisible by 3,5,7."<<endl; } if(m%3!=0&&m%5==0&&m%7==0) { cout<<"Can be divisible by 5,7."<<endl; } if(m%3==0&&m%5!=0&&m%7==0) { cout<<"Can be divisible by 3,7."<<endl; } if(m%3==0&&m%5==0&&m%7!=0) { cout<<"Can be divisible by 3,5."<<endl; } if(m%3!=0&&m%5!=0&&m%7==0) { cout<<"Can be divisible by 7."<<endl; } if(m%3!=0&&m%5==0&&m%7!=0) { cout<<"Can be divisible by 5."<<endl; } if(m%3==0&&m%5!=0&&m%7!=0) { cout<<"Can be divisible by 3."<<endl; } if(m%3!=0&&m%5!=0&&m%7!=0) { cout<<"Can not be divisible by 3,5,7."<<endl; } return 0; }

12345678910111213141516171819202122232425262728293031323334353637383940414243

#include <iostream> using namespace std; int main(){    int m;    cin>>m;    if(m%3==0&&m%5==0&&m%7==0)    {        cout<<"Can be divisible by 3,5,7."<<endl;    }    if(m%3!=0&&m%5==0&&m%7==0)    {        cout<<"Can be divisible by 5,7."<<endl;    }    if(m%3==0&&m%5!=0&&m%7==0)    {        cout<<"Can be divisible by 3,7."<<endl;    }    if(m%3==0&&m%5==0&&m%7!=0)    {        cout<<"Can be divisible by 3,5."<<endl;    }    if(m%3!=0&&m%5!=0&&m%7==0)    {        cout<<"Can be divisible by 7."<<endl;    }    if(m%3!=0&&m%5==0&&m%7!=0)    {        cout<<"Can be divisible by 5."<<endl;    }    if(m%3==0&&m%5!=0&&m%7!=0)    {        cout<<"Can be divisible by 3."<<endl;    }    if(m%3!=0&&m%5!=0&&m%7!=0)    {        cout<<"Can not be divisible by 3,5,7."<<endl;    }    return 0;}

##0x46 圆的体积 编写一个程序实现如下功能：从键盘输入圆球半径r，屏幕输出圆球的体积(保留2位小数)

#include <iostream> #include <iomanip> using namespace std; #define PI 3.1415926 int main() { double v; double r; cin>>r; v=4.0/3.0*PI*r*r*r; cout<<fixed<<setprecision(2); cout<<v<<endl; return 0; }

123456789101112131415

#include <iostream>#include <iomanip>using namespace std;#define PI 3.1415926int main(){    double v;    double r;    cin>>r;    v=4.0/3.0*PI*r*r*r;    cout<<fixed<<setprecision(2);    cout<<v<<endl;    return 0;}

##0x47 逆序乘积式

#include <iostream> using namespace std; int main() { int a; int b; cin>>a>>b; int c=0; int d=0; int e=a; int f=b; while(e) { c=c*10+e%10; e=e/10; } while(f) { d=d*10+f%10; f=f/10; } if(a*b==c*d) { cout<<a<<"*"<<b<<"="<<c<<"*"<<d<<endl; } else { cout<<a<<"*"<<b<<"!="<<c<<"*"<<d<<endl; } return 0; }

123456789101112131415161718192021222324252627282930313233

#include <iostream>using namespace std;int main(){    int a;    int b;    cin>>a>>b;    int c=0;    int d=0;    int e=a;    int f=b;    while(e)    {        c=c*10+e%10;        e=e/10;    }    while(f)    {        d=d*10+f%10;        f=f/10;    }    if(a*b==c*d)    {        cout<<a<<"*"<<b<<"="<<c<<"*"<<d<<endl;    }    else    {        cout<<a<<"*"<<b<<"!="<<c<<"*"<<d<<endl;    }    return 0;}

## 0x48 计算sinx的近似值

sin x = x – x3/3! + x5/5! – x7/7! + … + (-1)n-1x2n-1/(2n-1)!

#include <iostream> #include <cmath> #include <iomanip> using namespace std; double jc(double n) { double a=1; for(double i=1.0; i<=2*n-1; i++) { a*=i; } return a; } double lc(double x,double n) { double b=1; for(double i=1.0; i<=2*n-1; i++) { b=b*x; } return b; } int main() { double m=0; double x; double e; cin>>x>>e; for(int n=1; n<1000; n++) { double a=m; m=m+pow(-1,n-1)*lc(x,n)/jc(n); if(fabs(m-a)<e) { cout<<n<<" "<<fixed<<setprecision(9)<<m<<endl; break; } } return 0; }

123456789101112131415161718192021222324252627282930313233343536373839404142

#include <iostream>#include <cmath>#include <iomanip>using namespace std;double jc(double n){    double a=1;    for(double i=1.0; i<=2*n-1; i++)    {        a*=i;    }    return a;}double lc(double x,double n){    double b=1;    for(double i=1.0; i<=2*n-1; i++)    {        b=b*x;    }    return b;}int main(){    double m=0;    double x;    double e;    cin>>x>>e;    for(int n=1; n<1000; n++)    {        double a=m;        m=m+pow(-1,n-1)*lc(x,n)/jc(n);        if(fabs(m-a)<e)        {            cout<<n<<" "<<fixed<<setprecision(9)<<m<<endl;            break;        }    }    return 0;}

## 0x49 猴子选大王

#include <iostream> using namespace std; int f(int n, int m, int q) { if (n == 1) return 1; if (q > 1) return (f(n,m,1)+q-2)%n+1; else return (f(n-1,m,1)+m-1)%n+1; } int main() { int n, m, q; cin>>n>>m>>q; cout<<f(n,m,q); return 0; }

12345678910111213141516171819202122

#include <iostream>using namespace std; int f(int n, int m, int q){    if (n == 1)        return 1;     if (q > 1)        return (f(n,m,1)+q-2)%n+1;    else        return (f(n-1,m,1)+m-1)%n+1;} int main(){    int n, m, q;    cin>>n>>m>>q;    cout<<f(n,m,q);    return 0;}

##0x50 求cos(x)的近似值

#include <iostream> #include <cmath> #include <iomanip> using namespace std; double jc(double n) { double a=1; for(double i=1.0; i<=2*n; i++) { a*=i; } return a; } double lc(double x,double n) { double b=1; for(double i=1.0; i<=2*n; i++) { b=b*x; } return b; } int main() { double m=0; double x; double e; cin>>x>>e; for(int n=0; n<1000; n++) { double a=m; m=m+pow(-1,n)*lc(x,n)/jc(n); if(fabs(m-a)<e) { cout<<n<<" "<<fixed<<setprecision(7)<<m<<endl; break; } } return 0; }

1234567891011121314151617181920212223242526272829303132333435363738394041

#include <iostream>#include <cmath>#include <iomanip>using namespace std;double jc(double n){    double a=1;    for(double i=1.0; i<=2*n; i++)    {        a*=i;    }    return a;}double lc(double x,double n){    double b=1;    for(double i=1.0; i<=2*n; i++)    {        b=b*x;    }    return b;}int main(){    double m=0;    double x;    double e;    cin>>x>>e;    for(int n=0; n<1000; n++)    {        double a=m;        m=m+pow(-1,n)*lc(x,n)/jc(n);        if(fabs(m-a)<e)        {            cout<<n<<" "<<fixed<<setprecision(7)<<m<<endl;            break;        }    }    return 0;}

##0x51 奇数序列

#include <iostream> using namespace std; int main() { int a; cin>>a; a=a*a*a; int i; int b; int c=0; for(i=1; i<=a; i=i+2) { c=0; for(int b=i; b>=1; b=b-2) { c=c+b; if(c==a) for(int d=b; d<=i; d=d+2) cout<<d<<" "; if(c==a) break; } if(c==a) break; } return 0; }

12345678910111213141516171819202122232425262728293031

#include <iostream> using namespace std; int main(){    int a;    cin>>a;    a=a*a*a;    int i;    int b;    int c=0;    for(i=1; i<=a; i=i+2)    {        c=0;        for(int b=i; b>=1; b=b-2)        {            c=c+b;            if(c==a)                for(int d=b; d<=i; d=d+2)                    cout<<d<<" ";            if(c==a)                break;        }        if(c==a)            break;    }     return 0;}

## 0x52 求三角形面积

#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; double s=(a+b+c)/2; double S; S=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<fixed<<setprecision(3)<<S; return 0; }

12345678910111213141516

#include <iostream>#include <iomanip>#include <cmath>using namespace std; int main(){    int a,b,c;    cin>>a>>b>>c;    double s=(a+b+c)/2;    double S;    S=sqrt(s*(s-a)*(s-b)*(s-c));    cout<<fixed<<setprecision(3)<<S;    return 0;}

##0x53 计算三角形周长和面积

#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double a,b,c; double area; double perimeter; cout<<"Enter 3 sides of the triangle:"; cin>>a>>b>>c; if(a+b>c||b+c>a||a+c>b) { double s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); perimeter=a+b+c; cout<<fixed<<setprecision(2); cout<<"area="<<area<<"perimeter="<<perimeter; } else { cout<<"These sides do not correspond to a valid triangle"; } return 0; }

123456789101112131415161718192021222324252627

#include <iostream>#include <iomanip>#include <cmath>using namespace std; int main(){    double a,b,c;    double area;    double perimeter;    cout<<"Enter 3 sides of the triangle:";    cin>>a>>b>>c;    if(a+b>c||b+c>a||a+c>b)    {        double s=(a+b+c)/2;        area=sqrt(s*(s-a)*(s-b)*(s-c));        perimeter=a+b+c;        cout<<fixed<<setprecision(2);        cout<<"area="<<area<<"perimeter="<<perimeter;    }    else    {        cout<<"These sides do not correspond to a valid triangle";    }    return 0;}

## 0x54 两个整数的和、差、积、商与余数

#include <iostream> #include <iomanip> using namespace std; int main() { double num1; double num2; cout<<"Enter num1:"; cin>>num1; cout<<"Enter num2:"; cin>>num2; cout<<num1<<"+"<<num2<<"="<<num1+num2<<endl; cout<<num1<<"-"<<num2<<"="<<num1-num2<<endl; cout<<num1<<"*"<<num2<<"="<<num1*num2<<endl; cout<<num1<<"/"<<num2<<"="<<fixed<<setprecision(2)<<num1/num2<<endl; cout<<(int)num1<<"%"<<(int)num2<<"="<<(int)num1%(int)num2<<endl; return 0; }

12345678910111213141516171819

#include <iostream>#include <iomanip>using namespace std;int main(){    double num1;    double num2;    cout<<"Enter num1:";    cin>>num1;    cout<<"Enter num2:";    cin>>num2;    cout<<num1<<"+"<<num2<<"="<<num1+num2<<endl;    cout<<num1<<"-"<<num2<<"="<<num1-num2<<endl;    cout<<num1<<"*"<<num2<<"="<<num1*num2<<endl;    cout<<num1<<"/"<<num2<<"="<<fixed<<setprecision(2)<<num1/num2<<endl;    cout<<(int)num1<<"%"<<(int)num2<<"="<<(int)num1%(int)num2<<endl;    return 0;}

## 0x55 按位拆解

#include <iostream> using namespace std; int main() { int a; int x,y,z; cin>>a; x=a%10; a=a/10; y=a%10; a=a/10; z=a; cout<<x<<" "<<y<<" "<<z<<endl; return 0; }

123456789101112131415161718

#include <iostream> using namespace std; int main(){    int a;    int x,y,z;    cin>>a;    x=a%10;    a=a/10;    y=a%10;    a=a/10;    z=a;    cout<<x<<" "<<y<<" "<<z<<endl;    return 0;}

## 0x56 判断可逆素数

#include <stdio.h> #include <math.h> int getrnum(int num){ int rnum=0; do{ rnum *= 10; rnum = rnum + num%10; }while((num/=10)!=0); return rnum; } int sushu(int num){ int i,k; k = (int)sqrt((double)num); for(i=2;i<=k;i++){ if(num%i==0) break; } if(i<k) return 0;//返回值为0表示不为素数 else return 1; } void keni(int num){ int flag,rnum; flag = sushu(num); if(flag == 0) printf("no\n"); else{ rnum = getrnum(num); flag = sushu(rnum); if(flag == 0) printf("no\n"); else printf("yes\n"); } } int main(){ int num,rnum; scanf("%d",&num); keni(num); getchar(); return 0; }

1234567891011121314151617181920212223242526272829303132333435363738394041424344

#include <stdio.h>#include <math.h>int getrnum(int num){    int rnum=0;    do{        rnum *= 10;        rnum = rnum + num%10;    }while((num/=10)!=0);    return rnum;}int sushu(int num){    int i,k;    k = (int)sqrt((double)num);    for(i=2;i<=k;i++){        if(num%i==0)            break;    }    if(i<k)        return 0;//返回值为0表示不为素数    else        return 1;}void keni(int num){    int flag,rnum;    flag = sushu(num);    if(flag == 0)        printf("no\n");    else{        rnum = getrnum(num);        flag = sushu(rnum);        if(flag == 0)            printf("no\n");        else            printf("yes\n");    }}int main(){    int num,rnum;    scanf("%d",&num);    keni(num);    getchar();    return 0;}

## 0x57 r1重新构成新数

#include <iostream> using namespace std; int main() { int a; int b; int c=0; int d=0; cin>>a; while(a!=0) { int i=0; b=a%10; if(b%2==0) c=c*10+b; else d=d*10+b; a=a/10; i++; } cout<<d<<" "<<c; return 0; }

1234567891011121314151617181920212223242526

#include <iostream> using namespace std; int main(){    int a;    int b;    int c=0;    int d=0;    cin>>a;    while(a!=0)    {        int i=0;        b=a%10;        if(b%2==0)            c=c*10+b;        else            d=d*10+b;        a=a/10;        i++;    }    cout<<d<<" "<<c;    return 0;}

## 0x58 长度单位换算

#include <iostream> using namespace std; int main() { int a; cin>>a; int b=a/(1760*3*12); if(b!=0) cout<<b<<"mi"; int c=(a-b*(1760*3*12))/(3*12); if(c!=0) cout<<c<<"yd"; int d=(a-b*(1760*3*12)-c*(3*12))/12; if(d!=0) cout<<d<<"ft"; int e=a-b*(1760*3*12)-c*(3*12)-d*12; if(e!=0) cout<<e<<"in”; return 0; }

123456789101112131415161718192021222324

#include <iostream> using namespace std; int main(){    int a;    cin>>a;    int b=a/(1760*3*12);    if(b!=0)        cout<<b<<"mi";    int c=(a-b*(1760*3*12))/(3*12);    if(c!=0)        cout<<c<<"yd";    int d=(a-b*(1760*3*12)-c*(3*12))/12;    if(d!=0)        cout<<d<<"ft";    int e=a-b*(1760*3*12)-c*(3*12)-d*12;    if(e!=0)        cout<<e<<"in”;             return 0;}

## 0x59 判断三角形

#include<iostream> #include <cmath> using namespace std; int main() { double a; double b; double c; while(cin>>a>>b>>c) { if(a+b>c&&a+c>b&&b+c>a) { if(a==b||a==c||b==c) { if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b) { cout<<"isoceles right-angled triangle"<<endl; } if(a==b&&a==c) { cout<<"equilateral triangle"<<endl; } else { cout<<"isoceles triangle"<<endl; } } else { if(fabs(a*a+b*b-c*c)<0.00001||fabs(b*b+c*c-a*a)<0.00001||fabs(a*a+c*c-b*b)<0.00001) { cout<<"right-angled triangle"<<endl; } else { cout<<"arbitrary triangle"<<endl; } } } else { cout<<"It isn’t triangle"<<endl; } } return 0; }

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849

#include<iostream>#include <cmath>using namespace std;int main(){    double a;    double b;    double c;    while(cin>>a>>b>>c)    {        if(a+b>c&&a+c>b&&b+c>a)        {            if(a==b||a==c||b==c)            {                if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b)                {                    cout<<"isoceles right-angled triangle"<<endl;                }                if(a==b&&a==c)                {                    cout<<"equilateral triangle"<<endl;                }                else                {                    cout<<"isoceles triangle"<<endl;                }            }            else            {                 if(fabs(a*a+b*b-c*c)<0.00001||fabs(b*b+c*c-a*a)<0.00001||fabs(a*a+c*c-b*b)<0.00001)                {                    cout<<"right-angled triangle"<<endl;                }                else                {                    cout<<"arbitrary triangle"<<endl;                }            }        }        else        {            cout<<"It isn’t triangle"<<endl;        }    }    return 0;}

## 0x60 数值变换

#include <iostream> using namespace std; int main() { int a,b,c,d; while(cin>>a) { if(100<=a&&a<=999) { b=a/100; c=(a-100*b)/10; d=a-100*b-10*c; cout <<d<<c<<b<<endl; } else cout <<"-1"<<endl; } return 0; }

123456789101112131415161718192021

#include <iostream>using namespace std; int main(){    int a,b,c,d;    while(cin>>a)    {        if(100<=a&&a<=999)        {            b=a/100;            c=(a-100*b)/10;            d=a-100*b-10*c;            cout <<d<<c<<b<<endl;        }        else            cout <<"-1"<<endl;    }    return 0;}

0 条评论

• ### ACM札记四

输入一个正整数n(1 <n≤10)，再输入n 个整数，将最小值与第一个数交换，最大值与最后一个数交换，然后输出交换后的n 个数｡

• ### ACM札记之一

输入n(n<=100)个整数，按照绝对值从大到小排序后输出。题目保证对于每一个测试实例，所有的数的绝对值都不相等。

• ### 算法学习之排序基础

#include <iostream> #include <algorithm> using namespace std; void selectionSo...

• ### UVA-11600-Masud Rana

ACM模版 描述 ? ? 题解 image.png ? 保存六位小数…… 代码 #include <cstdio> #include <cstring> #in...

• ### 详解斯坦纳点及斯坦纳树及模版归纳总结

①什么是斯坦纳点？ 　　假设原来已经给定了个点，库朗等指出需要引进的点数至多为，此种点称为斯坦纳点。过每一斯坦纳点，至多有三条边通过。若为三条边，则它们两两交成...

• ### 拉格朗日插值

存在性和唯一性的证明以后再补。。。。 拉格朗日插值 拉格朗日插值，emmmm，名字挺高端的:joy: 它有什么应用呢？ 我们在FFT中讲到过 设n-1次多项式为...

• ### 【CCCC天梯赛决赛】

cccc的天梯赛决赛，水题一样的水，中档题以上的还是没做出来。补了一下题，觉得其实也不是很难，主要是练的少。

• ### 学大伟业 国庆Day2

期望得分：30+100+0=130 实际得分：30+100+20=150 忍者钩爪 (ninja.pas/c/cpp) 【问题描述】 小Q是一名酷爱钩爪的忍者，...