C语言复习问题总结

发布在 C语言

C语言复习问题总结


1.字段结构的成员的地址

改错题:

1
2
3
4
struct _half_byte {
unsigned short hb0: 4, hb1: 4, hb2: 4, hb3: 4;
} m;
scanf("%hu %hu %hu %hu", &m.hb0, &m.hb1, &m.hb2, &m.hb3);

这里需要注意到字段结构的成员是没有地址的,因此scanf("%hu %hu %hu %hu", &m.hb0, &m.hb1, &m.hb2, &m.hb3);是错误的,正确的代码可以类似:

1
2
3
unsigned short tmp[4];
scanf(“%hu %hu %hu %hu”, &tmp[0] , &tmp[1] , &tmp[2] , &tmp[3]);
m.hb0 = tmp[0], m.hb1 = tmp[1] , m.hb2 = tmp[2] , m.hb3 = tmp[3];

2.&&和||的编译器优化

如果开启了编译器优化,那么若&&的第一个条件为假或者||的第一个条件为真,那么编译器将不会去看&&或||的第二个条件,因此:

Declare int x = 0, y = 1; , which values of the following expressions are nonzero?

A. x++ + y--

B. y-x||x++, x

C. x*y&&y--, y

D. y-- ? y++ : y

这道题目的B和C为例,B中y-x已经为1,所以编译器将忽略x++,所以x仍然为0,所以B错误。而C中x*y为0所以编译器将忽略y–,所以y为1,C正确。

3.注意标识符和关键字的区别

例如:main是一个合法的标识符,而非关键字。

再例如:Case是一个合法的标识符,而非关键字。(注意C语言区分大小写)

4.词法分析

How many tokens are there in the statement of *p+++=12.L;?

A. 5

B. 6

C. 7

D. 10

记号共有5类:标识符、关键字、常量(含字符串常量)、运算符、标点符号。

*p+++=12.L;可以被分解为 * p ++ += 12.L ;共计6个记号,所以,答案为B。

5.转义序列

根据文档:

Escape sequences are used to represent certain special characters within string literals and character constants.

在C中可以由 \(反斜杠)+ 字符来进行转义。

6.类型转换

long x, y; x = -6L; y = 5UL; Which expression is equal to 1?

A. x<y && -6L<5UL

B. x<y && -6L>5UL

C. x>y && -6L<5UL

D. x>y && -6L>5UL

x,y的类型为long,因此5UL将会被转换为5L,所以x<y。而在进行比较运算的时候-6L会被提升至Unsigned Long,此时-6L>5UL,所以答案选B。

7.后缀自增自减运算符的计算延迟

后缀式++/–在遇到&& || ?: , 运算符后、或者完整表达式结束后自增/自减。

8.注意作用域、链接和存储时期

作用域描述程序中可以访问一个标识符的一个或多个区域;链接包括外部、内部和空链接,具有空链接的变量被代码块或函数原型所私有,外部链接的变量可以在多文件程序的任何地方使用,内部链接的变量可以在一个文件的任何地方使用;静态存储时期的变量将在程序执行期间一直存在,并且只能初始化一次。

具体可以参考C Primer Plus第十二章《存储类、链接和内存管理》或是课本。以下列出5个存储类的组合(来自C Primer Plus):
















































存储类 时期 作用域 链接 声明方式
自动 自动 代码块 代码块内
寄存器 自动 代码块 代码块内,使用关键字register
具有外部链接的静态 静态 文件 外部 所有函数之外
具有内部链接的静态 静态 文件 内部 所有函数之外,使用关键字static
空链接的静态 静态 代码块 代码块内,使用关键字static

In the following descriptions about the using of static, which ones are correct?

A. The static declaration can be used to variables and functions.

B. The external static declaration limits the scope of that object.

C. Internal static variables are local to a particular function just as auto variables are.

D. Internal static variables provide permanent storage and are initialized only once.

A正确,B external static具有内部链接,因此被限制了只能被当前文件引用,正确,C内部定义的静态变量具有代码块作用域,正确,D正确。

