软件学院oj1000~1049题解

1000 : Hello World

题目描述

这道超级简单的题目没有任何输入。
你只需要在一行中输出著名短句“Hello World!”就可以了。 dddd

输入

无需输入

输出

Hello World!

样例输入

样例输出

Hello World!

C

#include<stdio.h>
int main()
{
    printf("Hello World!");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

1001 : 整数a+b

题目描述

计算两个整数的和。

输入

输入两个整数,两个整数用空格隔开。

输出

输出为两个整数的和,单独占一行。

样例输入

1 1

样例输出

2

C

#include<stdio.h>
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d",a+b);
    return 0;
}

C++

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

1002 : 分铅笔

题目描述

有m支铅笔分给n位同学(且m>n),请问每位同学平均可分几支?还剩几支?

输入

输入两个整数m和n,分别表示铅笔的总数和学生的人数(m>n)。

输出

输出两个整数,以逗号分开,分别表示每位同学平均分配的数量及剩余的铅笔数量。

样例输入

163 32

样例输出

5,3

C

#include<stdio.h>
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    printf("%d,%d",m/n,m%n);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int m,n;
    cin >> m >> n;
    cout << m/n << "," << m%n << endl;
    return 0;
}

1003 : 求圆的面积

题目描述

求半径为r的圆的面积(令π=3.14)。

输入

输入一个浮点类型的数r表示圆的半径。 (1≤r≤100000)

输出

该圆的面积。(结果保留4位有效数字)

样例输入

2.3

样例输出

16.6106

C

#include<stdio.h>
#define PI 3.14
int main()
{
    double r;
    scanf("%lf",&r);
    printf("%.4lf",PI*r*r);
    return 0;
}

C++

#include<iostream>
#include<iomanip>
using namespace std;
const double PI=3.14;
int main()
{
    double r;
    cin >> r;
    cout << fixed << setprecision(4) << PI*r*r << endl;
    return 0;
}

1004 : 正整数的位数

题目描述

输入一个正整数,输出其位数。

输入

一个正整数。

输出

正整数的位数。

样例输入

123

样例输出

3

C

#include<stdio.h>
#include<math.h>
int main()
{
    int n;
    scanf("%d",&n);
    printf("%d",(int)log10(n)+1);
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cout << (int)log10(n)+1 << endl;
    return 0;
}

1005 : 英文字母的字母表位序

题目描述

输入一个小写英文字符,输出其在英文字母表中的排序。

输入

一个小写英文字符。

输出

该字符在英文字母表中的排序。

样例输入

c

样例输出

3

C

#include<stdio.h>
int main()
{
    char a;
    scanf("%c",&a);
    printf("%d",a-'a'+1);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    char a;
    cin >> a;
    cout << a-'a'+1 << endl;
    return 0;
}

1006 : 两个整数的四则运算

题目描述

输入两个整数a和b,请你设计一个程序,计算并输出它们的和、差、积、整数商及余数。

输入

输入只有两个正整数a、b。(1≤a,b≤100000)

输出

输出占一行,包括两个数的和、差、积、商及余数,数据之间用一个空格隔开。(对商取整数位)

样例输入

1 2

样例输出

3 -1 2 0 1

C

#include<stdio.h>
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d %d %d %d %d",a+b,a-b,a*b,a/b,a%b);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b;
    cout << a+b << " " << a-b << " " << a*b << " " << a/b << " " << a%b << endl;
    return 0;
}

1007 : 三位数的数位分离

题目描述

从键盘输入一个任意的三位正整数,分别求出其个位、十位和百位上的数字。

输入

输入任意的一个三位正整数。

输出

依次输出个位、十位、百位上的数字。以空格间隔,但最后一个数据的后面没有空格,直接换行。

样例输入

123

样例输出

3 2 1

C

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    printf("%d %d %d",n%10,n/10%10,n/100);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cout << n%10 << " " << n/10%10 << " " << n/100 << endl;
    return 0;
}

1008 : 压岁钱存款

题目描述

过年了,长辈们给小明发了很多压岁钱,妈妈向小明承诺如果把压岁钱存在妈妈处,则每过一年存款翻倍。

输入

两个用空格分隔的整数,分别表示小明的压岁钱存款数目m,和存在妈妈处的存款年限n。
0≤n≤20
数据保证最后存款数目在int范围内

输出

输出一个整数,代表小明能够得到的存款数目。

样例输入

100 3

样例输出

800

C

#include<stdio.h>
#include<math.h>
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    printf("%d",m*(int)pow(2,n));
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int m,n;
    cin >> m >> n;
    cout << m*(int)pow(2,n) << endl;
    return 0;
}

1009 : 等差数列求和

题目描述

给出三个整数,分别表示等差数列的第一项、最后一项和公差,求该数列的和。

输入

输入三个整数,之间用空格隔开。第1个数作为首项,第2个数作为末项,第3个数作为公差。

输出

输出占一行,包含一个整数,为该等差数列的和。

样例输入

2 11 3

样例输出

26

提示
保证输入数据 ≥ 1,并且合法
输出数据 < 1e9

C

