一
运行代码为
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> #define N 80 void printText(int line, int col, char text[]); void printSpaces(int n); void printBlankLines(int n); int main() { int line,col,i; char text[N] = "Hi,May~~"; srand(time(0)); for (i = 0; i <= 10; i++) { line = rand() % 25; col = rand() % 80; printText(line, col, text); Sleep(1000); } return 0; } void printBlankLines(int n) { int i; for (i = 1; i <= n; i++) printf("\n"); } void printSpaces(int n) { int i; for (i = 1; i <= n; i++) printf(" "); } void printText(int line, int col, char text[]) { printBlankLines(line - 1); printSpaces(col - 1); printf("%s", text); }
功能
随机生成数0-24和0-80的数赋值到line和col,是打印“hi may”时前打印col-1个空格,且每个“hi,may”间隔line-1行、且每生成一个”Hi may“后做1000ms的停留。
二
1.运行原源代码
#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! = %11d\n", i, fac(i)); return 0; } long long fac(int n) { static long long p = 1; p = p * n; return p; }
添加“printf("p = %lld\n", p);”在17与19行间,运行结果为
2.运行代码为
#include<stdio.h> long func(int ,int ); int main() { int k = 4, m = 1, p1, p2; p1 = func(k, m); p2 = func(k, m); printf("%d,%d", 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 fun(int n); int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = fun(n); printf("n=%d,f=%lld\n", n, f); } return 0; } long long fun(int n) { long long a; if (n == 0) return 0; else if (n == 1) return 1; if (n != 0) return 2 * fun(n - 1) + 1; }
运行结果为
四
运行代码为
#include<stdio.h> void han (int n); long long fun(int n); int main() { int n,p; char A='A',B='B',C='C'; while (scanf("%d", &n) != EOF) { han(n,A,C,B); p = fun(n); printf("一共移动了%d次\n", p); } return 0; } void han(int n,char x,char y,char z) { static int p=0; if (n == 1) { printf("第%d个盘子:%c-->%c\n", n, x, y); } if (n != 1) { han(n-1,x,z,y); printf("第%d个盘子:%c-->%c\n",n,x,y); han(n-1,z,y,x); } } long long fun(int n) { long long a; if (n == 1) return 1; if (n != 0) return 2 * fun(n - 1) + 1; }
运行结果为
五
运行代码为
#include<stdio.h> int is_prime(int x); int main() { int j = 1,p,q,k; for (j = 0; j <= 20; j=j+2) { for (p = 1;p < j; p++) { k = is_prime(p); if (k == 1) { q = j - p; k = is_prime(q); if (k == 1) { printf("%d=%d+%d\n", j, p, q); break; } } } } return 0; } int is_prime(int x) { int i; for (i = 2; i < x; i++) if (x % i == 0)break; if (i == x) return 1; else return 0; }
运行结果为
六
运行代码为
#include<stdio.h> long fun(long s); int main() { long s, t; printf("enter a number:"); while (scanf("%ld", &s) != EOF) { t = fun(s); printf("new number is: %1d\n\n", t); printf("enter a number:"); } return 0; } long fun(long s) { int m=1,k,s0=0; while (s != 0) { k = s % 10; if (k % 2 == 1) { s0 = s0 + k * m; m = 10 * m; } s = s / 10; } return s0; }
运行结果为