同时在另外一个文件中引用具有外部链接的变量时需要使用关键字 extern,因此,下面一道题选D。

Suppose that file_a.c and file_b.c can be compiled independently. and they share the following global variables

extern int x; char ch;

which are declared in file_a.c. The allowed global variables declared in file_b.c are

A、extern int x; char ch;

B、extern int x; extern char ch;

C、int x; char ch;

D、int x; extern char ch;

9.改错题注意悬挂指针

例如:

1
2
3
4
char *a[5];
int i = 0;
for (i=0; i<5; i++)
scanf("%s", a[i]);

10.Switch中的break

若switch中的case不加break,将会将该case后的所有语句全部执行完。

11.字段成员的内存分配

字段成员的内存分配是由低位到高位的。例如下面的题目,答案为C:

Suppose declared:

1
2
3
4
5
6
7
struct direction {
unsigned short int east:4,south:4,west:4,north:4; //east为最低位4,north为最高位1
};
union ud{
unsigned short int all;
struct direction d;
} a={0x1234};

the output of printf("%d\n",a.d.south); is

A、1

B、2

C、3

D、4

12.细心审题

C语言考试审题还是很重要的,稍微一不小心看错了一个字母,有可能题目就错了,务必仔细审题。

评论和共享

C语言第四次作业

发布在 C语言