#include<stdio.h>
int main()
{
    int m,n,d;
    scanf("%d%d%d",&m,&n,&d);
    printf("%d",(((n-m)/d+1)*(m+n))/2);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int m,n,d;
    cin >> m >> n >> d;
    cout << (((n-m)/d+1)*(m+n))/2 << endl;
    return 0;
}

1010 : 输出字符ASCII码值的2倍

题目描述

输入一个字符,将其对应的ASCII码值乘以2再输出

输入

输入一个字符。

输出

字符对应的ASCII码值*2。

样例输入

A

样例输出

130

C

#include<stdio.h>
int main()
{
    char a;
    scanf("%c",&a);
    printf("%d",a*2);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    char a;
    cin >> a;
    cout << a*2 << endl;
    return 0;
}

1011 : 虫子吃苹果

题目描述

小明买了一箱苹果共有n个,但不幸的是箱子里混进了一条虫子。虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时后这箱苹果中还有多少个苹果没有被虫子吃过?

输入

输入三个整数n、x、y,分别表示一箱苹果的个数,虫子吃完一个苹果所需时间和已经过去的时间。

输出

剩余好苹果的个数。

样例输入

3 2 1

样例输出

2

C

#include<stdio.h>
int main()
{
    int n,x,y;
    scanf("%d%d%d",&n,&x,&y);
    if(y%x==0)
        printf("%d",n-y/x);
    else
        printf("%d",n-y/x-1);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int n,x,y;
    cin >> n >> x >> y;
    if(y%x==0)
        cout << n-y/x << endl;
    else
        cout << n-y/x-1 << endl;
    return 0;
}

1012 : 三个整数的和

题目描述

求三个整数的和。

输入

输入三个整数,三个整数用空格隔开。

输出

三个整数的和。

样例输入

1234567890 1234567890 1234567890

样例输出

3703703670

提示

注意数据范围可能会超过int,建议改用long long

C

#include<stdio.h>
int main()
{
    long long a,b,c;
    scanf("%lld%lld%lld",&a,&b,&c);
    printf("%lld",a+b+c);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    long long a,b,c;
    cin >> a >> b >> c;
    cout << a+b+c << endl;
    return 0;
}

1013 : 身份证求出生日期

题目描述

输入一个公民身份证号,输出该公民的出生年月日。

输入

公民身份证号

输出

年月日(YYYY-MM-DD)

样例输入

430622197811204019

样例输出

1978-11-20

C

#include<stdio.h>
int main()
{
    int year,month,day;
    scanf("%*6d%4d%2d%2d%*4d",&year,&month,&day);
    printf("%04d-%02d-%02d",year,month,day);
    return 0;
}

C++

#include<cstdio>
int main()
{
    int year,month,day;
    scanf("%*6d%4d%2d%2d%*4d",&year,&month,&day);
    printf("%04d-%02d-%02d",year,month,day);
    return 0;
}

1014 : 鸡兔同笼

题目描述

鸡兔同笼,共有n个头,m条腿,求鸡和兔子各有多少只。

输入

输入头数和腿数。

输出

鸡和兔子的只数。

样例输入

35 94

样例输出

23 12

提示

数据保证在int范围内

C

#include<stdio.h>
int main()
{
    int n,m,chicken,rabbit;
    scanf("%d%d",&n,&m);
    chicken=(n*4-m)/2;
    rabbit=n-chicken;
    printf("%d %d",chicken,rabbit);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int n,m,chicken,rabbit;
    cin >> n >> m;
    chicken=(n*4-m)/2;
    rabbit=n-chicken;
    cout << chicken << " " << rabbit << endl;
    return 0;
}

1015 : 计算时间

题目描述

小明的家距离学校很远,小明想知道从家里出发到学校耗费了多少时间。

输入

输入用空格隔开的四个整数,分别代表从家出发的时、分和到校的时、分。

输出

输出用空格隔开的两个整数,代表总共花了多少小时多少分钟。

样例输入

12 45 13 56

样例输出

1 11

C

#include<stdio.h>
int main()
{
    int hour1,minute1,hour2,minute2,hour,minute;
    scanf("%d%d%d%d",&hour1,&minute1,&hour2,&minute2);
    if(minute1<=minute2)
    {
        hour=hour2-hour1;
        minute=minute2-minute1;
    }
    else
    {
        hour=hour2-hour1-1;
        minute=60+minute2-minute1;
    }
    printf("%d %d",hour,minute);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int hour1,minute1,hour2,minute2,hour,minute;
    cin >> hour1 >> minute1 >> hour2 >> minute2;
    if(minute1<=minute2)
    {
        hour=hour2-hour1;
        minute=minute2-minute1;
    }
    else
    {
        hour=hour2-hour1-1;
        minute=60+minute2-minute1;
    }
    cout << hour << " " << minute << endl;
    return 0;
}

1016 : 计算国民生产总值增长倍数

题目描述

假如我国国民生产总值的年增长率为7%,计算10年后我国国民生产总值与现在相比增长多少百分比(倍数)。计算公式为p=(1+r)n
其中:r为年增长率,n为年数,p为与现在相比的倍数
对于求an, C语言的数学库函数中有求指数的函数pow(a, n)

输入

无,增长率与年数以赋值的方式给出

输出

p=增长倍数,结果保留两位小数,注意末尾的换行

