admin 管理员组

文章数量: 1086019


2024年4月22日发(作者:编程random是什么意思)

数学11—1 C语言平时训练题

1、算术基本运算

Description

计算两整数x和y(0〈x,y<1000)的和、差、积、商、余数、x的平方和y的三次方。

Input

输入只有一行.

Output

输出为多行,按顺序每行输出x,y的和、差、积、商、余数、x的平方和y的三次方。

Sample Input

x = 11, y = 3

Sample Output

x + y : 14

x - y : 8

x * y : 33

x / y quotient: 3, remainder: 2

x ^ 2 : 121

y ^ 3 : 27

Answer

#include 〈stdio.h>

int main()

{

int x,y,a,b,c,d,e,f,g;

0

scanf(”x = %d, y = %d”,&x,&y);

a=x+y;

b=x-y;

c=x*y;

d=x/y;

e=x%y;

f=x*x;

g=y*y*y;

printf(”x + y : %dn”,a);

printf("x - y : %dn",b);

printf("x * y : %dn”,c);

printf("x / y quotient: %d, remainder: %dn”,d,e);

printf(”x ^ 2 : %dn”,f);

printf(”y ^ 3 : %dn”,g);

return 0;

2、求圆的面积和周长

Description

从键盘输入圆的半径,求圆的面积和周长,圆周率取3.14。

Input

输入一个浮点型数据,有效数字不会超过十进制的6位。

Output

输出为两行。

第一行为圆的面积,第二行为圆的周长,格式见sample。

Sample Input

3

Sample Output

Area: 28。260000

Perimeter: 18.840000

Answer

#include〈stdio。h〉

#define PI 3.14

int main()

{

float r,s,c;

scanf("%f”,&r);

s=PI*r*r;

c=2*PI*r;

printf("Area: %fn",s);

printf(”Perimeter: %fn”,c);

return 0;

}

3、 平均值

Description

求3个数的平均值。

Input

输入只有一行,为3个较小的整数.

Output

输出为这3个整数的平均值,保留3位小数。

Sample Input

1 2 3

Sample Output

2。000

Answer

#include 〈stdio.h>

int main()

int a,b,c;

float d;

scanf(”%d %d %d",&a,&b,&c);

d=(a+b+c)/3。0;

printf("%.3fn",d);

return 0;

}

4、货币兑换

Description

给出人民币对美元、欧元、日元的当日汇率,求给定金额的人民币能兑换成外币的金额,求给定金额的外币

能兑换成人民币的金额。

要计算的外币有三种:美元、欧元、日元。

Input

输入有三行。

第一行依次为美元、欧元、日元外币汇率,用空格分开.汇率用100外币为单位,精确到小数点后4位,如

668.5200表示“100美元=668.5200人民币”。汇率浮动范围为(0,10000).

第二行为外币金额x,第三行为人民币金额y。x,y均为整数,且0〈x,y<10000。

Output

输出为两行。

第一行为金额为x的美元、欧元、日元兑换成人民币的金额,用空格分开。

第二行为金额为y的人民币兑换成美元、欧元、日元的金额,用空格分开。

所有金额精确到小数点后两位。

Sample Input

668。5200 908。0685 7。9852

1500

1500

Sample Output

10027.80 13621.03 119。78

224。38 165。19 18784.75

Answer

#include

int main()

{

double x,y,a,b,c,d,e,f,g,h,i;

scanf("%lf%lf%lf”,&a,&b,&c);

scanf(”%lf”,&x);

scanf(”%lf”,&y);

d=x/100*a;

e=x/100*b;

f=x/100*c;

g=y/a*100;

h=y/b*100;

i=y/c*100;

printf("%.2lf %.2lf %。2lfn",d,e,f);

printf("%。2lf %。2lf %。2lfn",g,h,i);

return 0;

5、 求字符的值

Description

从键盘输入3个字符(不含双字节字符),分别输出每个字符的十进制值(ASCII码)、八进制值和十六进制

值。

Input

输入为3个字符。

Output

输出为3行。

每一行为每个字符(对应输入顺序)的十进制、八进制和十六进制值,用空格分隔开.每个输出的值占3个

字符,不足3个字符前面补0。

Sample Input

0 A

Sample Output

048 060 030

032 040 020

065 101 041

Answer

#include

int main()

{

char a,b,c;

scanf(”%c%c%c",&a,&b,&c);

printf(”%。3d %。3o %。3xn",a,a,a);

printf("%。3d %。3o %.3xn”,b,b,b);

printf("%。3d %。3o %.3xn",c,c,c);

return 0;

6、 奇数还是偶数?

Description

输入一个整数,判读它是奇数还是偶数.

Input

输入只有一行,为一个100以内的正整数。

Output

输出为一行。

若输入为偶数则输出“even",奇数输出“odd”。

Sample Input

30

Sample Output

even

Answer

#include

int main()

{

int a;

scanf(”%d”,&a);

if(a>=0&&a<=100)

{

if (a%2==0)

printf(”evenn”);

else

printf("oddn");

}

else

printf(”error”);

return 0;

7、绝对值

Description

求整型数据和浮点型数据的绝对值。

Input

输入两个数,第一个是整数,第二个是浮点数。

Output

输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。

Sample Input

-1

1

Sample Output

1

1

Answer

#include

#include

#include〈stdlib。h〉

int main()

int a,c;

double b,d;

scanf(”%dn%lf",&a,&b);

c=abs(a);

d=fabs(b);

printf("%dn%g",c,d);

return 0;

8、简单的打折计算

Description

商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单

位:元),精确到分。

Input

输入只有一行,三个整数m、n和x,且0〈x〈m〈n<1000。

Output

输出金额,精确到分。

Sample Input

95 300 4

Sample Output

334。40

Answer

#include

int main()

{

int m,x,n,a;

float b;

scanf(”%d%d%d",&m,&n,&x);

0

x

m〈n&&n<1000;

a=m*x;

if (a>n)

b=0。88*a;

else

b=a;

printf(”%.2fn",b);

return 0;

9、 判断闰年

Description

输入一个正整数的年份,判断是否为闰年。

Input

输入只有一行,为一个10000以内的正整数。

Output

输出为一行.

若输入为闰年偶数则输出“Yes",否则输出“No”。

Sample Input

2010

Sample Output

No

答案

#include

int main()

{

int a;

scanf(”%d",&a);

if (a>0&&a〈10000)

{

if (a%4==0&&a%100!=0)

printf(”Yesn");

else if (a%400==0)

printf("Yesn”);

else

printf("Non");

}

else

printf(”error”);

return 0;

10、 水仙花数

Description

333

如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数.如:1+5+3=153.

Input

一个整数x,100<=x〈=999。

Output

x是水仙花数,则输出“YES”,否则为“NO”。

Sample Input

153

Sample Output

YES

Answer

#include

int main()

{

int a,b,c,d,e;

scanf("%d",&a);

b=a/100;

c=(a—b*100)/10;

d=(a—b*100-c*10);

e=b*b*b+c*c*c+d*d*d;

if(a==e)

printf("YESn”);

else

printf(”NOn");

return 0;

11、 三个数比较大小

Description

从键盘上输入0~100之间的三个数,按从小到大的顺序输出。

Input

输入只有一行,为三个整数。

Output

按从小到大输出这三个数。

Sample Input

15 10 20

Sample Output

10 15 20

Answer

#include

int main()

int a,b,c;

scanf(”%d %d %d",&a,&b,&c);

if (a〉=b)

if (b〉=c)

printf("%d %d %dn”,c,b,a);

else if (a>c)

printf(”%d %d %dn”,b,c,a);

else

printf("%d %d %dn",b,a,c);

else

{

if (a〉=c)

printf("%d %d %dn",c,a,b);

else if (b〉=c)

printf("%d %d %dn”,a,c,b);

else

printf(”%d %d %dn”,a,b,c);

return 0;

}

12、 输出整数的最低两位

Description

把一个整数的最低两位打印出来,不输出整数的符号。

Input

输入为一个整数n,不会超出int类型的数据范围。

Output

输出n的最低两位数字。但是,输入的数字本身不足两位时,不应当补0。如,输入为“1”,则输出为“1"。

Sample Input

-102

Sample Output

02

Answer

#include

int main()

{

int a,b,c;

scanf("%d",&a);

if(a〉=100)

b=a-a/100*100;

printf(”%。2dn”,b);

else if(a>=0)

printf("%dn”,a);

}

else if(a〉=-99)

{

printf(”%dn",-a);

}

else

c=—a;

b=c—c/100*100;

printf("%。2dn",b);

}

return 0;

}

13、判断奇偶数(填空)

Description

编写一个程序,判断读取的正整数的奇偶性,部分程序已经给出,请填上空白语句,并提交填充后的完整程

序。

程序(含答案):

#include

int main()

{

int num;

scanf("%d”,&num);

if (num%2==0)

printf(”%d is an even number.",num);//num是一个偶数

else

printf(”%d is an odd number.",num);//num是一个奇数

return 0;

14、求分段函数的值(填空)

Description

设有分段函数如下:

给出N>0个x的值,求对应的y值并输出。

部分程序已经给出,请填充其中的空白语句,并提交填充后的完整程序.

程序(含答案):

#include

#include

int main()

double x,y;

int i,N;

scanf(”%d",&N);

for (i=0;i〈N;i++)

scanf("%lf”,&x);

if (x〈0)

y=-x;

else if (x〈1)

y=sin(2*x);

else if (x<5)

y=sqrt(x*x*x+x);

else

y=2*x+10;

if (i==0)

printf("case 1:y=%.6g。”,y);

else

printf("ncase %d:y=%。6g。",i+1,y);

}

return 0;

}

15、输出是m的倍数或n的倍数、但不是m和n的公倍数的数

Description

输出1~k之间是m的倍数或n的倍数、但不是m和n的公倍数的数,其中1<=m,n〈k〈100,且m与n不相等.

Input

输入三个整数,依次为k、m、 n.

Output

从小到大输出符合题意的所有整数,两数之间用一个空格分开.

Sample Input

15 2 3

Sample Output

2 3 4 8 9 10 14 15

Answer

#include

int main()

{

int k,m,n,a,i=1;

scanf("%d %d %d”,&k,&m,&n);

if(m

a=m;

else

a=n;

printf("%d",a);

for(i=a+1;i<=k;i++)

if((i%m==0&&i%n!=0)||(i%n==0&&i%m!=0))

printf(” %d",i);

}

return 0;

16、A+B ProblemT

Description

计算a+b,0〈=a,b<1000。

Input

输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开.

Output

每行输出一个a+b的值,顺序与输入对应。

Sample Input

1 2

10 20

Sample Output

3

30

Answer

#include

int main()

int a,b;

while(scanf(”%d %d",&a,&b)!=EOF)

{

printf(”%dn”,a+b);

}

return 0;

17、A+B Problem (II) : Input/Output Pratice

Description

计算a+b,0〈=a,b<1000。

Input

输入的第一行是一个整数N,后面有N对整数a和b,每对a和b占一行,a,b用空格分开。

Output

每行输出一个a+b的和,顺序与输入对应.

Sample Input

2

1 2

10 20

Sample Output

3

30

Answer

#include

int main()

{

int a[1000],b[1000],N,i;

scanf("%d",&N);

for(i=1;i<=N;i++)

scanf("%d %d”,&a[i],&b[i]);

for(i=1;i〈=N;i++)

printf(”%dn",a[i]+b[i]);

return 0;

18、成绩的等级

Description

把百分制的考试成绩转换成五级制的成绩:

90~100:Excellent

80~89:Good

70~79:Average

60~69:Pass

0~59:Failing

不在0~100之间的输入是非法数据,输出“Error”。

Input

输入多行,每行一个整数.

Output

输入所对应的成绩等级。

Sample Input

—1

81

92

35

68

72

100

Sample Output

Error

Good

Excellent

Failing

Pass

Average

Excellent

Answer

#include

int main()

int score;

while(scanf("%d”,&score)!=EOF)

{

if (score<0||score〉100)

printf(”Errorn");

else

{

switch (score/10)

{

case 0:

case 1:

case 2:

case 3:

case 4:

case 5:printf(”Failingn");break;

case 6:printf(”Passn”);break;

case 7:printf(”Averagen");break;

case 8:printf(”Goodn");break;

case 9:

case 10:printf("Excellentn”);break;

}

}

return 0;

}

19、n个数的最大值和最小值

Description

找出n个数中最大的数和最小的数,并将它们的值输出出来.

Input

输入为n+1个整数,都在int类型范围内.这些数可能用若干空格或者换行符分隔开。

输入的第1个数为n,表示后续有n个数输入。从输入的第2个数开始,求出直到第n+1个数中最大的数和

最小的数。

Output

输出为两行,格式见sample。

Sample Input

3 0 1 —1

Sample Output

The maximum number is 1.

The minimum number is —1.

Answer

#include〈stdio。h〉

int main()

{

int n,i,max,min;

scanf("%d",&n);

int a[n];

for(i=0; i〈n; i++)

scanf("%d",&a[i]);

max=a[0];

min=a[0];

for(i=0; i

if(max

max=a[i];

if(min〉a[i])

min=a[i];

printf("The maximum number is %d.n”,max);

printf(”The minimum number is %d。”,min);

return 0;

}

20、字符加密

Description

Tom和Jack是密码学爱好者,他们在聊天时经常使用一些暗语。他们使用的一种最简单的暗语是:将要说的

每句话里面的英文字母变成这个字母之后的某个字母.现在要求你写一个程序,将一个字母变成它之后的某

个字母。

Input

输入有2个:一个大写字母c和一个正整数d(0

后面第d个字母.

Output

输出字母c之后的第d个字母.大小写与c一致.如果c之后的某个字母已经超出'Z',则再从字母'A’开始计

数.

如:c='A',d=3,则输出应为:D.

若:c='Y’,d=3,则输出应为:B。

Sample Input

A 3

Sample Output

D

Answer

#include

void main()

int d,x;

char c;

scanf(”%c",&c);

scanf(”%d”,&d);

x=c+d;

c=c+d;

x<=90?printf("%c",c):printf(”%c",x—26);

21、求100以内的素数

Description

素数是只能被1和自身整除的正整数,根据数学定义1不是素数。素数也叫质数。

Input

输入为两个整数m和n,满足0<=m〈=n<=100。

Output

从大到小输出m~n之间的所有素数,一个素数一行.如果m~n之间没有素数,则不输出任何数。

输出的所有数在两行“=====”之间。

Sample Input

2 12

Sample Output

=====

11

7

5

3

2

=====

Answer

#include〈stdio.h〉

#include

int main()

int m,n,i,j,k,t;

scanf("%d %d",&m,&n);//2 12

printf("=====n");

for(i=n;i>=m;i—-)

{ t=0;

for(j=2;j<=sqrt(i);j++)//12

if(i%j==0)

t=1;

if(t==0&&i>1)

printf(”%dn”,i);

}

printf(”=====”);

return 0;

22、 Sum Problem (II) : Input/Output Pratice

Description

计算若干整数的和,这些整数都是小于1000的非负整数。

Input

输入的第一行是一个整数M,后面有M个测试样例.每个测试样例以一个整数N开始,后面接着是N个整数。

Output

每组测试样例对应一行输出,为所给的N个整数之和,顺序与输入对应。

Sample Input

2

3 1 2 3

5 10 15 20 30 50

Sample Output

6

125

Answer

#include

int main()

{

int m,n,a,i,j,s;

scanf("%d”,&m);

for(i=1;i〈=m;i++)

{

scanf("%d”,&n);

s=0;

for(j=1;j<=n;j++)

scanf(”%d",&a);

s=s+a;

printf(”%dn",s);

return 0;

}

23、十进制整数转二进制

Description

16

给出一个十进制的非负整数x,x<=2,把它转换成二进制数输出。

Input

输入为多行,每行一个整数x,至读入EOF结束。

Output

每行输出x对应的二进制数值。

Sample Input

0

1

3

33

65535

Sample Output

0

1

11

100001

11111

Answer

#include 〈stdio。h>

int main()

int a[100],i,b;

while(scanf("%d”,&b)!=EOF)

for(i=0;;i++)

{

a[i]=b%2;

b=b/2;

if(b==0)

break;

for(;i>=0;)

printf("%d”,a[i]);

i-—;

printf("n”);

}

return 0;

24、简单的整数排序

Description

对给出的若干整数按从小到大排序。

Input

输入的第一个数为n(n〈=1000),后接n个整数.

Output

按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。

Sample Input

10 3 9 1 5 2 8 5 6 7 3

Sample Output

1 2 3 3 5 5 6 7 8 9

Answer

#include

int main()

{

int c,i,n,j;

int a[1000];

scanf("%d”,&n);

for (i=0;i〈n;i++)

scanf(”%d”,&a[i]);

for(i=1;i<=n-1;i++)

for(j=0;j〈n-i;j++)

if(a[j]〉a[j+1])

c=a[j];

a[j]=a[j+1];

a[j+1]=c;

}

printf("%d”,a[0]);

for(i=1;i〈n;i++)

printf(” %d”,a[i]);

return 0;

}

25、兔子的繁殖问题

Description

假设一对兔子每月能生一对小兔(一雌一雄),每对小兔出生后的下一个月是没有繁殖能力的,至出生后的

第三个月开始又可以每月生一队小兔,问从一对刚出生的小兔开始,经过若干个月后一共有多少兔子(假设在

此过程中兔子没有死亡)?

这个问题是意大利数学家菲波那契(Fibonacci)在他1202年出版的《算盘全书》中提出来的,从第一对刚

出生的小兔开始每月的兔子数被乘坐菲波那契序列.

Input

输入的第一个数为n,接下来有n个数字。每个数字为一个月份m(m<=45)。


本文标签: 整数 输入 输出 金额 兔子