task1
源代码
#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_line(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_line(int n) { int i; for (i = 1; i <= n; ++i) { printf("\n"); } } void print_text(int line, int col, char text[]) { print_spaces(col-1); print_blank_line(line-1); printf("%s", text); }
运行结果
task2_1
源代码
#include <stdio.h> #include <stdlib.h> long long fac(int n); int main() { int i, n; printf("ENTER n:"); scanf_s("%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; printf("p = %lld\n", p); p = p * n; return p; }
运行结果
task2_2
源代码
#include <stdio.h> #include <stdlib.h> int func(int a, int b); 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;//3 //12 m = i + a + b;//8//17 return m; }
运行结果
task3
源代码
#include <stdio.h> long long func(int n); // 函数声明 int main() { int n; long long f; while (scanf_s("%d", &n) != EOF) { f = func(n); // 函数调用 printf("n = %d, f = %lld\n", n, f); } return 0; } long long func(int n) { static long long ans; if (n == 1) return 1; else { ans = ((func(n - 1) + 1) * 2) - 1; return ans; } }
实验结果
task4
源代码
#include <stdio.h> int func(int n, int m); int main() { int n, m; while (scanf_s("%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) { int ans; if (n < m) return 0; else if (n == m || m == 0) return 1; else { return func(n - 1, m - 1) + func(n-1, m ); } }
运行结果
task5
源代码
#include <stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while (scanf_s("%d%d", &x, &y) != EOF) { if (y > 0) ans = mypow(x, y); else { y = -y; ans = 1.0 / mypow(x, y); } printf("%d的%d次方: %g\n\n", x, y, ans); } return 0; } double mypow(int x, int y) { if (y == 1) return x; else return mypow(x, y - 1) * x; }
实验结果
task6
源代码
#include<stdio.h> int hannoi_step(int n); void hannoi_move(int n, char A, char B, char C); int hannoi_step(int n) { if (n <= 1) return 1; else return 2 * hannoi_step(n - 1)+1; } void hannoi_move( int n,char from, char temp, char to) { if (n == 1) printf("%d:%c-->%c\n",n, from, to); else { hannoi_move(n - 1, from, to,temp); printf("%d:%c-->%c\n",n, from, to); hannoi_move(n - 1, temp, from, to); } } int main() { int n=0, ans; while (scanf_s("%d", &n) != EOF) { ans = hannoi_step(n); printf("一共移动了%d次\n", ans); hannoi_move(n, 'A', 'B', 'C'); } return 0; }
运行结果
task7
源代码
#include<stdio.h> int is_prime(int n); int is_prime(int n) { if (n <= 1) return 0; else { for (int i = 2; i < n; ++i) { if (n % i == 0) return 0; } return 1; } } int main() { int ans, num,i; for (int m = 1; m <101; ++m) { if (m % 2 == 0) { num = m; for (i = 2; i <=num / 2; ++i) { if (is_prime(i) && is_prime(num - i)) { printf("%d=%d+%d\n", num, i, num - i); break; } } } else continue; } return 0; }
运行结果
task8
源代码
#include <stdio.h> #include <math.h> long func(long s); int main() { long s, t; printf("Enter a number: "); while (scanf_s("%ld", &s) != EOF) { t = func(s); printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long func(long s) { int ans, m, t; m =t= 0; while(s>0) { ans = s % 10; if (ans % 2) m = m * 10 + ans; else m = m; s = s / 10; } while (m > 0) { t = t * 10 + m % 10; m = m / 10; } return t; }
运行结果
标签:return,int,long,实验,func,ans,include From: https://www.cnblogs.com/cjqyyds666666/p/17273750.html