样例输入

样例输出

p=1.97

C

#include<stdio.h>
#include<math.h>
int main()
{
    double r,n,p;
    r=0.07,n=10;
    p=pow(1+r,n);
    printf("p=%.2lf",p);
    return 0;
}

C++

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    double r,n,p;
    r=0.07,n=10;
    p=pow(1+r,n);
    cout << "p=" << fixed << setprecision(2) << p << endl;
    return 0;
}

1017 : 计算贷款还款时间

题目描述

某人为购房,从银行贷款30万元(d),准备按月还款6000元(p),月利率为1%(r),计算还款月数(m,即多少月还清贷款)。对求得的月份取小数点后一位(四舍五入)。
计算还清贷款月数m的计算公式如下:
在这里插入图片描述

可以将公式改写为
在这里插入图片描述

C语言数学库函数中有求对数的函数,log10,是求以10为底的对数,log10§表示以10为底p的对数。

输入

输出

m=计算出的贷款还款月数,精确到小数点后1位,注意末尾的换行。

样例输入

样例输出

m=69.7

C

#include<stdio.h>
#include<math.h>
int main()
{
    double d,p,r,m;
    d=3e5,p=6e3,r=0.01;
    m=log10(p/(p-d*r))/log10(1+r);
    printf("m=%.1lf",m);
    return 0;
}

C++

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    double d,p,r,m;
    d=3e5,p=6e3,r=0.01;
    m=log10(p/(p-d*r))/log10(1+r);
    cout << "m=" << fixed << setprecision(1) << m << endl;
    return 0;
}

1018 : 交换变量

题目描述

编写程序,从键盘输入两个整数给变量x和y输出x和y。
在交换x和y中的值后,再输出x和y,验证两个变量中的值是否正确的进行了交换。

输入

1 2

输出

2 1

样例输入

1 2

样例输出

2 1

C

#include<stdio.h>
int main()
{
    int x,y,z;
    scanf("%d%d",&x,&y);
    z=x,x=y,y=z;
    printf("%d %d",x,y);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cin >> x >> y;
    swap(x,y);
    cout << x << " " << y << endl;
    return 0;
}

1019 : 译密码(一)

题目描述

写程序将字符串“China”译成密码,密码规律是:用原来的字母后面第四个字母代替原来的字母。例如,字母A后面第四个字母是E,用E代替A。因此,China应译为Glmre。
请编写一个程序,用赋初值的方法使c1, c2, c3, c4, c5这5个字符变量的值分别为’C’, ‘h’, ‘i’, ‘n’, ‘a’,经过运算,使c1, c2, c3, c4, c5的值分别变为’G’, ‘l’, ‘m’, ‘r’, ‘e’
用printf函数输出这5个字符

输入

输出

Glmre,注意末尾的换行

样例输入

样例输出

Glmre

C

#include<stdio.h>
int main()
{
    char c1='C',c2='h',c3='i',c4='n',c5='a';
    c1+=4,c2+=4,c3+=4,c4+=4,c5+=4;
    printf("%c%c%c%c%c",c1,c2,c3,c4,c5);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    char c1='C',c2='h',c3='i',c4='n',c5='a';
    c1+=4,c2+=4,c3+=4,c4+=4,c5+=4;
    cout << c1 << c2 << c3 << c4 << c5 << endl;
    return 0;
}

1020 : 从小到大输出三个整数

题目描述

输入三个整数,按从小到大的顺序输出这三个数。

输入

三个整数。

输出

从小到大输出三个整数,以空格分隔。

样例输入

2 9 3

样例输出

2 3 9

C

#include<stdio.h>
int main()
{
    int a,b,c,t;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
    {
        t=a;
        a=b;
        b=t;
    }
    if(b>c)
    {
        t=b;
        b=c;
        c=t;
    }
    if(a>b)
    {
        t=a;
        a=b;
        b=t;
    }
    printf("%d %d %d",a,b,c);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int a,b,c,t;
    cin >> a >> b >> c;
    if(a>b)
        t=a,a=b,b=t;
    if(b>c)
        t=b,b=c,c=t;
    if(a>b)
        t=a,a=b,b=t;
    cout << a << " " << b << " " << c << endl;
    return 0;
}

1021 : positive, negative, or zero

题目描述

输入一个整数,判断该数是正数、负数还是零。

输入

一个整数。

输出

该数为正数则输出“positive”,负数则输出“negative”,零则输出“zero”。

样例输入

3

样例输出

positive

C

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    if(n>0)
        printf("positive");
    if(n<0)
        printf("negative");
    if(n==0)
        printf("zero");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if(n>0)
        cout << "positive" << endl;
    if(n<0)
        cout << "negative" << endl;
    if(n==0)
        cout << "zero" << endl;
    return 0;
}

1022 : 龟兔赛跑

题目描述

龟兔赛跑中,已知乌龟速度为a米每秒,兔子速度为b米每秒,龟兔赛跑的赛程总长度为s米,兔子在比赛中到达终点前的某一时刻睡着了(且仅睡着这一次),耽误了t秒的时间,如果乌龟赢了输出"Turtle win",兔子赢了输出"Rabbit win",否则输出"Tie"。

输入

