实验3
一、实验任务1
#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, November~"; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 1; i <= 10; ++i) { line = rand() % 25; col = ?rand() % 80; print_text(line, col, text); Sleep(1000); ?// 暂停1000ms } return 0; } // 打印n个空格 void print_spaces(int n) { int i; for(i = 1; i <= n; ++i) printf(" "); } // 打印n行空白行 void print_blank_lines(int n) { int i; for(i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列打印一段文本 void print_text(int line, int col, char text[]) { print_blank_lines(line-1); // 打印(line-1)行空行 print_spaces(col-1); // 打印(col-1)列空格 printf("%s", text); ? ? ? ? // 在第line行、col列输出text中字符串 }
该程序的目的是在命令行窗口中以随机位置输出字符串"hi, November~",每次输出后暂停1秒。
二、实验任务2
#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; }
#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
关键字的作用包括限定变量或函数的作用范围、保留变量的值和扩展变量的生命周期。
三、实验任务3
#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==0) return 0; else return (func(n-1)+1)*2-1; }
四、实验任务4
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) return 0; int num= 1; int deno= 1; for (int i = 0; i < m; i++) num *= n - i; for (int i = 1; i <= m; i++) deno*= i; return num/ deno; }
2、递归
#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) return 0; if (m == 0 || n == m) return 1; return func(n - 1, m - 1) + func(n - 1, m); }
五、实验任务5
#include <stdio.h> #include <stdlib.h> int hanoi(int n, char x, char y, char z, int *counter) { int move(char, int, char); if (n == 1) { move(x, 1, z); (*counter)++; } else { hanoi(n - 1, x, z, y, counter); move(x, n, z); (*counter)++; hanoi(n - 1, y, x, z, counter); } return 0; } int move(char get, int n, char put) { static int k = 1; printf("%d: %c --> %c\n", n, get, put); k++; return 0; } int main() { system("color CF"); int hanoi(int, char, char, char, int *); int n, counter; while (scanf("%d", &n) != EOF) { counter = 0; hanoi(n, 'A', 'B', 'C', &counter); printf("\n一共移动了%d次\n\n", counter); } return 0; }
六、实验任务6
#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 result = 0; long multiplier = 1; while (s > 0) { long digit = s % 10; if (digit % 2 != 0) { result = result + digit * multiplier; multiplier *= 10; } s /= 10; } return result; }
六、实验任务6
#include <stdio.h> #include <stdlib.h> int fuck(long n ,long m); int main(void) { for (int i=0;;i++){ int a=i*i; int b=i*i*i; if(fuck(a,b)==1){ printf("%d",i); break; } } return 0; } int fuck(long n,long m){ long temp = n; int Di= 0; while (temp > 0) { temp /= 10; Di++; } int a[Di]; for (int i = Di - 1; i >= 0; i--) { a[i] = n% 10; n/= 10; } long temp2 = m; int Di2= 0; while (temp2 > 0) { temp2 /= 10; Di2++; } int b[Di2]; for (int i = Di2 - 1; i >= 0; i--) { b[i] = m% 10; m/= 10; } int v= Di+Di2; int c[v]; int i, j, k; i = j = k = 0; while (i < Di){ c[k++] = a[i++]; } while (j<Di2){ c[k++] = b[j++]; } int cao[10]={0}; for(int i=0;i<v;i++){ cao[c[i]]++; } for(int i=0;i<=9;i++) { if(cao[i]!=1) return 0; } return 1; }