第一题

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
#include <stdio.h>;
void swap(int *a, int *b);
int main (void)
{
int number[10];
int i, j;//循环变量
while (scanf("%d", &amp;number[0]) != EOF)
{
for (i = 1 ; i &lt; 10 ; i++)
scanf("%d", &amp;number[i]);
for (i = 0 ; i &lt; 9 ; i++)
{
for (j = 0 ; j &lt;= 8 - i ; j++)
{
if (number[j] &gt; number[j+1])
swap(&amp;number[j], &amp;number[j+1]);
}
}
for (i = 0 ; i &lt; 10 ; i++)
{
if (i != 0) printf("-&gt;");
printf("%d", number[i]);
}
putchar('\n');
}
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

第二题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>;
int main (void)
{
long number;
char currentByte;
int count;
while (scanf("%ld", &amp;number) != EOF)
{
for (count = 7 ; count &gt;= 0 ; count--)
{
currentByte = (((0xf &lt;&lt; (count * 4)) &amp; number) &gt;&gt; (count * 4)) &amp; 0xf;
printf("%hd(%c) ", currentByte, currentByte);
}
putchar('\n');
}
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
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>;
void swapArray(int a[], int n);
void swap(int *a, int *b);
int main (void)
{
int number[100];
int totalNumber, individualNumber;
int count, i;//循环变量
scanf("%d", &amp;totalNumber);
for (count = 0 ; count &lt; totalNumber; count++)
{
scanf("%d", &amp;individualNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
scanf("%d", &amp;number[i]);
swapArray(number, individualNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
{
if (i != 0) putchar(' ');
printf("%d", number[i]);
}
putchar('\n');
}
return 0;
}
void swapArray(int a[], int n)
{
int startIndex = 0;
int endIndex = n - 1;
while (startIndex &lt;= endIndex)
{
swap(&amp;a[startIndex], &amp;a[endIndex]);
startIndex++;
endIndex--;
}
return;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

第四题

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
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>;
void swap(int *a, int *b);
void moveArray(int a[], int n, int m);//轮移数组,其中n为总个数,m为轮移次数
int main (void)
{
int number[22];
int individualNumber, middleNumber;
int i;//循环变量
while ((scanf("%d", &amp;individualNumber), individualNumber) != 0)
{
scanf("%d", &amp;middleNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
scanf("%d", &amp;number[i]);
moveArray(number, individualNumber, middleNumber);
for (i = 0 ; i &lt; individualNumber ; i++)
{
if (i != 0) putchar(' ');
printf("%d", number[i]);
}
putchar('\n');
}
return 0;
}
void moveArray(int a[], int n, int m)
{
if (m == 0)
{
return;
}
else
{
int temp, count;
temp = a[0];
for (count = 0 ; count &lt; n - 1 ; count++)
a[count] = a[count+1];
a[n-1] = temp;
moveArray(a, n, --m);
}
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

评论和共享

C语言第三次作业

发布在 C语言

第一题 C语言成绩


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
37
#include<stdio.h>
#include<string.h>
void swap(int *score_a, int *score_b, char *name_a, char *name_b);
int main (void)
{
int number, count;
while (scanf("%d", &number) != EOF)
{
putchar('\n');
int scores[number];
char names[number][81];
for (count = 0 ; count < number ; ++count)
scanf("%s%d", names[count], &scores[count]);
int i, j;
for (i = 0 ; i < number ; ++i)
for (j = i ; j < number ; ++j)
if (scores[i] < scores[j])
swap(&scores[i], &scores[j], &names[i][0], &names[j][0]);
for (count = 0 ; count < number ; ++count)
printf("%s %d\n", names[count], scores[count]);
putchar('\n');
}
return 0;
}

void swap(int *score_a, int *score_b, char *name_a, char *name_b)
{
int t;
char str_t[81];
t = *score_a;
*score_a = *score_b;
*score_b = t;
strcpy(str_t, name_a);
strcpy(name_a, name_b);
strcpy(name_b, str_t);
return;
}

第二题 冒泡排序


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(void)
{
int sequence[10];
int count;
int i, j;
int temp;
while (scanf("%d", &sequence[0]) != EOF)
{
for (count = 1 ; count < 10 ; ++count)
scanf("%d", &sequence[count]);
for (i = 0 ; i < 10 ; ++i)
{
for (j = 0 ; j < 8 - i ; ++j)
{
if (sequence[j] > sequence[j+1])
{
temp = sequence[j];
sequence[j] = sequence[j+1];
sequence[j+1] = temp;
}
}
}
for(count = 0 ; count < 10 ; ++count)
printf("%d ", sequence[count]);
putchar('\n');
}
return 0;
}

第三题 n人报数(此题可查找“约瑟夫问题”)


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
37
38
39
#include <stdio.h>
int main (void)
{
int number,count;
int current;
int i;
while (scanf("%d", &number) != EOF)
{
int people[10000];
for (i = 0 ; i < number ; ++i)
people[i] = 1;
count = number;
current = 0;
i = 0;
while (count > 1)
{
if (i >= number)
i = 0;
if (people[i] == 1)
current += 1;
if (count == 1)
break;
if (current == 3)
{
people[i] = 0;
count -= 1;
current = 0;
}
i++;
}
for (i = 0 ; i < number ; ++i)
if (people[i] == 1)
{
printf("%d\n", i + 1);
break;
}
}
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>
#define Swap(x,y) {x = x + y; y = x - y; x = x - y;}

int main (void)
{
int a, b;
int count = 0;
while (scanf("%d%d", &a, &b) != EOF)
{
if (count)
putchar('\n');
count += 1;
printf("Case %d:\n", count);
printf("Before Swap:a=%d b=%d\n", a, b);
Swap(a,b);
printf("After Swap:a=%d b=%d\n", a, b);

}
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
#include <stdio.h>
void mycpy (char s[], char t[], int n);
int main (void)
{
int number;
char sentence[1001];
char result[1001];
int n;
scanf("%d", &number);
getchar();//读取\n
int count;
for (count = 0 ; count < number ; ++count)
{
fgets(sentence, 1001, stdin);
scanf("%d", &n);
getchar();//读取\n
mycpy(result, sentence, n);
printf("%s\n", result);
}
return 0;
}
void mycpy (char s[], char t[], int n)
{
int i;
for (i = 0 ; i < n ; ++i)
{
s[i] = t[i];
}
s[n] = '\0';
return;
}

第六题 统计


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
#include <stdio.h>
int main (void)
{
char currentChar;
int numberCount[10] = {0};
int englishCharCount = 0;
int otherCount = 0;
int count;
while ((currentChar = getchar()) != EOF)
{
switch (currentChar)
{
case '0': numberCount[0] += 1 ; break;
case '1': numberCount[1] += 1 ; break;
case '2': numberCount[2] += 1 ; break;
case '3': numberCount[3] += 1 ; break;
case '4': numberCount[4] += 1 ; break;
case '5': numberCount[5] += 1 ; break;
case '6': numberCount[6] += 1 ; break;
case '7': numberCount[7] += 1 ; break;
case '8': numberCount[8] += 1 ; break;
case '9': numberCount[9] += 1 ; break;
default: break;
}
if ((currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= 'a' && currentChar <= 'z'))
englishCharCount += 1;
else if (!(currentChar >= '0' && currentChar <= '9'))
otherCount += 1;
}
for (count = 0 ; count < 10 ; ++count)
printf("Number %d: %d\n", count, numberCount[count]);
printf("characters: %d\n", englishCharCount);
printf("other: %d\n", otherCount);
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
#include <stdio.h>
int main (void)
{
int sequence[20];
int number;
int count;
int i, j;
int temp;
while (scanf("%d", &number), number != 0)
{
for (count = 0 ; count < number ; ++count)
scanf("%d", &sequence[count]);
for (i = 0 ; i < number ; ++i)
for (j = i ; j < number ; ++j)
if (sequence[i] > sequence[j])
{
temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
}
for (count = 0 ; count < number ; ++count)
{
if (count)
putchar(' ');
printf("%d", sequence[count]);
}
putchar('\n');
}
return 0;
}

评论和共享

C语言第二次作业

发布在 C语言

1、哥德巴赫猜想(2)——改编自习题5.14


题目描述:

哥德巴赫作了如下猜想:一个大于等于4的偶数都是两个素数之和。编写一个程序证明对于在正整数Begin、End之间的偶数,这一猜测成立。将判断一个数是否是素数定义成函数。
本题包含多组测试数据。

输入格式说明:

每组测试数据输入占一行,包括两个正整数,依次为Begin,End,4≤Begin<End≤200,遇文件尾测试结束。

输出格式说明:

输出在规定范围内所有偶数的哥德巴赫猜想形式的分解,存在多种不同输出结果组合时,输出满足要求的结果中分解后第一个数最小的结果,需要在第i组测试数据结果输出前添加一行提示信息:“CASE:i”,表示是第i组测试数据对应的结果,相邻两组测试样例的输出结果之间空一行。

样例输入:

10 20
45 145

样例输出:

CASE:1
COLDBACH’S CONJECTURE:
Every even number n>=4 among 10~20 is the sum of two primes:
10=3+7
12=5+7
14=3+11
16=3+13
18=5+13
20=3+17

CASE:2
COLDBACH’S CONJECTURE:
Every even number n>=4 among 45~145 is the sum of two primes:
46=3+43
48=5+43
50=3+47
52=5+47
54=7+47
56=3+53
58=5+53
60=7+53
62=3+59
64=3+61
66=5+61
68=7+61
70=3+67
72=5+67
74=3+71
76=3+73
78=5+73
80=7+73
82=3+79
84=5+79
86=3+83
88=5+83
90=7+83
92=3+89
94=5+89
96=7+89
98=19+79
100=3+97
102=5+97
104=3+101
106=3+103
108=5+103
110=3+107
112=3+109
114=5+109
116=3+113
118=5+113
120=7+113
122=13+109
124=11+113
126=13+113
128=19+109
130=3+127
132=5+127
134=3+131
136=5+131
138=7+131
140=3+137
142=3+139
144=5+139

解答

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
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <math.h>

int isPrime (int x);
int main (void)
{
int begin, end;
int count, number;
int subnumber1, subnumber2;
for (count = 1 ; scanf("%d%d", &begin, &end) != EOF ; ++count)
{
if (count != 1)
putchar('\n');
printf("CASE:%d\n", count);
printf("COLDBACH'S CONJECTURE:\n");
printf("Every even number n>=4 among %d~%d is the sum of two primes:\n", begin, end);
if (begin % 2 != 0)
begin++;
for (number = begin ; number <= end ; number += 2)
{
for (subnumber1 = 2 ; subnumber1 <= (number/ 2) ; ++subnumber1)
{
subnumber2 = number - subnumber1;
if (isPrime(subnumber2) && isPrime(subnumber1))
{
printf("%d=%d+%d\n", number, subnumber1, subnumber2);
break;
}
}
}
}
return 0;
}

/*************************************************
Function: isPrime
Description: 判断正整数是否为素数
Input: 一个正整数x
Return: 1表示素数 0表示合数
*************************************************/
int isPrime (int x)
{
int i;
for (i = 2 ; i <= sqrt(x) ; ++i)
if (x % i == 0) return 0;
return 1;
}

2、第k个数——改编自习题5.13


题目描述:

输入正整数n和k,输出n中从右端开始的第k个数字的值(k从1开始)。将求n中右端第k个数字定义成函数,如果k超过了n的位数,则函数返回-1;否则返回n中第k个数字。
本题包含多组测试数据。

输入格式说明:

每组测试数据输入占一行,包括两个正整数,依次为n,k,n在1~4000000000之间(闭区间),遇文件尾测试结束。

输出格式说明:

每组测试样例的输出结果占一行,输出函数返回结果。

样例输入:

321 3
421 4
42 12

样例输出:

3
-1
-1

解答:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
int getKFromN (unsigned long n , int k);

int main (void)
{
unsigned long n;
int k;
while (scanf("%lu%d", &n, &k) != EOF)
{
printf("%d\n", getKFromN(n, k));
}
return 0;
}

/*************************************************
Function: getKFromN
Description: 返回n中从右端开始的第k个值
Input: 一个1~4000000000的正整数n 一个正整数k
Return: 一个正整数 n中的第k个数字 -1表示越位
*************************************************/
int getKFromN (unsigned long n , int k)
{
int unit = 0;//表示最后一位
int count;//表示当前位数
unsigned long result = n;//存储结果
for (count = 1 ; count <= k ; count++)
{
unit = result % 10;
result = result / 10;
}
if (!(unit == 0 && result == 0))
return unit;
else
return -1;
}
```

## 3、冰雹数——改编自习题5.11

* * *

### 题目描述:

n(0)是一个给定的正整数,对于i=0,1,2,...,定义:(1)若n(i)是偶数,则n(i+1)=n(i)/2;(2)若n(i)是奇数,则n(i+1)=3n(i)+1;(3)若n(i)是1,则序列结束。
本题包含多组测试数据。


### 输入格式说明:

每组测试数据输入占一行,包括一个正整数n0,n0在1~4000000000之间(闭区间),遇文件尾测试结束。

### 输出格式说明:

输出冰雹数序列以及序列数的总个数,相邻两组测试样例的输出结果之间空一行。

### 样例输入:

77
92


### 样例输出:

Hailstone generated by 77:
77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Number of hailstone generated:23

Hailstone generated by 92:
92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
Number of hailstone generated:18

### 解答:

#include <stdio.h>
unsigned long long getNextHailstoneNumber (unsigned long long n);

int main (void)
{
unsigned long long number;
int count;
int flag = 1;
while (scanf(“%llu”, &number) != EOF)
{
if (!flag)
putchar(‘\n’);
printf(“Hailstone generated by %llu:\n”, number);
count = 0;
while (number != 1)
{
count += 1;
printf(“%llu “, number);
number = getNextHailstoneNumber(number);
}
printf(“1\n”);
printf(“Number of hailstone generated:%d\n”, count + 1);
flag = 0;
}
return 0;
}

/*
Function: getNextHailstoneNumber
Description: 返回以n为起始数的下一个冰雹数
Input: 一个的正整数n
Return: 一个正整数,表明n的下一个冰雹数
*/

unsigned long long getNextHailstoneNumber (unsigned long long n)
{
if (n % 2 == 0)
return n / 2;
else
return 3 * n + 1;
}

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

## 4、计算题——改编自习题5.10

* * *

### 题目描述:

编程计算s=1+1/2!+1/3!+1/4!+...+1/n!。n由终端输入,将计算n!定义成函数。
本题包含多组测试数据。

### 输入格式说明:

每组测试数据输入占一行,包括一个正整数n,n在1~20之间(闭区间),遇文件尾测试结束。

### 输出格式说明:

每组测试样例的输出结果占一行,输出s,保留18位小数。
注:20!=2432902008176640000。

### 样例输入:

1
2

### 样例输出:

1.000000000000000000
1.500000000000000000

### 解答:

#include <stdio.h>
long long getFactorial (int n);

int main (void)
{
int number;
int i;//循环变量
double s;
while (scanf(“%d”, &number) != EOF)
{
s = 0;
for (i = 1 ; i <= number ; ++i)
{
s += 1.0 / getFactorial(i);
}
printf(“%.18f\n”, s);
}
return 0;
}

/*
Function: getFactorial
Description: 返回n的阶乘
Input: 一个正整数n
Return: 一个正整数n!
*/
long long getFactorial (int n)
{
int count;
long long result = 1;
for (count = 2 ; count <= n; ++count)
{
result *= count;
}
return result;
}

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

## 5、掷骰子游戏——改编自习题5.1

* * *

### 题目描述:

编写一个模拟“投掷双骰子”的游戏程序。游戏规则:每轮投两次,取两次的和,第一轮若和为7或11则获胜,游戏结束;若和为2,3,或12则输了,失败结束;若和为其他数字,则将此值作为自己的点数,继续第二轮,第三轮⋯⋯直到某轮的和等于该点数则获胜,若出现和为7,则输掉游戏。模拟每掷一次骰子的随机取数规则:输入一个非负整数,称为启动数,你需要计算得到启动数各位数之和记为sum,现将1~6这6张数字卡片按顺时钟方向摆放一个环,一只蚂蚁每次从数字为1的卡片摆放的位置出发,这时你告诉蚂蚁按顺时钟方向走sum步,蚂蚁最终停留位置上的卡片数字即视为本次掷骰子得到的点数。
本题包含多组测试数据。


### 输入格式说明:

第一行输入n表示测试样例的个数,后面紧接着有n行输入,每行输入包含两个非负整数记为a,b,整数范围在0~10000之间(闭区间),视为第一轮两次投掷的启动数,若进行到了第n轮时,则第n轮两次投掷的启动数分别看作是a+n-1,b+n-1;


### 输出格式说明:

每组测试样例的输出结果占一行,输出本次游戏的最终结果。

### 样例输入:

3
1234 1
1 42
0 422

### 样例输出:

success!
fail!
success!

### 解答:

#include <stdio.h>
int calcSum (int startNumber);

int main (void)
{
int number;
int count;
int a, b;
int dieA, dieB, dieSum;
int winNumber;
int n;
scanf(“%d”, &number);
for (count = 0 ; count < number ; ++count)
{
scanf(“%d%d”, &a, &b);
dieA = calcSum(a) % 6 + 1;
dieB = calcSum(b) % 6 + 1;
dieSum = dieA + dieB;
if (dieSum == 7 || dieSum == 11)
{
printf(“success!\n”);
continue;
} else if (dieSum == 2 || dieSum == 3 || dieSum == 12)
{
printf(“fail!\n”);
continue;
} else
{
winNumber = dieSum;
}
n = 1;//此处与题目描述不符
do
{
dieA = calcSum(a + n) % 6 + 1;
dieB = calcSum(b + n) % 6 + 1;
dieSum = dieA + dieB;
n += 1;
if (dieSum == 7)
{
printf(“fail!\n”);
break;
}
}while (dieSum != winNumber);
if (dieSum == winNumber)
printf(“success!\n”);
}

return 0;

}

/*
Function: calcSum
Description: 计算启动数的各位的和sum
Input: 一个正整数n(启动数)
Return: 一个正整数n(启动数各个位数的和)
*/
int calcSum (int startNumber)
{
int unit = 0;
int result = startNumber;
int sum = 0;
while (result != 0)
{
unit = result % 10;
sum += unit;
result = result / 10;
}
return sum;
}
`

评论和共享

C语言第一次作业

发布在 C语言

2016年4月1日C语言作业答案及遇到的问题

题目及解答

2.一年中的第几天——改编自习题4.3


原题链接

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
#include<stdio.h>;
int main (void)
{
int year, month, day;
int count;
int isLeap;
while (scanf("%d%d%d", &year, &month, &day) == 3)
{
count = 0;
isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
count += day;
month -= 1;
switch (month)
{
case 12: count += 31; month -= 1;
case 11: count += 30; month -= 1;
case 10: count += 31; month -= 1;
case 9: count += 30; month -= 1;
case 8: count += 31; month -= 1;
case 7: count += 31; month -= 1;
case 6: count += 30; month -= 1;
case 5: count += 31; month -= 1;
case 4: count += 30; month -= 1;
case 3: count += 31; month -= 1;
case 2: count += 28 + isLeap; month -= 1;
case 1: count += 31; month -= 1;
default: break;
}
printf("该日期是这一年中的第%d天\n", count);
}
return 0;
}

3.税金额度计算——改编自习题4.4


原题链接

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* ZhihaoChen CSEE 1501 */
#include<stdio.h>;
/* Prototype Declaration */
void if_else_process (double x);
void switch_process (double x);
/* Main Function */
int main(void)
{
int number;
scanf("%d", &number);
int t; double x;
int count;
for (count = 0 ; count & number ; ++count)
{
scanf("%d%lf", &t, &x);
switch (t)
{
case 0:
if_else_process(x);
break;
case 1:
switch_process(x);
break;
case 2:
if_else_process(x);
switch_process(x);
break;
default:
break;
}
if (count != (number - 1))
printf("\n");
}
return 0;
}
/* data processing and output */
void if_else_process (double x)
{
double output;
if (x < 1000)
output = 0;
else if (x >= 1000 && x < 2000)
output = x * 0.05;
else if (x >= 2000 && x < 3000)
output = x * 0.10;
else if (x >= 3000 && x < 4000)
output = x * 0.15;
else if (x >= 4000 && x < 5000)
output = x * 0.20;
else if (x > 5000)
output = x * 0.25;
printf ("After if-else processing,the amount of tax to be paid is : %.2f\n", output);
}

void switch_process (double x)
{
double output;
switch((int)x / 1000)
{
case 0: output = 0; break;
case 1: output = x * 0.05; break;
case 2: output = x * 0.10; break;
case 3: output = x * 0.15; break;
case 4: output = x * 0.20; break;
default: output = x * 0.25; break;
}
printf("After switch processing,the amount of tax to be paid is : %.2f\n", output);
}

4.两个实数的四则运算——改编自习题4.5


原题链接

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* ZhihaoChen CSEE 1501 */
#include<stdio.h>;
/* Prototype Declaration */
void if_else_process(double a, double b, char c);
void switch_process(double a, double b, char c);
/* Main Function */
int main(void)
{
int t, count;
double a, b;
char c;
for (count = 0 ; (scanf("%d%lf%lf %c", &t, &a, &b, &c) != EOF) ; ++count)
{
if(count != 0)
printf("\n");
switch (t)
{
case 0:
if_else_process(a, b, c);
break;
case 1:
switch_process(a, b, c);
break;
case 2:
if_else_process(a, b, c);
switch_process(a, b, c);
break;
default:
break;
}
}
return 0;
}
/* data processing and output */
void if_else_process (double a, double b, char c)
{
double output = 0.0;
if (c == '+')
output = a + b;
if (c == '-')
output = a - b;
if (c == '*')
output = a * b;
if (c == '/')
output = a / b;
printf ("After if-else processing,the result is : %.2f\n", output);
}

void switch_process (double a, double b, char c)
{
double output = 0.0;
switch(c)
{
case '+': output = a + b; break;
case '-': output = a - b; break;
case '*': output = a * b; break;
case '/': output = a / b; break;
default: break;
}
printf("After switch processing,the result is : %.2f\n", output);
}

5.合并空格字符输出——改编自习题4.7


原题链接

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
#include <stdio.h>
#define CHAR 0
#define SPACE 1

int main (void)
{
char character;
int status = CHAR;
while ((character = getchar()) != EOF)
{
if ((character == ' ') && (status == CHAR))
{
status = SPACE;
printf(" ");
}
if ((character != ' ') && (status == CHAR))
{
printf("%c", character);
}
if ((character != ' ') && (status == SPACE))
{
status = CHAR;
printf("%c", character);
}
}
return 0;
}

6.求π的近似值——改编自习题4.10


原题链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <math.h>

int main (void)
{
double item = 1.0;
double pi = 0.0;
int count;
for (count = 0 ; (item >= 1.0e-5) ; ++count)
{
item = 1.0/(2 * count + 1);
if (count % 2 == 0)
pi += item;
else
pi += (-item);
}
printf("%.9f\n", pi * 4);
return 0;
}

7.验证哥德巴赫猜想——改编自习题4.13


原题链接

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
#include <stdio.h>
#include <math.h>

int isPrime (int x);

int main (void)
{
int number, testnum;
scanf ("%d", &number);
int count, subnumber1, subnumber2;
for (count = 0 ; count < number ; ++count)
{
scanf("%d", &testnum);
for (subnumber1 = 2 ; subnumber1 <= (testnum / 2) ; ++subnumber1)
{
subnumber2 = testnum - subnumber1;
if (isPrime(subnumber2) && isPrime(subnumber1))
{
printf("%d=%d+%d\n", testnum, subnumber1, subnumber2);
break;
}
}
}
return 0;
}

int isPrime (int x)
{
int i;
for (i = 2 ; i <= sqrt(x) ; ++i)
if (x % i == 0) return 0;
return 1;
}

8.正整数相加求平均数——改编自习题4.17


原题链接

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
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
int main (void)
{
int number;
scanf("%d", &number);
int count, subcount, status; int n, positiveNumber;
long sumNumber;
double averageNumber;
for (count = 0 ; count < number ; ++count)
{
n = 0; sumNumber = 0;
positiveNumber = 0;
averageNumber = 0.0;
scanf("%d", &status);
switch(status)
{
case 0:
for (subcount = 0 ; subcount < 10 ; ++subcount)
{
scanf("%d", &n);
if (n <= 0) continue;
sumNumber += n;
positiveNumber += 1;
}
averageNumber = (double)sumNumber / positiveNumber;
if (positiveNumber != 0)
printf("In \"continue\" way,numbers=%d,average=%f\n", positiveNumber, averageNumber);
break;
case 1:
for (subcount = 0 ; subcount < 10 ; ++subcount)
{
scanf("%d", &n);
if(n > 0)
{
sumNumber += n;
positiveNumber += 1;
}
}
averageNumber = (double)sumNumber / positiveNumber;
if (positiveNumber != 0)
printf("In \"no continue\" way,numbers=%d,average=%f\n", positiveNumber, averageNumber);
break;
default:
break;
}
}
return 0;
}

要注意的问题

1.注意审题,比如第六题注意到“最后一项”的绝对值,再比如说第三题注意到输出格式逗号前后都没有空格

2.注意数据类型的范围

3.注意浮点数的科学计数法表示

4.注意条件运算要不要加等于号

5.逻辑错误比语法错误更难检查,要关注程序的细节

6.注意scanf在读取的字符(%c)时候会识别’ ‘,所以在格式化字符串中用’ %c’或者’%1s’均可(后者无法通过系统)

7.注意浮点数的运算存在精度损失,例:double i = 1/e-5 而(int)i=99999,在作为循环条件时需要格外注意,一般情况下最好避免,或者+0.0001再取整

评论和共享

  • 第 1 页 共 1 页
作者的图片

码龙黑曜

iOS开发者/计算机科学/兽人控


华中科技大学 本科在读


Wuhan, China