输入四个用空格隔开的整数,分别代表a,b,s,t。

输出

如果乌龟赢了输出"Turtle win",兔子赢了输出"Rabbit win",否则输出"Tie"。

样例输入

3 6 12 2

样例输出

Tie

C

#include<stdio.h>
int main()
{
    int a,b,s,t;
    double t1,t2;
    scanf("%d%d%d%d",&a,&b,&s,&t);
    t1=1.0*s/a;
    t2=1.0*s/b+t;
    if(t1<t2)
        printf("Turtle win");
    if(t1>t2)
        printf("Rabbit win");
    if(t1==t2)
        printf("Tie");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int a,b,s,t;
    double t1,t2;
    cin >> a >> b >> s >> t;
    t1=1.0*s/a;
    t2=1.0*s/b+t;
    if(t1<t2)
        cout << "Turtle win" << endl;
    if(t1>t2)
        cout << "Rabbit win" << endl;
    if(t1==t2)
        cout << "Tie" << endl;
    return 0;
}

1023 : 一元二次方程求解

题目描述

输入a、b、c,求一元二次方程ax2+bx+c=0的解。

输入

三个整数a、b、c。

输出

方程ax2+bx+c=0的解(保留两位精度),a=0或者该方程没有解 则输出“This is not a quadratic equation”。
若有解输出解,要求先输出小的解,如果只有一个解则只输出一个解

样例输入

1 -2 1

样例输出

1.00

提示

输入样例2: 3 2 -1 输出:-1.00 0.33

建议用double进行小数的运算

C

#include<stdio.h>
#include<math.h>
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    if(a==0) printf("This is not a quadratic equation");
    else
    {
        double deta;
        deta=b*b-4*a*c;
        if(deta==0) printf("%.2lf",-2.0*a/b);
        else if(deta>0)
        {
            double x1,x2;
            x1=(-1.0*b-sqrt(deta))/(2.0*a);
            x2=(-1.0*b+sqrt(deta))/(2.0*a);
            if(x1<x2) printf("%.2lf %.2lf",x1,x2);
            else printf("%.2lf %.2lf",x2,x1);
        }
        else printf("This is not a quadratic equation");
    }
    return 0;
}

C++

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    if(a==0) cout << "This is not a quadratic equation" << endl;
    else
    {
        double deta;
        deta=b*b-4*a*c;
        if(deta==0) cout << fixed << setprecision(2) << -2.0*a/b <<endl;
        else if(deta>0)
        {
            double x1,x2;
            x1=(-1.0*b-sqrt(deta))/(2.0*a);
            x2=(-1.0*b+sqrt(deta))/(2.0*a);
            if(x1<x2) cout << fixed <<setprecision(2) << x1 << " " << x2 << endl;
            else cout << fixed <<setprecision(2) << x2 << " " << x1 << endl;
        }
        else cout << "This is not a quadratic equation" << endl;
    }
    return 0;
}

1024 : 奇偶判断

题目描述

输入一个整数,判断该数的奇偶性(“odd”或“even”)。

输入

一个整数。

输出

奇数则输出“odd”,偶数则输出“even”。

样例输入

7

样例输出

odd

C

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    if(n%2==0) printf("even");
    else printf("odd");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if(n%2==0) cout << "even" << endl;
    else cout << "odd" << endl;
    return 0;
}

1025 : 是否闰年

题目描述

输入年份year1,判断该年是否为闰年。

输入

输入一个整数代表年份。

输出

若该年是闰年则输出“year1 is a leap year”,否则输出“year1 is not a leap year”。

样例输入

2021

样例输出

2021 is not a leap year

C

#include<stdio.h>
int main()
{
    int year;
    scanf("%d",&year);
    if(year%4==0&&year%100!=0||year%400==0)
        printf("%d is a leap year",year);
    else
        printf("%d is not a leap year",year);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int year;
    cin >> year;
    if(year%4==0&&year%100!=0||year%400==0)
        cout << year <<" is a leap year" << endl;
    else
        cout << year << " is not a leap year" << endl;
    return 0;
}

1026 : 两个整数中的较大者

题目描述

输入两个整数,输出其中的较大数。

输入

两个整数。

输出

两个整数中的较大数。

样例输入

6 15

样例输出

15

C

#include<stdio.h>
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    if(a>b) printf("%d",a);
    else printf("%d",b);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b;
    if(a>b) cout << a << endl;
    else cout << b << endl;
    return 0;
}

1027 : 成绩等级

题目描述

给定一个百分制成绩, 请根据百分制成绩输出其对应的等级。转换关系如下:90分及以上为’A’,80~89为’B’, 70~79为’C’, 60~69为’D’,60分以下为’E’。

输入

一个百分制成绩(0~100的整数)。

输出

成绩对应的等级。

样例输入

90

样例输出

A

C

#include<stdio.h>
int main()
{
    int score;
    scanf("%d",&score);
    if(score>=90) printf("A");
    else if(score>=80&&score<90) printf("B");
    else if(score>=70&&score<80) printf("C");
    else if(score>=60&&score<70) printf("D");
    else printf("E");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int score;
    cin >> score;
    if(score>=90) cout << "A" << endl;
    else if(score>=80&&score<90) cout << "B" << endl;
    else if(score>=70&&score<80) cout << "C" << endl;
    else if(score>=60&&score<70) cout << "D" << endl;
    else cout << "E" << endl;
    return 0;
}

