A - C语言实验——Hello World!(printf练习)
Description
很高兴你能上机实践所学的C语言知识!
编程不是在课本上的几页纸就能学会的,你必须多思考、多上机才能真正学会一门编程语言,这也是我们出这些题目的初衷。
这些题目都是课本上的基本题目,主要目的是让大家巩固课堂上所学到的,希望大家能够认真对待!
为了便于调试题目,做这些题目时可以先在CodeBlocks、DevC++或Microsoft VC++6.0中调试成功后再提交。
下面我们就开始吧:
利用C语言基本格式显示以下内容: Hello World!
Input
本题没有输入数据
Output
输出字符串Hello World!输出后需要换行。
Sample
Output
Hello World!
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main() 5 { 6 printf("Hello World!\n"); 7 return 0; 8 }
B - C语言实验——计算A+B(顺序结构)
Description
从键盘上输入两个整数,然后计算他们的和,并把他们的和打印出来。
Input
Output
Sample
Input
2 3
Output
5
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a,b; 7 scanf("%d %d",&a,&b); 8 printf("%d",a+b); 9 return 0; 10 }
C - C语言实验——交换两个整数的值(顺序结构)
Description
Input
Output
Sample
Input
4 6
Output
6 4
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a,b,t; 7 scanf("%d %d",&a,&b); 8 t=a,a=b,b=t; 9 printf("%d %d",a,b); 10 return 0; 11 }
D - C语言实验——逆置正整数
Description
Input
Output
Sample
Input
123
Output
321
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int x,a,b,c; 7 scanf("%d",&x); 8 a=x/100; 9 b=x/10%10; 10 c=x%10%10; 11 x=a+b*10+c*100; 12 printf("%d",x); 13 return 0; 14 }
E - C语言实验——买糖果
Description
Input
Output
Sample
Input
2
Output
6 2
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a; 7 scanf("%d",&a); 8 printf("%d %d",a*10/3,a*10%3); 9 return 0; 10 }
F - C语言实验——三个整数和、积与平均值
Description
给出三个整数,请你设计一个程序,求出这三个数的和、乘积和平均数。
Input
输入只有三个正整数a、b、c。
Output
输出一行,包括三个的和、乘积、平均数。 数据之间用一个空格隔开,其中平均数保留小数后面两位。
Sample
Input
2 3 3
Output
8 18 2.67
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a,b,c,sum,mul; 7 double p; 8 scanf("%d %d %d",&a,&b,&c); 9 sum=a+b+c; 10 mul=a*b*c; 11 p=sum/3.0; 12 printf("%d %d %.2lf",sum,mul,p); 13 return 0; 14 }
G - C语言实验——圆柱体计算
Description
已知圆柱体的底面半径r和高h,计算圆柱体底面周长和面积、圆柱体侧面积以及圆柱体体积。其中圆周率定义为3.1415926。
Input
输入数据有一行,包括2个正实数r和h,以空格分隔。
Output
输出数据一行,包括圆柱体底面周长和面积、圆柱体侧面积以及圆柱体体积,以空格分开,所有数据均保留2位小数。
Sample
Input
1 2
Output
6.28 3.14 12.57 6.28
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define PI 3.1415926 4 int main() 5 { 6 double r,h,l,s,cs,v; 7 scanf("%lf %lf",&r,&h); 8 l=2*PI*r; 9 s=PI*r*r; 10 cs=l*h; 11 v=s*h; 12 printf("%.2lf %.2lf %.2lf %.2lf",l,s,cs,v); 13 return 0; 14 }
H - C语言实验——温度转换
Description
Input
Output
Sample
Input
32.0
Output
0.00
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define PI 3.1415926 4 int main() 5 { 6 double F,C; 7 scanf("%lf",&F); 8 C=5*(F-32)/9; 9 printf("%.2lf",C); 10 return 0; 11 }
I - C语言实验——单个字符输入和输出(顺序结构)
Description
Input
Output
Sample
Input
A
Output
A
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define PI 3.1415926 4 int main() 5 { 6 char c; 7 c=getchar(); 8 putchar(c); 9 return 0; 10 }
J - C语言实验——转换字母(顺序结构)
Description
Input
Output
Sample
Input
a
Output
A
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define PI 3.1415926 4 int main() 5 { 6 char c; 7 c=getchar(); 8 putchar(c-32); 9 return 0; 10 }
K - 实数的输出和占位
Description
输入一个实数,请你按如下要求输出:
第一行按双精度默认输出,
第二行双精度数输出共占 10 位,其中 3 位小数,右对齐,左补空格并在两端添加星号包裹,
第三行双精度数输出共占 10 位,其中 3 位小数,左对齐,右补空格并在两端添加星号包裹。
Input
一个double范围内的正实数 a 。
Output
共三行,按题目描述输出。
Sample
Input
123.56789
Output
123.567890 * 123.568* *123.568 *
1 #include <stdio.h> 2 #include <stdlib.h> 5 int main() 6 { 7 double a; 8 scanf("%lf",&a); 9 printf("%lf\n",a); 10 printf("*%10.3lf*\n",a);//%(”+“右对齐,”-“左对齐)(占位数)(.小数个数)lf 11 printf("*%-10.3lf*\n",a); 12 return 0; 13 }
L - 大整数的输入输出
Description
输入两个 long long 范围内的整数,输出他们的和。
Input
两个 long long 范围内的整数。
Output
输出的两个大整数的和,保证结果在 long long 范围内。
Sample
Input
2222222222 3333333333
Output
5555555555
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 long long a,b; 7 scanf("%lld %lld",&a,&b); 8 printf("%lld",a+b); 9 return 0; 10 }
M - 带’ 和 ”字符的输出
Description
输入一个字符,输出两行。
第一行将字符用 ' 包裹。
第二行将字符用 " 包裹。
Input
输入一个字符。
Output
按题目描述输出。
Sample
Input
A
Output
'A' "A"
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 char c; 7 scanf("%c",&c); 8 printf("\'%c\'\n",c); 9 printf("\"%c\"",c); 10 return 0; 11 }
N - '%'字符的输入输出
Description
输入三个用 `` % `` 分割的正整数 a b c 代表月,日,年,
要求按照输入样式原样输出。
Input
三个int范围内的正整数,中间用 `` %`` 分割。
Output
按题目描述原样输出。
Sample
Input
9%17%2018
Output
9%17%2018
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a,b,c; 7 scanf("%d%%%d%%%d",&a,&b,&c); 8 printf("%d%%%d%%%d",a,b,c); 9 return 0; 10 }
O - ‘\’字符的输入输出
Description
输入三个用 `` \ `` 分割的正整数 a b c 代表日,月,年,
要求按照输入样式原样输出。
Input
三个int范围内的正整数,中间用 `` \ `` 分割。
Output
按题目描述原样输出。
Sample
Input
9\17\2018
Output
9\17\2018
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a,b,c; 7 scanf("%d\\%d\\%d",&a,&b,&c); 8 printf("%d\\%d\\%d",a,b,c); 9 return 0; 10 }
P - 十六进制数输出和占位
Description
输入一个整数,请你按如下要求输出:
第一行按原样输出,
第二行以十六进制输出(字母小写),
第三行以十六进制输出(字母大写)。
Input
一个int范围内的正整数 a 。
Output
共三行,按题目描述输出。
Sample
Input
456
Output
456 1c8 1C8
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a; 7 scanf("%d",&a); 8 printf("%d\n",a); 9 printf("%x\n",a); 10 printf("%X\n",a); 11 return 0; 12 }
Q - 八进制数输出和占位
Description
输入一个整数,请你按如下要求输出:
第一行按原样输出,
第二行以八进制靠右输出,不足 8 位左补 0 并在两端添加星号包裹,
第三行以八进制靠左输出,不足 8 位右补空格并在两端添加星号包裹。
Input
一个int范围内的正整数 a 。
Output
共三行,按题目描述输出。
Sample
Input
123
Output
123 *00000173* *173 *
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a; 7 scanf("%d",&a); 8 printf("%d\n",a); 9 printf("*%08o*\n",a);//%(默认补空格,0则补零)(占位数)(.小数个数)(进制) 10 printf("*%-8o*\n",a); 11 return 0; 12 }
R - 十进制输入输出和其它非空格占位
Description
输入一个整数,请你按如下要求输出:
第一行按原样输出,
第二行整数靠右原样输出,不足 8 位左补 0 并在两端添加星号包裹,
第三行整数靠左原样输出,不足 8 位右补空格并在两端添加星号包裹。
Input
一个int范围内的正整数 a 。
Output
共三行,按题目描述输出。
Sample
Input
123456
Output
123456 *00123456* *123456 *
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a; 7 scanf("%d",&a); 8 printf("%d\n",a); 9 printf("*%08d*\n",a);//%(默认补空格,0则补零)(占位数)(.小数个数)(进制) 10 printf("*%-8d*\n",a); 11 return 0; 12 }
S - 十进制输入输出和空格占位
Description
输入一个整数,请你按如下要求输出:
第一行按原样输出,
第二行按原样靠右输出,不足 8 位左补空格并在两端添加星号包裹,
第三行按原样靠左输出,不足 8 位右补空格并在两端添加星号包裹。
Input
一个int范围内的正整数 a 。
Output
共三行,按题目描述输出。
Sample
Input
123456
Output
123456 * 123456* *123456 *
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a; 7 scanf("%d",&a); 8 printf("%d\n",a); 9 printf("*%8d*\n",a);//%(默认补空格,0则补零)(占位数)(.小数个数)(进制) 10 printf("*%-8d*\n",a); 11 return 0; 12 }
T - 字符型数据输出和占位
Description
输入一个字符,请你按如下要求输出:
第一行字符数据默认输出,
第二行字符型数据输出共占 4 位,右对齐,左补 3 个空格并在两端添加星号包裹,
第三行字符型数据输出共占 4 位,左对齐,右补 3 个空格并在两端添加星号包裹。
Input
输入一个字符 。
Output
共三行,按题目描述输出。
Sample
Input
c
Output
c * c* *c *
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 char a; 7 scanf("%c",&a); 8 printf("%c\n",a); 9 printf("*%4c*\n",a);//%(默认补空格,0则补零)(占位数)(.小数个数)(进制) 10 printf("*%-4c*\n",a); 11 return 0; 12 }