C语言程序设计实验课参考答案(复习题)

复习资料参考答案

基本程序设计

多位数逆转

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main(){
int x,sum=0;
scanf("%d", &x);
while(x>0){
sum = sum*10 + x%10;
x = x /10;
}
printf("%d", sum);
return 0;
}

平行四边形

1
2
3
4
5
6
7
8
9
#include<stdio.h>
int main(){
char x;
scanf("%c", &x);
printf("%c%c%c%c%c\n", x, x, x, x, x);
printf(" %c%c%c%c%c\n", x, x, x, x, x);
printf(" %c%c%c%c%c\n", x, x, x, x, x);
return 0;
}

凯撒密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main(){
char x;
x = getchar();
if(x == 'x'){
x = 'a';
}else if(x == 'y'){
x = 'b';
}else if( x == 'z'){
x = 'c';
}else{
x = x + 3;
}
printf("%c", x);
return 0;
}

简单计算器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>

int main(){
int x ,y;
char z;
scanf("%d %d %c", &x, &y, &z);
switch(z){
case '+':
printf("%d", x + y);
break;
case '-':
printf("%d", x - y);
break;
case '*':
printf("%d", x * y);
break;
case '/':
if(y == 0){
printf("Divided by zero!");
}else{
printf("%d", x / y);
}
break;
default:
printf("Invalid operator!");
break;
}
return 0;
}

判断素数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<math.h>
int main(){
int n;
scanf("%d", &n);
if(n<2){
printf("NO");
}else if(n==2){
printf("YES");
}else if(n%2==0){
printf("NO");
}else{
for(int i=2; i<=sqrt(n); i++ ){
if(n%i==0){
printf("NO");
return 0;
}
}
printf("YES");
}
return 0;
}

进制转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
int main(){
int n, r, s[10000], i=0;
scanf("%d %d", &n, &r);
while(n>0){
s[i] = n%r;
n = n / r;
i++;
}
for(int j=i-1; j>=0; j--){
printf("%d", s[j]);
}
return 0;
}

八进制转十进制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<math.h>
int main(){
int n, sum=0, i=0;
scanf("%d", &n);

while(n>0){
sum = sum + n%10*pow(8, i);
n = n /10;
i++;
}

printf("%d", sum);
return 0;
}

最重的苹果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main(){
int n;
scanf("%d", &n);
int max = 0, id = 0;
for(int i=0; i<n; i++){
int t;
scanf("%d", &t);
if(max < t){
max = t;
id = i+1;
}
}
printf("%d", id);
return 0;
}

判断字符串是否是回文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>
int main(){
char s[100];
scanf("%s", s);
int len = strlen(s);
for(int i=0, j=len-1; i<j; i++, j--){
if(s[i] != s[j]){
printf("no");
return 0;
}
}
printf("yes");
return 0;
}

多数求和

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int main(){
int t, sum = 0;
for(int i=0; i<1000; i++){
scanf("%d", &t);
if(t == -1){
break;
}
sum = sum + t;
}
printf("%d", sum);
return 0;
}

应用程序设计

单词替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<string.h>
//在此处编写代码
int main()
{
//在此处编写程序代码
char s[200][100], t[100], len=0;
while(scanf("%s", t)==1){
strcpy(s[len], t);
len++;
}
for(int i=0; i<len-2; i++){
if(strcmp(s[i],s[len-2])==0){
strcpy(s[i], s[len-1]);
}
}
for(int i=0; i<len-2; i++){
printf("%s ", s[i]);
}
return 0;
} //代码在DEV下 测试完备后再点击提交

总分排名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<string.h>
//此处编写代码
int main()
{
//此处编写代码
struct stu{
char name[30];
int ch, ma, en, sum;
};
struct stu s[1000];
char xm[30];
int n, xms, x=0;
scanf("%d %s", &n, xm);
for(int i=0; i<n; i++){
scanf("%s %d %d %d", s[i].name, &s[i].ch, &s[i].ma, &s[i].en);
s[i].sum = s[i].ch + s[i].ma + s[i].en;
if(strcmp(s[i].name, xm)==0){
xms = s[i].sum;
}
}
for(int i=0; i<n; i++){
if(s[i].sum > xms){
x++;
}
}
printf("%d %d", xms, x+1);
return 0;
} //编写完成后,DEV测试通过完备,再提交

寻找素数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<stdio.h>
#include<math.h>
int isPrime(int x){
if(x<2){
return 0;
}
if(x==2){
return 1;
}
if(x%2==0){
return 0;
}
for(int i=2; i<=sqrt(x); i++){
if(x%i==0){
return 0;
}
}
return 1;
}
//写代码处
int main()
{
//编写代码处
int m,n, count=0;
scanf("%d %d", &m, &n);
for(; m<=n; m++){
if(isPrime(m)){
printf("%d ", m);
count++;
}
}
if(count==0){
printf("none");
}
return 0;
} //请仔细编写代码,完备后提交

单次翻转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#include<string.h>
//若需要请在此处编写代码
int main()
{
//请在此处编写代码,完善后提交
char s[100][100], t[100];
int i = 0;
while(scanf("%s", t) == 1){
strcpy(s[i], t);
i++;
}
for(int j=0; j<i; j++){
for(int k=strlen(s[j])-1; k>=0; k--){
printf("%c", s[j][k]);
}
printf(" ");
}
return 0;
}

修改职工信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef struct {
int id;
char name[20];
int age;
} worker;
int modifyworker(worker *w, int n ,int id, char *name, int age) {
/***********代码完成区***********/
for(int i=0; i<n; i++){
if(w[i].id == id){
strcpy(w[i].name,name);
w[i].age =age;
return 1;
}
}
return 0;
/***********代码完成区***********/
}
int main() {
int n;
worker P[100], pmodify;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%s%d", &P[i].id, P[i].name, &P[i].age);
scanf("%d%s%d", &pmodify.id, pmodify.name, &pmodify.age);
if (modifyworker(P, n, pmodify.id, pmodify.name, pmodify.age)==0)
printf("error!");
else
for (int i = 0; i < n; i++)
printf("%d %s %d\n", P[i].id, P[i].name, P[i].age);
return 0;
}

简单排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
//代码编写处
int main()
{
//代码编写处
int n, s[100];
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &s[i]);
}
//排序,3种方式任选一种
for(int i=0; i<n; i++){
for(int j=0; j<n-1-i; j++){
if(s[j] > s[j+1]){
int t = s[j];
s[j] = s[j+1];
s[j+1] = t;
}
}
}
//输出结果
for(int i=0; i<3; i++){
printf("%d ", s[i]);
}
return 0;
}