1028 : 向0舍入

题目描述

输入一个双精度浮点数,将其向0舍入到整数。向0舍入的含义是“正数向下舍入,负数向上舍入”。

输入

输入一个双精度浮点数。

输出

该双精度浮点数向0舍入的整数。

样例输入

3.14

样例输出

3

C

#include<stdio.h>
int main()
{
    double n;
    scanf("%lf",&n);
    printf("%d",(int)n);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    double n;
    cin >> n;
    cout << (int)n << endl;
    return 0;
}

1029 : 求最大数与最小数

题目描述

输入三个整数,输出最大数和最小数。

输入

输入三个整数a、b、c。

输出

三个数中的最大数和最小数(以空格分隔)。

样例输入

3 7 9

样例输出

9 3

C

#include<stdio.h>
int main()
{
    int a,b,c,max,min;
    scanf("%d%d%d",&a,&b,&c);
    max=a>b?a:b;
    min=a<b?a:b;
    if(c>max) max=c;
    if(c<min) min=c;
    printf("%d %d",max,min);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int a,b,c,max,min;
    cin >> a >> b >> c;
    max=a>b?a:b;
    min=a<b?a:b;
    if(c>max) max=c;
    if(c<min) min=c;
    cout << max << " " << min << endl;
    return 0;
}

1030 : 四则运算

题目描述

输入运算数和四则运算符,输出计算结果。

输入

输入两个实数和一个操作符。

输出

输出计算结果(精度为2,遵循四舍五入)

样例输入

2.3 5.6 +

样例输出

7.90

提示

保证数据都在整型大小范围内

C

#include<stdio.h>
int main()
{
    double a,b;
    char op;
    scanf("%lf %lf %c",&a,&b,&op);
    switch(op)
    {
        case '+':printf("%.2lf",a+b);break;
        case '-':printf("%.2lf",a-b);break;
        case '*':printf("%.2lf",a*b);break;
        case '/':printf("%.2lf",a/b);break;
    }
    return 0;
}

C++

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double a,b;
    char op;
    cin >> a >> b;
    getchar();
    cin >> op;
    switch(op)
    {
        case '+':cout << fixed << setprecision(2) << a+b << endl;break;
        case '-':cout << fixed << setprecision(2) << a-b << endl;break;
        case '*':cout << fixed << setprecision(2) << a*b << endl;break;
        case '/':cout << fixed << setprecision(2) << a/b << endl;break;
    }
    return 0;
}

1031 : 国庆促销

题目描述

商场国庆促销,购物500元以下无折扣;购物500元(含)以上95折;购物1000元(含)以上9折;购物3000元(含)以上85折;购物5000元(含)以上8折。根据消费金额,计算用户实际需要支付的数目。

输入

输入一个实数,表示消费金额。

输出

输出一个实数,表示用户实际需要支出的数目,保留两位小数。

样例输入

6000

样例输出

4800.00

C

#include<stdio.h>
int main()
{
    double n;
    scanf("%lf",&n);
    if(n<500) printf("%.2lf",n);
    else if(n>=500&&n<1000) printf("%.2lf",n*0.95);
    else if(n>=1000&&n<3000) printf("%.2lf",n*0.9);
    else if(n>=3000&&n<5000) printf("%.2lf",n*0.85);
    else printf("%.2lf",n*0.8);
    return 0;
}

C++

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double n;
    cin >> n;
    if(n<500) cout << fixed << setprecision(2) << n << endl;
    else if(n>=500&&n<1000) cout << fixed << setprecision(2) << n*0.95 << endl;
    else if(n>=1000&&n<3000) cout << fixed << setprecision(2) << n*0.9 << endl;
    else if(n>=3000&&n<5000) cout << fixed << setprecision(2) << n*0.85 << endl;
    else cout << fixed << setprecision(2) << n*0.8 << endl;
    return 0;
}

1032 : 求平方根

题目描述

从键盘输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。
要求在输入数据后先对其进行检查是否为小于1000的正数。若不是,则输出一行错误信息,信息内容为:“invalid data!”

输入

一个小于1000的正数

输出

输出数据的平方根,仅输出整数部分。
注意末尾的换行。

样例输入

108.7

样例输出

10

C

#include<stdio.h>
#include<math.h>
int main()
{
    double n;
    scanf("%lf",&n);
    if(n>0&&n<1000) printf("%d",(int)sqrt(n));
    else printf("invalid data!");
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double n;
    cin >> n;
    if(n>0&&n<1000) cout << (int)sqrt(n) << endl;
    else cout << "invalid data!" << endl;
    return 0;
}

1033 : 分段函数求值

题目描述

有一个函数y=f(x)的表达式如下:
当x<1时,y=x
当1<=x<10时,y=2x-1
当x>=10时,y=3x-11
输入x的值,输出y相应的值。
末尾换行

输入

一个实数

输出

y=计算的结果,保留2位小数,注意末尾换行。

样例输入

5.1

样例输出

y=9.20

C

