├── README.md └── code ├── 02-0.整数四则运算.c ├── 02-1.厘米换算英尺英寸.c ├── 02-2.然后是几点.c ├── 02-3.逆序的三位数.c ├── 02-4.BCD解密.c ├── 03-0.超速判断.c ├── 03-1.三天打鱼两天晒网.c ├── 03-2.用天平找小球.c ├── 03-3.12-24小时制.c ├── 03-4.成绩转换.c ├── 04-0.求符合给定条件的整数集.c ├── 04-1.水仙花数.c ├── 04-2.打印九九口诀表.c ├── 04-3.统计素数并求和.c ├── 04-4.猜数字游戏.c ├── 05-0.求序列前N项和.c ├── 05-1.约分最简分式.c ├── 05-2.念数字.c ├── 05-3.求a的连续和.c ├── 06-0.混合类型数据格式化输入.c ├── 06-1.简单计算器.c ├── 06-2.字符串字母大小写转换.c ├── 06-3.单词长度.c ├── 07-0.写出这个数.c ├── 07-2.A+B和C.c ├── 07-3.数素数.c ├── 08-0.查找整数.c ├── 10-3.字符串逆序.c ├── 10-4.字符串循环左移.c ├── 11-0.平面向量加法.c └── 11-1.通讯录的录入与显示.c /README.md: -------------------------------------------------------------------------------- 1 | #中国大学MOOC-翁恺-C语言程序设计习题集 2 | http://www.patest.cn/contests/mooc-c -------------------------------------------------------------------------------- /code/02-0.整数四则运算.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | 7 | printf("%d + %d = %d\n", a, b, a+b); 8 | printf("%d - %d = %d\n", a, b, a-b); 9 | printf("%d * %d = %d\n", a, b, a*b); 10 | printf("%d / %d = %d\n", a, b, a/b); 11 | 12 | 13 | } -------------------------------------------------------------------------------- /code/02-1.厘米换算英尺英寸.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int cmlen; 6 | scanf("%d",&cmlen); 7 | int foot= cmlen/100.0/0.3048; 8 | int inch = (cmlen/100.0/0.3048-foot)*12; 9 | printf("%d %d\n",foot,inch); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /code/02-2.然后是几点.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | 7 | int c = 0; 8 | int hour_add = 0; 9 | 10 | int a_hour = a / 100; 11 | int a_min = a % 100; 12 | 13 | while(b > 0) { 14 | 15 | } 16 | 17 | c = a_hour * 100 + a_min; 18 | 19 | printf("%d", c); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /code/02-3.逆序的三位数.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num; 5 | 6 | scanf("%d", &num); 7 | 8 | while(num > 0) { 9 | int pos = num % 10; 10 | num = num / 10; 11 | 12 | if(pos != 0) { 13 | printf("%d", pos); 14 | } 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /code/02-4.BCD解密.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void convert(int hex) { 4 | if(hex / 16 > 0) { 5 | convert(hex / 16); 6 | } 7 | printf("%d", hex % 16); 8 | } 9 | 10 | int main() { 11 | int hex = 0; 12 | scanf("%d", &hex); 13 | 14 | convert(hex); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /code/03-0.超速判断.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int speed = 0; 5 | scanf("%d", &speed); 6 | 7 | if(speed > 60) { 8 | printf("Speed: %d - Speeding", speed); 9 | } else { 10 | printf("Speed: %d - OK", speed); 11 | } 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /code/03-1.三天打鱼两天晒网.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num = 0; 5 | scanf("%d", &num); 6 | 7 | int result = num % 5; 8 | if(result > 0 && result <=3) { 9 | printf("Fishing in day %d", num); 10 | } else { 11 | printf("Drying in day %d", num); 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /code/03-2.用天平找小球.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a = 0, b = 0, c = 0; 5 | 6 | scanf("%d %d %d", &a, &b, &c); 7 | 8 | if(a != b) { 9 | if(a != c) { 10 | printf("A"); 11 | } else { 12 | printf("B"); 13 | } 14 | } else { 15 | printf("C"); 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /code/03-3.12-24小时制.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int hour = 0; 5 | int min = 0; 6 | int isPm = 0; 7 | 8 | scanf("%d:%d", &hour, &min); 9 | 10 | if(hour >= 12) { 11 | isPm = 1; 12 | if(hour != 12) 13 | hour -= 12; 14 | } 15 | 16 | if(isPm) { 17 | printf("%d:%d PM", hour, min); 18 | } else { 19 | printf("%d:%d AM", hour, min); 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /code/03-4.成绩转换.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int grade; 5 | scanf("%d", &grade); 6 | 7 | if(grade < 60) { 8 | printf("E"); 9 | } else { 10 | if(grade >= 90) { 11 | printf("A"); 12 | } else if(grade >=80) { 13 | printf("B"); 14 | } else if(grade >= 70) { 15 | printf("C"); 16 | } else { 17 | printf("D"); 18 | } 19 | 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /code/04-0.求符合给定条件的整数集.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int start = 0; 5 | scanf("%d", &start); 6 | int i = 0, j = 0, k = 0; 7 | int counter = 0; 8 | 9 | for(i = start; i <= start+3; i++) { 10 | counter = 0; 11 | for(j = start; j <= start+3; j++) { 12 | for(k = start; k <= start+3; k++) { 13 | if(i != j && j != k && i != k) { 14 | if(++counter != 6) { 15 | printf("%d%d%d ", i, j, k); 16 | } else { 17 | printf("%d%d%d", i, j, k); 18 | } 19 | 20 | } 21 | 22 | } 23 | } 24 | printf("\n"); 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /code/04-1.水仙花数.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int myPow(int num, int i) { 4 | if(num == 0) return num; 5 | 6 | int counter = 0; 7 | int result = num; 8 | while(counter < i-1) { 9 | result *= num; 10 | counter++; 11 | } 12 | return result; 13 | } 14 | 15 | int main() { 16 | int num = 0; 17 | scanf("%d", &num); 18 | 19 | int sum = 0; 20 | int i = 0; 21 | int max_value = myPow(10, num); 22 | //printf("%d", max_value); 23 | 24 | for(i = myPow(10, num-1); i 2 | 3 | int main() { 4 | int max = 0; 5 | scanf("%d", &max); 6 | 7 | int i = 0, j = 0; 8 | for(i = 1; i <= max; i++) { 9 | for(j = 1; j <= i; j++) { 10 | printf("%d*%d=%-4d", j, i, i*j); 11 | 12 | } 13 | printf("\n"); 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /code/04-3.统计素数并求和.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int start = 0, end = 0; 5 | 6 | int i = 0, j = 0; 7 | int counter = 0; 8 | int sum = 0; 9 | 10 | scanf("%d %d", &start, &end); 11 | if(start == 2) { 12 | counter++; 13 | sum += 2; 14 | } 15 | for(i = start; i <= end; i++) { 16 | for(j = 2; j < i; j++) { 17 | if(i % j == 0) { 18 | break; 19 | } 20 | 21 | if(j == i-1) { 22 | counter++; 23 | sum += i; 24 | //printf("%d\n", start); 25 | } 26 | } 27 | } 28 | printf("%d %d", counter, sum); 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /code/04-4.猜数字游戏.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int goal = 0; 5 | int max_guess = 0; 6 | 7 | scanf("%d %d", &goal, &max_guess); 8 | 9 | int temp = 0; 10 | 11 | int counter = 0; 12 | 13 | while(counter < max_guess) { 14 | scanf("%d", &temp); 15 | 16 | if(temp < 0) { 17 | printf("Game Over"); 18 | return 0; 19 | 20 | } 21 | 22 | if(temp < goal) { 23 | printf("Too small"); 24 | } else if(temp > goal) { 25 | printf("Too big"); 26 | } else { 27 | if(counter == 0) { 28 | printf("Bingo!"); 29 | return 0; 30 | } else if(counter < 3) { 31 | printf("Lucky You!"); 32 | return 0; 33 | } else { 34 | printf("Good Guess!"); 35 | return 0; 36 | } 37 | } 38 | counter ++; 39 | } 40 | 41 | 42 | printf("Game Over"); 43 | return 0; 44 | 45 | 46 | } -------------------------------------------------------------------------------- /code/05-0.求序列前N项和.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int input = 0; 5 | scanf("%d", &input); 6 | 7 | int a = 2; //分子 8 | int b = 1; //分母 9 | double c = 0.0; //结果 10 | 11 | int counter = 1; 12 | double sum = 0.0; //和 13 | 14 | while(counter <= input) { 15 | c = (double)a / b; 16 | sum += c; 17 | 18 | int temp = a; 19 | a = a + b; 20 | b = temp; 21 | 22 | counter++; 23 | } 24 | printf("%.2lf", sum); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /code/05-1.约分最简分式.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a = 0; //分子 5 | int b = 0; //分母 6 | scanf("%d/%d", &a, &b); 7 | 8 | int _a = 0; 9 | int _b = 0; 10 | 11 | //求最大公约数就行了 12 | if(a < b) { //保证a>b 13 | _a = b; 14 | _b = a; 15 | 16 | } else { 17 | _a = a; 18 | _b = b; 19 | } 20 | 21 | //辗转相除开始 22 | int c = _a % _b; 23 | while(c != 0) { 24 | _a = _b; 25 | _b = c; 26 | 27 | c = _a % _b; 28 | } 29 | 30 | //printf("%d\n", _b); 31 | if(c != _a) { 32 | printf("%d/%d", a/_b, b/_b); 33 | } else { 34 | printf("%d/%d", a/b); 35 | } 36 | 37 | 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /code/05-2.念数字.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void convert(int num, char strs[10][5], int output_space) { 4 | if(num == 0) { 5 | return ; 6 | } 7 | 8 | convert(num / 10, strs, 1); 9 | 10 | if(output_space) { 11 | printf("%s ", strs[num % 10]); 12 | } else { 13 | printf("%s", strs[num % 10]); 14 | } 15 | } 16 | 17 | int main() { 18 | char strs[10][5] = { 19 | "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" 20 | }; 21 | 22 | int num = 0; 23 | scanf("%d", &num); 24 | 25 | if(num < 0) { 26 | printf("fu "); 27 | num = -num; 28 | } 29 | 30 | if(num != 0) { 31 | convert(num, strs, 0); 32 | } else { 33 | printf("ling"); 34 | } 35 | 36 | 37 | 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /code/05-3.求a的连续和.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a = 0, n = 0; 5 | 6 | scanf("%d %d", &a, &n); 7 | int num = a; 8 | 9 | int i = 0; 10 | int sum = 0; 11 | for(i=0; i 2 | 3 | int main() { 4 | float f1 = 0.0; 5 | int i1 = 0; 6 | char c1 = 0; 7 | float f2 = 0.0; 8 | 9 | scanf("%f %d %c %f", &f1, &i1, &c1, &f2); 10 | 11 | printf("%c %d %.2f %.2f", c1, i1, f1, f2); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /code/06-1.简单计算器.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char c = 0; 5 | int result = 0; 6 | int temp_num = 0; 7 | 8 | scanf("%d", &result); 9 | while(1) { 10 | scanf("%c", &c); 11 | 12 | /* if(c == '+' || c == '-' || c == '*' || c == '/') { 13 | if(c == '+') { 14 | state = 1; 15 | } else if(c == '-') { 16 | state = 2; 17 | } else if(c == '*') { 18 | state = 3; 19 | } else if(c == '/') { 20 | state = 4; 21 | } 22 | }*/ 23 | 24 | if(c == '=') { 25 | break; 26 | } 27 | 28 | scanf("%d", &temp_num); 29 | 30 | if(c == '+') { 31 | result += temp_num; 32 | } else if(c == '-') { 33 | result -= temp_num; 34 | } else if(c == '*') { 35 | result *= temp_num; 36 | } else if(c == '/') { 37 | if(temp_num == 0) { 38 | printf("ERROR"); 39 | return 0; 40 | } 41 | result /= temp_num; 42 | } else if(c != '=') { 43 | printf("ERROR"); 44 | return 0; 45 | } 46 | } 47 | 48 | printf("%d", result); 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /code/06-2.字符串字母大小写转换.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char str[81]; 5 | char c = 0; 6 | 7 | int i = 0; 8 | 9 | 10 | while((c = getchar()) != '#') { 11 | str[i++] = c; 12 | } 13 | str[i] = '\0'; 14 | 15 | 16 | for(i = 0; str[i] != '\0'; i++) { 17 | if(str[i] >= 'A' && str[i] <= 'Z') { 18 | str[i] = str[i] + 32; 19 | } else if(str[i] >= 'a' && str[i] <= 'z') { 20 | str[i] = str[i] - 32; 21 | } 22 | } 23 | 24 | puts(str); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /code/06-3.单词长度.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char str[81]; 6 | memset(str, '.', sizeof(str)); 7 | char c = 0; 8 | 9 | int begin_flag = 0; 10 | int counter = 0; 11 | gets(str); 12 | 13 | if(str[0] == '.') return 0; 14 | 15 | int i = 0; 16 | for(i = 0; str[i] != '.'; i++) { 17 | c = str[i]; 18 | 19 | if(c != ' ') { 20 | if(begin_flag == 0) { 21 | begin_flag = 1; 22 | 23 | } 24 | counter++; 25 | } 26 | 27 | if(c == ' ' && str[i+1] != ' ') { 28 | begin_flag = 0; 29 | printf("%d ", counter); 30 | counter = 0; 31 | } 32 | } 33 | if(str[i] == '.' && counter != 0) printf("%d", counter); 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /code/07-0.写出这个数.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void convert(int num, char strs[10][5], int output_space) { 4 | if(num == 0) { 5 | return ; 6 | } 7 | 8 | convert(num / 10, strs, 1); 9 | 10 | if(output_space) { 11 | printf("%s ", strs[num % 10]); 12 | } else { 13 | printf("%s", strs[num % 10]); 14 | } 15 | } 16 | 17 | 18 | int main() { 19 | char strs[10][5] = { 20 | "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" 21 | }; 22 | char str[81]; 23 | int sum = 0; 24 | 25 | gets(str); 26 | int i = 0; 27 | char c = 0; 28 | while((c = str[i]) != '\0') { 29 | if(c >= '0' && c <= '9') 30 | sum += c - '0'; 31 | i++; 32 | } 33 | 34 | if(sum != 0) { 35 | convert(sum, strs, 0); 36 | } else { 37 | printf("ling"); 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /code/07-2.A+B和C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long a = 0, b = 0, c = 0; 5 | int total = 0; 6 | 7 | scanf("%d", &total); 8 | int i = 0; 9 | for(i = 0; i < total; i++) { 10 | scanf("%ld %ld %ld", &a, &b, &c); 11 | if(a + b > c) { 12 | printf("Case #%d: true\n", i+1); 13 | } else { 14 | printf("Case #%d: false\n", i+1); 15 | } 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /code/07-3.数素数.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | 6 | return 0; 7 | } -------------------------------------------------------------------------------- /code/08-0.查找整数.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n = 0, x = 0; 5 | int nums[20]; 6 | scanf("%d %d", &n, &x); 7 | 8 | int i = 0; 9 | 10 | for(i = 0; i 2 | 3 | int main() { 4 | char str[81]; 5 | gets(str); 6 | int end = 0; 7 | 8 | while(str[end+1] != '\0') { 9 | end ++; 10 | } 11 | 12 | int start = 0; 13 | 14 | while(start < end) { 15 | char temp = str[start]; 16 | str[start] = str[end]; 17 | str[end] = temp; 18 | 19 | start++; 20 | end--; 21 | } 22 | puts(str); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /code/10-4.字符串循环左移.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char str[81]; 5 | int n = 0; 6 | gets(str); 7 | 8 | scanf("%d", &n); 9 | int end = 0; 10 | 11 | while(str[end + 1] != '\0') end++; 12 | 13 | int i = 0; 14 | int j = 0; 15 | for(i = 0; i 2 | 3 | int main() { 4 | float x1 = 0, y1 = 0, x2 = 0, y2 = 0; 5 | 6 | scanf("%f %f %f %f", &x1, &y1, &x2, &y2); 7 | 8 | float c = x1 + x2; 9 | float d = y1 + y2; 10 | 11 | if((int)c == 0) { 12 | c = 0; 13 | } 14 | 15 | if((int)d == 0) { 16 | d = 0; 17 | } 18 | 19 | printf("(%.1f, %.1f)", c, d); 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /code/11-1.通讯录的录入与显示.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Date { 5 | int year; 6 | int month; 7 | int day; 8 | }; 9 | 10 | struct Student { 11 | char name[81]; 12 | struct Date date; 13 | char gender; 14 | char telephone[20]; 15 | char mobile[20]; 16 | }; 17 | 18 | int main() { 19 | int total = 0; 20 | scanf("%d", &total); 21 | int i = 0; 22 | 23 | if(total == 0) return 0; 24 | 25 | struct Student *students = (struct Student *)malloc(total * sizeof(struct Student)); 26 | 27 | for(i = 0; i= total-1 || goal < 0) { 45 | printf("Not Found\n"); 46 | continue; 47 | } else { 48 | printf("%s %s %s %c %04d/%02d/%02d\n", students[goal].name, students[goal].telephone, 49 | students[goal].mobile, students[goal].gender, students[goal].date.year, 50 | students[goal].date.month, students[goal].date.day); 51 | } 52 | } 53 | 54 | return 0; 55 | } --------------------------------------------------------------------------------