人人范文网 范文大全

C语言作业

发布时间:2020-03-02 09:44:16 来源:范文大全 收藏本文 下载本文 手机版

Problem B: 算术基本运算 Description 计算两整数x和y(0

printf("x * y : %d\n",x*y);

printf("x / y quotient: %d, remainder: %d\n",x/y,x%y);

printf("x ^ 2 : %d\n",x*x);

printf("y ^ 3 : %d\n",y*y*y);

return 0; }

Problem C: 求圆的面积和周长 Description 从键盘输入圆的半径,求圆的面积和周长,圆周率取3.14。 Input 输入一个浮点型数据,有效数字不会超过十进制的6位。 Output 输出为两行。

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

3 Sample Output Area: 28.260000 Perimeter: 18.840000 HINT 了解浮点类型的输入、输出和算术运算符 #include int main() {

double Area,Perimeter,r,p=3.14;

scanf("%lf",&r);

Area=p*r*r,Perimeter=2*p*r;

printf("Area: %lf\n",Area);

printf("Perimeter: %lf\n",Perimeter);

return 0; }

Problem D:平均值 Description 求3个数的平均值。 Input 输入只有一行,为3个较小的整数。 Output 输出为这3个整数的平均值,保留3位小数。 Sample Input 1 2 3 Sample Output 2.000 HINT 注意除法运算对整型数据和浮点型数据是不一样的。 #include int main() {

int x,y,z;

float ave;

scanf("%d %d %d",&x,&y,&z);

ave=(x+y+z)/3.0;

printf("%.3f",ave);

return 0; }

Problem E: 货币兑换

Description 给出人民币对美元、欧元、日元的当日汇率,求给定金额的人民币能兑换成外币的金额,求给定金额的外币能兑换成人民币的金额。 要计算的外币有三种:美元、欧元、日元。 Input 输入有三行。

第一行依次为美元、欧元、日元外币汇率,用空格分开。汇率用100外币为单位,精确到小数点后4位,如668.5200表示“100美元=668.5200人民币”。汇率浮动范围为(0,10000)。

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

第一行为金额为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 HINT 了解浮点数据类型的精确度和输出控制。

#include int main() { double a,b,c; double x; double y; scanf("%lf%lf%lf",&a,&b,&c); scanf("%lf",&x); scanf("%lf",&y); printf("%.2lf %.2lf %.2lf\n",x*0.01*a,x*0.01*b,x*0.01*c); printf("%.2lf %.2lf %.2lf\n",y/a*100,y/b*100,y/c*100);

return 0;

} Problem F: 求字符的值 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 HINT 了解字符值的存储和整型的关系。 #include int main() { char x,y,z; scanf("%c%c%c",&x,&y,&z); printf("%.3d %.3o %.3x\n",x,x,x); printf("%.3d %.3o %.3x\n",y,y,y); printf("%.3d %.3o %.3x\n",z,z,z); return 0; }

Problem G: 奇数还是偶数? Description 输入一个整数,判读它是奇数还是偶数。 Input 输入只有一行,为一个100以内的正整数。 Output 输出为一行。

若输入为偶数则输出“even”,奇数输出“odd”。 Sample Input 30 Sample Output even HINT 用整数运算可以解决,练习“?:”表达式。 #include int main() {

int x;

scanf("%d",&x);

if (x%2==0)

printf("even");

else

printf("odd");

return 0; }

Problem H: 绝对值 Description 求整型数据和浮点型数据的绝对值。 Input 输入两个数,第一个是整数,第二个是浮点数。 Output 输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。 Sample Input -1 1 Sample Output 1 1 HINT 求绝对值可以用标准库函数来完成,也可以自己判断。注意浮点数的输出格式。求绝对值的函数在哪个头文件?貌似很多人会搞错,包括很多编书的人! #include #include int main() {

int x;

float y;

scanf("%d",&x);

scanf("%f",&y);

printf("%d\n",abs(x));

printf("%g\n",fabs(y));

return 0; } Problem I: 简单的打折计算 Description 商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单位:元),精确到分。 Input 输入只有一行,三个整数m、n和x,且0

95 300 4 Sample Output 334.40 HINT 了解浮点型的输出控制,注意整型和浮点型混合运算过程中的数据类型转换。 #include int main() {

int m,n,x;

double y;

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

y=m*x;

if(y>n)

y=y*0.88;

else

y=y;

printf("%.2lf\n",y);

return 0; } Description 输入一个正整数的年份,判断是否为闰年。 Input 输入只有一行,为一个10000以内的正整数。 Output 输出为一行。

若输入为闰年偶数则输出“Yes”,否则输出“No”。 Sample Input 2010 Sample Output No HINT 了解逻辑运算符和关系运算符。 #include int main() {

int a;

scanf("%d",&a);

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