#include<stdio.h>
int main()
{
    double x;
    scanf("%lf",&x);
    if(x<1) printf("y=%.2lf\n",x);
    else if(x>=1&&x<10) printf("y=%.2lf\n",2*x-1);
    else printf("y=%.2lf\n",3*x-11);
    return 0;
}

C++

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double x;
    cin >> x;
    if(x<1) cout << "y=" << fixed << setprecision(2) << x << endl;
    else if(x>=1&&x<10) cout << "y=" << fixed << setprecision(2) << 2*x-1 << endl;
    else cout << "y=" << fixed << setprecision(2) << 3*x-11 << endl;
    return 0;
}

1034 : 求建筑物高度

题目描述

有4个圆塔,圆心分别为(2, 2)、(-2, 2)、(-2, -2)、(2, -2),圆半径为1,如下图所示。这4个塔的高度为10米(m),塔以外无建筑物。
在这里插入图片描述

现输入任一点的坐标,求该点的建筑物高度(塔外的高度为零)。

输入

输入以圆括号括起来的两个数字,两个数字之间以逗号(,)分隔,格式如:(x,y)
如下为正确输入:
(1.5,1.9)
浮点型变量,请定义为float类型

输出

该建筑物的高度,末尾换行

样例输入

(1.8,1.9)

样例输出

10

提示

根据输入的点的坐标,判断该点在圆内,还是在圆外。在圆内的话,建筑物的高度就是10;否则,建筑物的高度就是0。

C

#include<stdio.h>
#include<math.h>
int main()
{
    float x,y;
    scanf("(%f,%f)",&x,&y);
    if(sqrt((x-2)*(x-2)+(y-2)*(y-2))<=1) printf("10");
    else if(sqrt((x+2)*(x+2)+(y-2)*(y-2))<=1) printf("10");
    else if(sqrt((x-2)*(x-2)+(y+2)*(y+2))<=1) printf("10");
    else if(sqrt((x+2)*(x+2)+(y+2)*(y+2))<=1) printf("10");
    else printf("0");
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float x,y;
    scanf("(%f,%f)",&x,&y);
    if(sqrt((x-2)*(x-2)+(y-2)*(y-2))<=1) cout << 10 << endl;
    else if(sqrt((x+2)*(x+2)+(y-2)*(y-2))<=1) cout << 10 << endl;
    else if(sqrt((x-2)*(x-2)+(y+2)*(y+2))<=1) cout << 10 << endl;
    else if(sqrt((x+2)*(x+2)+(y+2)*(y+2))<=1) cout << 10 << endl;
    else cout << 0 << endl;
    return 0;
}

1035 : 字母排序

题目描述

输入三个字母,按字母表顺序输出这三个字母。

输入

三个字母

输出

按字母表顺序输出

样例输入

cba

样例输出

abc

C

#include<stdio.h>
int main()
{
    char a,b,c,t;
    scanf("%c%c%c",&a,&b,&c);
    if(a>b) t=a,a=b,b=t;
    if(a>c) t=a,a=c,c=t;
    if(b>c) t=b,b=c,c=t;
    printf("%c%c%c",a,b,c);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    char a,b,c,t;
    cin >> a >> b >> c;
    if(a>b) t=a,a=b,b=t;
    if(a>c) t=a,a=c,c=t;
    if(b>c) t=b,b=c,c=t;
    cout << a << b << c << endl;
    return 0;
}

1036 : 加班费

题目描述

编写一个计算员工收入的程序,公司按照规定工时的工资10元/小时付给每个员工160个工时的薪水,按3倍的工资率付给160个工时以外的工资。

输入

输入员工的工时数,1个整数。

输出

计算员工的收入

样例输入

20

样例输出

200

C

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    if(n<=160) printf("%d",n*10);
    else printf("%d",1600+(n-160)*30);
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if(n<=160) cout << n*10 << endl;
    else cout << 1600+(n-160)*30 << endl;
    return 0;
}

1037 : 某年某月的天数

题目描述

输入x和y,输出x年y月有多少天。

输入

一行两个正整数x和y,分别表示年份和月份。x在int范围以内,y为1~12。

输出

一行一个整数,表示该年该月有多少天。

样例输入

2021 3

样例输出

31

C

#include<stdio.h>
int main()
{
    int x,y;
    scanf("%d%d",&x,&y);
    if(y==1||y==3||y==5||y==7||y==8||y==10||y==12) printf("31");
    else if(y==2)
    {
        if(x%4==0&&x%100!=0||x%400==0) printf("29");
        else printf("28");
    }
    else printf("30");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cin >> x >> y;
    if(y==1||y==3||y==5||y==7||y==8||y==10||y==12) cout << 31 << endl;
    else if(y==2)
    {
        if(x%4==0&&x%100!=0||x%400==0) cout << 29 << endl;
        else cout << 28 << endl;
    }
    else cout << 30 << endl;
    return 0;
}

1038 : 蚂蚁的位置

题目描述

有一只蚂蚁在一个圆上爬行,圆心坐标是(0,0),半径r=4.5,任意输入蚂蚁在圆上的坐标(x,y),判断这只蚂蚁是在圆内,圆周上,还是在圆外。

输入

两个浮点数x,y

