实验任务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,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); }
截图:
结论:
不断循环:随机生成0~25的数字作为line,随机生成0~80的数字作为col,打印line-1行空行,打印col-1列空格后,在第line行,第col列输出hi,April~的字符串。即随机分布hi,April~
实验任务2
代码:
#include<stdio.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!=%lld\n", i, fac(i)); return 0; } long long fac(int n) { static long long p =1; p = p * n; return p; }
#include<stdio.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!=%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; }
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变量的特性是:定义局部变量并将其初始化,所以在程序运行时静态局部变量就存在且执行完毕,程序调用函数并执行的时候static的这条定义并不起作用,所以只在第一次调用时变量取初始化的值,之后的调用函数并执行的过程中使用的变量的值都是由前一次执行函数所得的值继承而来。也就是上面程序第一次执行的结果是8而第二次执行的结果是17。
实验任务3
代码:
#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) { long long f=0; long long m=1; for (int i = 1; i <= n; i++) { m *= 2; f =m-1; } return f; }
截图:
实验任务4
代码:
#include<stdio.h> int func(int x, int y); 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 x,int y) { double ans; if (y == 0 || y == x) ans = 1; else if (y == 1) ans = x; else if (y > x) ans = 0; else ans = (func(x - 1, y) + func(x - 1, y - 1)); return ans; }
截图:
实验任务5
代码:
#include<stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while (scanf_s("%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 ans=1; if (y >= 0) { for (int i = 1; i <= y; i++) ans *= x; } else { for (int j = 1; j <= -y; j++) ans = ans / x; } return ans; }
#include<stdio.h> double mypow(int x, int y); int main() { int x, y; double ans; while (scanf_s("%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 ans = 1,t=1; if (y == 0) ans = 1; else { if (y > 0) { for (int i = 1; i <= y - 1; i++) { t *= x; } ans = t * x; } else { for (int j = 1; j <= -y; j++) { t *= x; } ans = 1 / t; } } return ans; }
截图:
实验任务6
代码:
#include<stdio.h> #include<stdlib.h> void hanoi(unsigned int n, char from, char temp, char to); void moveplate(unsigned int n, char from, char to); int tot; int main() { unsigned int n; while (scanf_s("%u", &n) != EOF) { tot = 0; hanoi(n, 'A', 'B', 'C'); printf("一共移动了%d次\n", tot); } system("pause"); 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) { tot++; printf("%u:%c-->%c\n", n, from, to); }
截图:
实验任务7
代码:
#include<stdio.h> int is_prime(int n); int main() { int i=4; while( i<=20){ int x, y; if (i == 4) printf("4=2+2\n"); else{ for (x = 3; x <= i; x += 2) { y = i - x; if (is_prime(x) && is_prime(y)) { printf("%d=%d+%d\n", i,x,y); break; } } } i += 2; } return 0; } int is_prime(int n) { int i; if (n <= 2) return 0; else { for (i = 2; i <= n - 1; i++) { if (n % i == 0) break; } if (i >= n) return 1; else return 0; } }
截图:
实验任务8
代码:
#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) { long t = 0; int x, sum = 0; while (s) { x = s % 10; if (x % 2 == 1) { sum += 1; t += x * pow(10, sum-1); } s /= 10; } return t; }
截图:
标签:return,int,long,实验,func,ans,include From: https://www.cnblogs.com/gueal/p/17273756.html