printf("Yes");

else printf("No");

return 0; } Problem K: GHacker的解谜过关游戏 Description

GHacker最近痴迷于一个新上市的解谜游戏,其中一关的过关是破解一个字符串S。经过3天的冥思苦想,GHacker成功的搞明白了这其中的奥秘,把串S中的整数取出来求和,就可以过关了。但是GHacker的数学实在糟糕。他无法在短暂的时间内算出来,只好求助Jackie。Jackie观察到虽然每次出现的数字不同,但是其它的符号并不会变化。于是Jackie编写了一个非常短的程序,帮助GHacker把这一关过了。 Input 输入为串S,只有一行。 Output 串S中用非数字(0~9)分隔开的非负整数之和,不会超出int类型的数据范围。 Sample Input `13?:[7514],54.487=="(438922x159??392)%032\n111cdef120$95; Sample Output 447899 HINT scanf()可以解决这个问题,注意转义字符和格式控制字符。 #include int main() {

int i,sum,num;

char str[1000];

while(scanf("%s",str)!=EOF)

{

num=sum=0;

for(i=0;;i++)

{

if(str[i]>='0' && str[i]

num=num*10+str[i]-'0';

else

{

sum=sum+num;num=0;

if(str[i]=='\0')break;

}

}

printf("%d\n",sum);

}

return 0; }

Problem L: 水仙花数 Description 如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数。如:13+53+33=153。 Input

一个整数x,100 int main() {

int x,a,b,c,sum;

scanf("%d",&x);

a=x/100;

b=(x-a*100)/10;

c=(x-a*100-b*10);

sum=a*a*a+b*b*b+c*c*c;

if(x==sum)

printf("YES");

else

printf("NO");

return 0; } Problem M: 求1+2+...+n=? Description 给定一个n,求出s = 1+2+3+...+n的值。 Input 输入只有一行,包含一个正整数n(n int main() {

unsigned long long n,s,a;

scanf("%llu",&n);

a=n%2;

if(a==0) { s=n/2*(n+1);

printf("%llu",s); }

else if(a!=0)

{s=n*((n+1)/2); printf("%llu",s); } return 0; }

Problem N: 2的多少次幂 Description 从键盘输入一个数x,x是2的整数次幂(x=2y),请编程求出y的值。 Input 一个非负有理数x,x在[0,2256]范围内。 Output 一个整数y。 Sample Input 1 Sample Output 0 HINT 看起来数据很大,但是用double完全可以存储。为什么?请研究下IEEE-754标准的浮点数存储格式。这里要用到C语言标准库的数学函数。 #include int main() {

double x;

scanf("%lf",&x);

printf("%g",log2(x));

return 0; } Problem A: 哪一行比较长 Description 读取两行字符串,按每行的长度从长到短输出。 Input 输入为两行,每行不会超过26个字符。 Output 输出为两行,按每行的长度从长到短输出。 Sample Input abcdefghijk abcdefghijklmnopqrstuvwxyz Sample Output abcdefghijklmnopqrstuvwxyz abcdefghijk HINT

了解字符串的存储和操作,了解gets()和scanf("%s")读入字符串的不同之处。 #include #include int main() { int x,y; char a[30],b[30]; gets(a); gets(b); x=strlen(a); y=strlen(b); if(x>y) {

puts(a);

puts(b);

return 0;

} puts(b); puts(a); } Problem B: 三个数比较大小 Description 从键盘上输入0~100之间的三个数,按从小到大的顺序输出。

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

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

Sample Input 15 10 20 Sample Output 10 15 20 HINT

用if语句判断各种情况可以解决这个问题。

include int main() { int a,b,c,t;

scanf("%d%d%d",&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("%d %d %d\n",a,b,c); return 0; } Problem C: 输出是m的倍数或n的倍数、但不是m和n的公倍数的数 Description 输出1~k之间是m的倍数或n的倍数、但不是m和n的公倍数的数,其中1

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

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

Sample Input 15 2 3 Sample Output 2 3 4 8 9 10 14 15 HINT

难点在于输出格式的控制:空格在数的中间,学会用循环时边界情况的特殊处理。

#include int main()

{

int k,m,n,a;

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

{

if(m>n)

{

printf("%d",n);

for(a=n+1;a

{

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

printf(" %d",a);}

}

else

{

printf("%d",m);

for(a=m+1;a

{

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

printf(" %d",a);}

}

}

return 0; } Problem D: A+B Problem Description 计算a+b,0 int main() {

int a,b;

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

printf("%d\n",a+b); } Problem E: A+B Problem (II) : Input/Output Pratice Description 计算a+b,0

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

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

Sample Input 2 1 2 10 20 Sample Output 3 30 HINT

N给出了测试样例数,用for循环处理方便。

#include int main() { int a,b,N,i; scanf("%d\n",&N); for(i=1;i

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

printf("%d\n",a+b); } return 0; } Problem F: A+B Problem (III) : Input/Output Pratice Description 计算a+b,0

Input

输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。当测试样为0 0时表示输入结束,0 0不参与运算。

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

Sample Input 1 2 10 20 0 0

Sample Output 3 30

HINT

练习break的使用。

#include int main() {

int a,b;

while(scanf("%d%d",&a,&b))

{

if(a==0&&b==0)

break;

printf("%d\n",a+b);

} } Problem G: A+B Problem (IV) : Input/Output Pratice Description 计算a+b,0

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

Output 每行输出一个a+b的值,顺序与输入对应。每个格式样例之间用一个空行分隔开。

Sample Input 1 2 10 20

15 35 Sample Output 3 30 50 HINT 由于输出的和比空行多一个,所以全部计算放在一个循环里是不行的,必须要特殊处理开头或者结尾。 #include int main() {

int a,b,i;

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

printf("%d\n",a+b);

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

printf("\n%d\n",a+b); } Problem H: 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.

HINT

分隔符是空格还是回车都是空白符,对scanf("%d")来说没有区别;先读入n,然后用for循环就很容易控制读入n个数的过程。

#include int main() { int n,i,max,min,x,y; scanf("%d",&n); scanf("%d",&x); min=x; max=x; for(i=3;i

scanf("%d",&y);

if(min>y)

min=y;

if(max

max=y; } printf("The maximum number is %d.\n",max); printf("The minimum number is %d.\n",min); return 0; } Problem I: 成绩的等级 Description 把百分制的考试成绩转换成五级制的成绩:

90~100:Excellent

80~89:Good

70~79:Average

60~69:Pa

0~59:Failing

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

Input 输入多行,每行一个整数。

Output 输入所对应的成绩等级。

Sample Input -1 81 92 35 68 72 100 Sample Output Error Good Excellent Failing Pa Average Excellent

HINT

用switch语句解决这个问题比较方便。

#include int main() {

int a,grade;

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

{

if (grade>=0 && grade

a=1;

else if (grade>=60 && grade

a=2;

else if (grade>=70 && grade

a=3;

else if (grade>=80 && grade

a=4;

else if (grade>=90 && grade

a=5;

else

a=6;

switch (a)

{

case 1:

printf("Failing\n");

break;

case 2:

printf("Pa\n");

break;

case 3:

printf("Average\n");

break;

case 4:

printf("Good\n");

break;

case 5:

printf("Excellent\n");

break;

case 6:

printf("Error\n");

break;

}

}

return 0; } Problem J: 只有一个二元运算符的表达式运算 Description 编程序读入并计算只有一个二元运算符的表达式的值。用到的二元运算符有:“+”、“-”、“*”、“/”、“%”,与C语言的语法一致。

Input 每行输入一个表达式,格式为:二个整型的操作数a和b,中间用一个符号分开,这个符号就是运算符。测试样例不存在除数为0的情况。

输入以a和b为0,且用一个空格分开结束。

Output 每行对应输入的运算符为“+”、“-”、“*”、“/”、“%”,则计算a+b、a-b、a*b、a/b、a%b的值;否则输出“invalid op”。

Sample Input 33+5 8*9 2.2 1-6 17/3 9%3 0 0

Sample Output 38 72 invalid op -5 5 0 HINT

教材上有非常相似的例题可以参考。

#include int main() {

int a, b;

char ch;

while (scanf("%d%c%d", &a, &ch, &b))

{

if (a == 0 && b == 0 && ch == ' ') break;

if

(ch == '+') ch = '+';

else if (ch == '-') ch = '-';

else if (ch == '*') ch = '*';

else if (ch == '/') ch = '/';

else if (ch == '%') ch = '%';

else

ch = '~';

switch (ch)

{

case '+' :

printf("%d\n", a + b);

break;

case '-' :

printf("%d\n", a2x + 1 = 0 only one real root : 1

Case 3 : 5x^22x = 0 two real roots : 0, 0.666667

Case 5 : 3x^2 + 12 = 0 two imaginary roots : 2i, -2i

Case 6 : 2x^2 + 4x + 4 = 0 two imaginary roots : -1+i, -1-i

HINT

输出方程格式的各种情况要想清楚,这一部分测试数据给的很全面。另一个就是浮点数的精度控制,这一部分sample给出了例子。

值得注意的是,linux下gcc编译的浮点数运算结果有-0,这是OJ系统Judge端使用的系统;而windows XP下的minGW编译器和VC6不会产生-0,只会输出0;但windows 7下的minGW编译器是能够产生-0的(确实很诡异)。因此使用windows XP的同学忽略了对结果为0的检测,程序需要对结果为0的情况进行全面考虑,确保正确的输出0。这个问题卡了好些同学好几天。

关于是否会产生-0,输出表达式0.0/-1的结果就能测试出来。浮点数从负数方向运算出结果为0,则浮点值为-0是符合C语言浮点数运算规则的,目前尚不清楚windows XP系统不能产生-0的原因。

#include #include #define eps 0.00001 int main() {

int i,n=1;

double a,b,c,x1,x2,p,q,m,t;

char x,y;

while(1)

{

scanf("%lf",&a);

if(fabs(a)

break;

//结束条件

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

p=(-b)/(2*a);

q=b*b-4*a*c;

if(fabs(q)

//精度控制

q = 0;

m=(sqrt(-q))/(2*a);

x1 = (-b + sqrt(q))/(2*a);

x2 = (-bxx - y;

if(a * x + b * y + (c * 1.0) /(d * 1.0) * z == m)

printf("%ld,%ld,%ld\n",x,y,z);

}

}

printf("\n");

}

}

return 0; } Problem I: 神棍的纯真愿望 Description 问题背景:

神棍队的神棍童鞋很喜欢和女盆友逛街。神棍节这天,他们照例去逛街,亲昵过程中忽然发现路边上围了好一圈人。好奇心大盛的神棍于是凑过去围观。原来那里有一个棍神,他出了一道题目,如果有人能够答对的话,他就会实现那个人的一个愿望。神棍心想,有个女盆友多么幸福,要是大家都有女盆友该有多好。于是神棍想要答出这个问题,然后许一个让大家都可以很快拥有自己的女盆友的愿望,顺便在女盆友的面前臭美一番。神棍扫了一眼题目,拿出贴身小电脑,巴拉巴拉几下就敲出了代码,解决了那个问题。大家都在为神棍欢呼。这是什么问题

呢?你是否也有兴趣看看?如果AC了的话就可以跟魔法少女签订契约,成为魔法少女的奴隶喔!

问题描述:

某个数的立方如果以111结尾的话,我们就称其为“神棍数”,现在要你求第k大的“神棍数”是多少。

Input 多组case,以EOF结尾。

每个case一行,只包含一个整数k(1

Output 一个整数表示第k大的“神棍数”

Sample Input 1 Sample Output 471 #include int main() {

unsigned long long k;

while(scanf("%llu",&k)!=EOF)

{

if(k>1)

printf("%llu471\n",k-1);

else

printf("471\n",k);

}

return 0; } Problem J: 魔方阵 Description 所谓N阶魔方阵,是一个N*N的方阵,其元素由1到N^2组成,且方阵每行每列以及对角线的元素和相等。如三阶魔方阵: 8 1 6 3 5 7 4 9 2 魔方阵的规律如下:

从1~N*N的 各个数依次如下规则存放:

(1) 1在第一行中间一列;

(2) 每一个数存放的行比前一个数的行数减一,列数加一(如上的三阶方阵5在4的上一行,后一列);

(3) 如果上一个数在第一行,则下一个数在最后一行,列数加一;

(4) 如果上一个数在最后一列,则下一个数在第一列,行数减一;

(5) 如果按上述规则确定的位置已经有数,或上一个数在第一行第N列,则下一个数放在上一个数的正下方。

Input 输入包含多组数据,每组为一个小于100的正奇数。

Output 对于每个输入的N,输出N阶魔方阵;两组数据之间用一个空行分隔。方阵中每行每两个数之间有一个空格,行首和行末没有多余的空格。

Sample Input 3 Sample Output 8 1 6 3 5 7 4 9 2

HINT #include int main() {

int i, j, k, n, a[200][200];

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

{

for(i = 1; i

for(j = 1; j

a[i][j] = 0;

j = n / 2 + 1;

a[1][j] = 1;

for(k = 2; k

{

i=i-1;

j=j+1;

if (i n)

{

i=i+2;

j=j-1;

}

else

{

if (i

if (j > n) j = 1;

}

if(a[i][j] == 0) a[i][j] = k;

else

{

j=j-1;

i=i+2;

a[i][j] = k;

}

}

for(i = 1; i

{

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

for(j = 2; j

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

printf("\n");

//for(j=1;j

//printf("%5d",a[i][j]);

//printf("\n");

}

printf("\n");

}

return 0; }

C语言作业

c语言第六次作业

C语言作业答案

C语言作业总结

c语言课程设计作业(推荐)

西工大C语言POJ作业

C语言作业实验报告[材料]

C语言程序设计大作业

大连理工大学c语言大作业

C语言程序设计课程作业_A资料

C语言作业
《C语言作业.doc》
将本文的Word文档下载到电脑,方便编辑。
推荐度:
点击下载文档
相关专题 c语言作业 作业
相关范文推荐
点击下载本文文档