输出
如果在圆内,输出in
如果在圆外,输出out
如果在圆上,输出on

样例输入

1.0 1.0

样例输出

in

C

#include<stdio.h>
#include<math.h>
int main()
{
    double x,y,d;
    scanf("%lf%lf",&x,&y);
    d=sqrt(x*x+y*y);
    if(d<4.5) printf("in");
    else if(d==4.5) printf("on");
    else printf("out");
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double x,y,d;
    cin >> x >> y;
    d=sqrt(x*x+y*y);
    if(d<4.5) cout << "in" << endl;
    else if(d==4.5) cout << "on" << endl;
    else cout << "out" << endl;
    return 0;
}

1039 : 吃水果

题目描述
妈妈去超市买水果,她问小明想吃什么水果,现在超市只有五种水果,分别是Apple 苹果,Banana 香蕉,Cherry 樱桃,Durian 榴莲,Mango 芒果。如果小明说’A’,就是想吃Apple,如果小明说’B’,就是想吃Banana,如果小明说’C’,就是想吃Cherry ,如果小明说’D’,就是想吃Durian,如果小明随便说其它字母,妈妈就买Mango。
请采用switch语句实现。

输入

水果的英文单词首字母的大小形式

输出

水果的英文单词
样例输入

A

样例输出

Apple

C

#include<stdio.h>
int main()
{
    char ch;
    scanf("%c",&ch);
    switch(ch)
    {
        case 'A':printf("Apple");break;
        case 'B':printf("Banana");break;
        case 'C':printf("Cherry");break;
        case 'D':printf("Durian");break;
        default :printf("Mango");
    }
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cin >> ch;
    switch(ch)
    {
        case 'A':cout << "Apple" <<endl;break;
        case 'B':cout << "Banana" <<endl;break;
        case 'C':cout << "Cherry" <<endl;break;
        case 'D':cout << "Durian" <<endl;break;
        default :cout << "Mango" <<endl;
    }
    return 0;
}

1040 : 求m+(m+1)+…+n

题目描述

求m+(m+1)+…+n。

输入

两个正整数m和n(m<=n)。

输出

m加到n的和。
样例输入

1 3

样例输出

6

C

#include<stdio.h>
int main()
{
    int m,n,i,sum=0;
    scanf("%d%d",&m,&n);
    for(i=m;i<=n;i++)
        sum+=i;
    printf("%d",sum);
    return 0;
}

C++

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

1041 : 多用例测试

题目描述

输入多组整数,每组数据包含两个整数a和b,对每组数据输出a+b的结果。

输入

多组整数,每组一行,为两个以空格分隔的整数。

输出

对每组数据输出a+b的结果,每组结果占一行。

样例输入

1 2
3 4

样例输出

3
7

提示

保证结果都在整型范围内,并且数据个数 ≤ 100

C

#include<stdio.h>
int main()
{
    int a,b;
    while(~scanf("%d%d",&a,&b))
        printf("%d\n",a+b);
    return 0;
}

C++

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

1042 : 求使1+2+…+i>=n成立的最小整数 i

题目描述

输入正整数n,求使1+2+…+i>=n成立的最小整数i。
输入

一个整数n。

输出

使1+2+…+i>=n成立的最小整数i。

样例输入

123

样例输出

16

C

#include<stdio.h>
int main()
{
    int n,i,sum=0;
    scanf("%d",&n);
    for(i=1;;i++)
    {
        sum+=i;
        if(sum>=n)
        {
            printf("%d",i);
            break;
        }
    }
    return 0;
}

C++

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

1043 : 质数判断

题目描述

输入正整数n,判定它是否为素数(prime,又称质数)。

输入

一个正整数n。

输出

若n为质数则输出“Yes”,否则输出“No”。
样例输入

5

样例输出

Yes

提示

n 在整型范围内

C

#include<stdio.h>
#include<math.h>
int main()
{
    int n,i,f=1;
    scanf("%d",&n);
    for(i=2;i<=(int)sqrt(n);i++)
    {
        if(n%i==0)
        {
            f=0;
            break;
        }
    }
    if(n==1||f==0) printf("No");
    else printf("Yes");
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,f=1;
    cin >> n;
    for(int i=2;i<=(int)sqrt(n);i++)
    {
        if(n%i==0)
        {
            f=0;
            break;
        }
    }
    if(n==1||f==0) cout << "No" << endl;
    else cout << "Yes" << endl;
    return 0;
}

1044 : 求1-2/3+3/5-4/7+5/9-6/11+…的前n项和

题目描述

输入正整数n,求1-2/3+3/5-4/7+5/9-6/11+…的前n项和,结果保留3位小数。

输入

一个正整数n。(n<100)

输出

求1-2/3+3/5-4/7+5/9-6/11+…的前n项和。

样例输入

100

样例输出

0.391

C

#include<stdio.h>
int main()
{
    int n,i;
    double sum=0,f=1.0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        sum+=f*i/(2*i-1);
        f=-f;
    }
    printf("%.3lf",sum);
    return 0;
}

C++

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n;
    cin >> n;
    double sum=0,f=1.0;
    for(int i=1;i<=n;i++)
    {
        sum+=f*i/(2*i-1);
        f=-f;
    }
    cout << fixed << setprecision(3) << sum << endl;
    return 0;
}

