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};
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; }
#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; } ```
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; }
#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; }
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; } `
/* 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); }