实验任务1
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<windows.h> 5 #define N 80 6 7 void print_text(int line, int col, char text[]); 8 void print_spaces(int n); 9 void print_blank_lines(int n); 10 11 int main(){ 12 int line,col,i; 13 char text[N] = "hi,November~"; 14 15 srand(time(0)); 16 17 for(i = 1;i <= 10; ++i){ 18 line = rand() % 25; 19 col = rand() % 80; 20 print_text(line,col,text); 21 Sleep(1000); 22 } 23 return 0; 24 } 25 26 27 void print_spaces(int n){ 28 int i; 29 30 for(i = 1;i <= n; ++i) 31 printf(" "); 32 } 33 34 35 void print_blank_lines(int n){ 36 int i; 37 38 for(i = 1;i <= n; ++i) 39 printf("\n"); 40 } 41 42 43 void print_text(int line,int col,char text[]){ 44 print_blank_lines(line-1); 45 print_spaces(col-1); 46 printf("%s", text); 47 48 }View Code
截图
功能:随机打印hi,November
实验任务2
task2.1源代码
1 #include<stdio.h> 2 long long fac(int n); 3 4 int main(){ 5 int i,n; 6 7 printf("Eter n: "); 8 scanf("%d",&n); 9 for(i = 1;i <= n; ++i) 10 printf("%d! = lld\n" ,i,fac(i)); 11 12 return 0; 13 } 14 15 16 long long fac(int n){ 17 static long long p = 1; 18 19 p = p * n; 20 21 return p; 22 }View Code
截屏
增加一行后源代码
1 #include<stdio.h> 2 long long fac(int n); 3 4 int main(){ 5 int i,n; 6 7 printf("Eter n: "); 8 scanf("%d",&n); 9 for(i = 1;i <= n; ++i) 10 printf("%d! = lld\n" ,i,fac(i)); 11 12 return 0; 13 } 14 15 16 long long fac(int n){ 17 static long long p = 1; 18 printf("p=%lld\n",p); 19 p = p * n; 20 21 return p; 22 }View Code
截屏
task2.2
源代码
1 #include<stdio.h> 2 int func(int,int); 3 4 int main(){ 5 int k = 4, m = 1, p1, p2; 6 7 p1=func(k, m); 8 p2=func(k, m); 9 printf("%d, %d\n", p1, p2); 10 11 return 0; 12 } 13 14 15 int func(int a, int b){ 16 static int m = 0, i = 2; 17 18 i += m + 1; 19 m = i + a + b; 20 21 return m; 22 }View Code
截屏
实验任务3
源代码
1 #include<stdio.h> 2 long long func(int n); 3 4 int main() 5 { 6 int n; 7 long long f; 8 9 while (scanf("%d",&n)!=EOF){ 10 f = func(n) - 1; 11 printf("n = %d, f = %lld\n", n, f); 12 } 13 14 return 0; 15 } 16 17 18 long long func(int n){ 19 if(n==0){ 20 return 1; 21 } 22 else if(n==1){ 23 return 2; 24 } 25 else{ 26 return 2 * func(n - 1); 27 } 28 29 }View Code
截屏
实验任务4
源代码
迭代
1 #include<stdio.h> 2 int main(){ 3 int n, m; 4 int sum1 = 1; 5 int sum2 = 1; 6 int sum3 = 1; 7 int ans; 8 while(scanf("%d%d", &n, &m)!=EOF){ 9 if(m==0){ 10 ans = 1; 11 } 12 else{ 13 int i = 1; 14 for (i = 1; i <= n; i++){ 15 sum1 *= i; 16 } 17 int j = 1; 18 for (j = 1; j <= m; j++){ 19 sum2 *= j; 20 } 21 int x = 1; 22 for (x = 1; x<= n - m; x++){ 23 sum3 *= x; 24 } 25 ans = sum1 / sum2 / sum3; 26 } 27 printf("n = %d, m = %d, ans = %d\n", n, m, ans); 28 sum1 = 1; 29 sum2 = 1; 30 sum3 = 1; 31 } 32 33 return 0; 34 }View Code
递归
1 #include<stdio.h> 2 int func(int n, int m); 3 4 int main(){ 5 int n, m; 6 7 while(scanf("%d%d", &n, &m) != EOF) 8 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 9 10 return 0; 11 } 12 13 int func(int n, int m){ 14 if(n<m){ 15 return 0; 16 } 17 else{ 18 int a; 19 if(m == 0){ 20 a = 1; 21 } 22 else if(m == 1){ 23 a = n; 24 } 25 else { 26 return func(n -1, m) + func(n - 1, m - 1); 27 } 28 return a; 29 } 30 }View Code
截屏
实验任务5
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 void hanoi(unsigned int n, char from, char temp, char to); 4 void moveplate(unsigned int n, char from, char to); 5 6 int main() 7 { 8 unsigned int n; 9 int count = 0; 10 while(scanf ("%u", &n)!=EOF){ 11 hanoi(n, 'A', 'B', 'C'); 12 printf("\n"); 13 int i=1; 14 for(i = 1; i <= n; i++){ 15 count = 2 * count + 1; 16 } 17 printf("一共移动了%d次\n",count); 18 printf("\n"); 19 count = 0; 20 } 21 system("pause"); 22 23 return 0; 24 } 25 26 void hanoi(unsigned int n, char from, char temp, char to){ 27 if (n == 1){ 28 moveplate (n, from, to); 29 } 30 else{ 31 hanoi(n - 1, from, to, temp); 32 moveplate(n, from, to); 33 hanoi(n - 1, temp, from, to); 34 } 35 } 36 37 void moveplate(unsigned int n, char from, char to){ 38 printf("%u : %c-- > %c\n", n, from, to); 39 40 }View Code
截屏
实验任务6
源代码
1 #include<stdio.h> 2 #include<math.h> 3 long func(long s); 4 5 int main() 6 { 7 long s, t; 8 9 printf("Enter a number: "); 10 while(scanf("%ld", &s)!=EOF){ 11 t = func(s); 12 printf("new number is: %ld\n\n", t); 13 printf("Enter a number: "); 14 } 15 16 return 0; 17 } 18 19 long func(long s){ 20 int n = 0; 21 int m = 0; 22 int i = 0; 23 int j; 24 while(s > 0){ 25 n = s % 10; 26 if(n % 2 != 0){ 27 m = 10 * m + n; 28 } 29 s /= 10; 30 } 31 while(m > 0){ 32 j = m % 10; 33 i = i * 10 + j; 34 m /= 10; 35 } 36 return i; 37 } 38 39 40View Code
截屏
标签:10,return,函数,int,编程,long,试验,func,include From: https://www.cnblogs.com/Eternity91/p/17798407.html