1045 : 质数判断(使用break语句)

题目描述

输入正整数n,判定它是否为素数(prime,又称质数)。

输入

一个正整数n。

输出

若n为质数则输出“Yes”,否则输出“No”。

样例输入

7

样例输出

Yes

C

#include<stdio.h>
#include<math.h>
int main()
{
    int n,i,f=1;
    scanf("%d",&n);
    for(i=2;i<=(int)sqrt(n);i++)
    {
        if(n%i==0)
        {
            f=0;
            break;
        }
    }
    if(n==1||f==0) printf("No");
    else printf("Yes");
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,f=1;
    cin >> n;
    for(int i=2;i<=(int)sqrt(n);i++)
    {
        if(n%i==0)
        {
            f=0;
            break;
        }
    }
    if(n==1||f==0) cout << "No" << endl;
    else cout << "Yes" << endl;
    return 0;
}

1046 : 输出a到b之间的不能被3整除的整数

题目描述

输出a到b之间的不能被3整除的整数。

输入

两个正整数a、b。

输出

a到b之间的不能被3整除的整数,以空格分隔。

样例输入

1 10

样例输出

1 2 4 5 7 8 10

C

#include<stdio.h>
int main()
{
    int a,b,i;
    scanf("%d%d",&a,&b);
    for(i=a;i<=b;i++)
    {
        if(i%3!=0) printf("%d ",i);
    }
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b;
    for(int i=a;i<=b;i++)
    {
        if(i%3!=0) cout << i << " ";
    }
    return 0;
}

1047 : 百文买百鸡

题目描述

公鸡五文钱一只,母鸡三文钱一只,小鸡一文钱三只,用m文钱买m只鸡,公鸡、母鸡、小鸡各买多少只?

输入

正整数m。

输出

只输出一个解,即公鸡数量最少的那个解(输出公鸡、母鸡和小鸡的只数,以空格隔开)。

样例输入

100

样例输出

0 25 75

提示

m ≤ 900

C

#include<stdio.h>
int main()
{
    int m,x,y,z,f=0;
    scanf("%d",&m);
    for(x=0;x<=m/5;x++)
    {
        for(y=0;y<=m/3;y++)
        {
            z=m-x-y;
            if(15*x+9*y+z==m*3)
            {
                printf("%d %d %d",x,y,z);
                f=1;
                break;
            }
        }
        if(f==1) break;
    }
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int m;
    cin >> m;
    int x,y,z,f=0;
    for(x=0;x<=m/5;x++)
    {
        for(y=0;y<=m/3;y++)
        {
            z=m-x-y;
            if(15*x+9*y+z==m*3)
            {
                cout << x << " " << y << " " << z << endl;
                f=1;
                break;
            }
        }
        if(f==1) break;
    }
    return 0;
}

1048 : 求a到b之间存在多少个素数

题目描述

求a到b之间存在多少个素数。

输入

两个正整数a、b。

输出

a到b之间的全部素数的个数。

样例输入

100 200

样例输出

21

提示

0≤a≤b≤100000

C

#include<stdio.h>
#include<math.h>
int main()
{
    int a,b,i,j,f,num=0;
    scanf("%d%d",&a,&b);
    for(i=a;i<=b;i++)
    {
        f=1;
        if(i==1) f==0;
        for(j=2;j<=(int)sqrt(i);j++)
        {
            if(i%j==0)
            {
                f=0;
                break;
            }
        }
        if(f==1) num++; 
    }
    printf("%d",num);
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b;
    int num=0;
    for(int i=a;i<=b;i++)
    {
        int f=1;
        if(i==1) f==0;
        for(int j=2;j<=(int)sqrt(i);j++)
        {
            if(i%j==0)
            {
                f=0;
                break;
            }
        }
        if(f==1) num++; 
    }
    cout << num << endl;
    return 0;
}

1049 : 百文买百鸡(增加无解输出)

题目描述

公鸡五文钱一只,母鸡三文钱一只,小鸡一文钱三只,用m文钱买m只鸡,公鸡、母鸡、小鸡各买多少只?

输入

一个正整数m。
输出

若有解只输出一个解,即公鸡数量最少的那个解;若无解输出“No answer”。

样例输入

100

样例输出

0 25 75

提示

m≤100000

C

#include<stdio.h>
int main()
{
    int m,x,y,z,f=0;
    scanf("%d",&m);
    for(x=0;x<=m/5;x++)
    {
        for(y=0;y<=m/3;y++)
        {
            z=m-x-y;
            if(15*x+9*y+z==m*3)
            {
                printf("%d %d %d",x,y,z);
                f=1;
                break;
            }
        }
        if(f==1) break;
    }
    if(f==0) printf("No answer");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int m;
    cin >> m;
    int x,y,z,f=0;
    for(x=0;x<=m/5;x++)
    {
        for(y=0;y<=m/3;y++)
        {
            z=m-x-y;
            if(15*x+9*y+z==m*3)
            {
                cout << x << " " << y << " " << z << endl;
                f=1;
                break;
            }
        }
        if(f==1) break;
    }
    if(f==0) cout <<"No answer" << endl;
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值