三月三十日第三次实验
实验一:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void print_text(int line, int col, char text[]); void print_spaces(int n); void print_blank_lines(int n); int main() { int line, col, i; char text[N] = "hi, April~"; srand(time(0)); for(i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); } return 0; } void print_spaces(int n) { int i; for(i = 1; i <= n; ++i) printf(" "); } void print_blank_lines(int n) { int i; for(i = 1; i <= n; ++i) printf("\n"); } void print_text(int line, int col, char text[]) { print_blank_lines(line-1); print_spaces(col-1); printf("%s", text); }
问题回答:该代码旨在分别随机地从1~25,1~80中取两个数作为line与col,随后打印line-1个空行后打印col-1个空格,再打印相应文本。随后程序暂停1000毫秒后继续进行。
实验二:
task2_1.c
#include <stdio.h> long long fac(int n); int main() { int i, n; printf("Enter n: "); scanf("%d", &n); for (i = 1; i <= n; ++i) printf("%d! = %lld\n", i, fac(i)); return 0; } long long fac(int n) { static long long p = 1; printf("p = %lld\n", p); p = p * n; return p; }
实验图片:
task2_2.c
#include <stdio.h> int func(int, int); int main() { int k = 4, m = 1, p1, p2; p1 = func(k, m); p2 = func(k, m); printf("%d, %d\n", p1, p2); return 0; } int func(int a, int b) { static int m = 0, i = 2; i += m + 1; m = i + a + b; return m; }
实验图片:
问题回答:static所修饰的变量在被赋初值后会持续保留在作用域中,直到变量改变或程序结束。
实验三:
#include <stdio.h> long long func(int n); int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = func(n); printf("n = %d, f = %lld\n", n, f); } return 0; } long long func(int n) { if(n == 1) return 1; else return func(n-1) * 2 + 1; }
实验图片:
实验四:
#include <stdio.h> int func(int n, int m); int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); return 0; } int func(int n, int m) { if(n > m && m != 0) return func(n - 1, m) + func(n - 1, m - 1); else if(n < m) return 0; else if(m == 0 || n == m) return 1; }
实验图片:
实验五:
迭代法:
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while(scanf("%d%d", &x, &y) != EOF) { ans = mypow(x, y); printf("%d的%d次方: %g\n\n", x, y, ans); } return 0; } double mypow(int x, int y) { double i, X = x, sum = 1; if(y > 0){ for(i = 1; i <= y; i++) sum *= X; return sum;} else if(y < 0){ for(i = 1; i <= -y; i++) sum *= 1 / X; return sum;} else if(y == 0) return 1; }
实验图片:
函数递归法:
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while(scanf("%d%d", &x, &y) != EOF) { ans = mypow(x, y); printf("%d的%d次方: %g\n\n", x, y, ans); } return 0; } double mypow(int x, int y) { double X = x; if(y > 0) { if(y == 1) return x; else if(y != 1) return mypow(x, y - 1) * x; } else if(y < 0) { if(y == 1) return 1 / X; else if(y != 1) return mypow(X, y + 1) * 1 / X; } else return 1; }
实验图片:
实验六:
#include<stdio.h> #include<math.h> void hanoi(unsigned int n, char from, char temp, char to); void moveplate(unsigned int n, char from, char to); static double total, delta;//total定义:从程序起总共移动次数;delta定义:最近上一次移动的移动次数 int main() { unsigned int n; int num;//定义:本次移动次数 while(scanf("%d", &n) != EOF) { hanoi(n,'A','B','C'); printf("\n"); num = total - delta; printf("一共移动了%d次\n", num); delta = total; printf("\n"); } return 0; } void hanoi(unsigned int n, char from, char temp, char to) { if(n == 1){ moveplate(n, from, to);} else { hanoi(n - 1, from, to, temp); moveplate(n, from, to); hanoi(n - 1, temp, from, to); } } void moveplate(unsigned int n, char from, char to) { printf("%u : %c --> %c \n", n, from, to);total++; }
实验图片:
实验七:
#include<stdio.h> #include<math.h> int is_prime(int x) { int i; for(i = 2; i <= sqrt(x); i++) if(x % i == 0) return 0; return 1; } void print(int x) { int i; for(i = 2; i <= x / 2; i++) { if((is_prime(i) && is_prime(x - i))) { printf("%d = %d + %d\n", x, i, x - i); return; } } } int main() { int i; for(i = 2; i <= 20; i += 2) print(i); return 0; }
实验图片:
实验八:
#include <stdio.h> #include <math.h> long func(long s); int main() { long s, t; printf("Enter a number: "); while (scanf("%ld", &s) != EOF) { t = func(s); printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long func(long s) { long yushu, result = 0, j = 1, i; for(i = 1; s != 0; i++) { yushu = s % 10; if(yushu % 2 == 1) { result += yushu * j; j *= 10; } s /= 10; } return result; }
实验图片: