Task1
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, April~"; 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 24 return 0; 25 } 26 27 void print_spaces(int n) { 28 int i; 29 30 for(i = 1; i <= n; ++i) 31 printf(" "); 32 } 33 34 void print_blank_lines(int n) { 35 int i; 36 37 for(i = 1; i <= n; ++i) 38 printf("\n"); 39 } 40 41 void print_text(int line, int col, char text[]) { 42 print_blank_lines(line-1); 43 print_spaces(col-1); 44 printf("%s", text); 45 }
该程序运行后在屏幕上每隔1s随机输出一个“hi,April~”(由函数控制打印空行、空列),共输出10个。
Task2
2-1
1 #include <stdio.h> 2 #include<stdlib.h> 3 long long fac(int n); 4 5 int main() { 6 int i, n; 7 8 printf("Enter n: "); 9 scanf("%d", &n); 10 11 for (i = 1; i <= n; ++i) 12 printf("%d! = %lld\n", i, fac(i)); 13 system("pause"); 14 return 0; 15 } 16 17 long long fac(int n) { 18 static long long p = 1; 19 printf("p=%11d\n",p); 20 p = p * n; 21 22 return p; 23 }
2-2
1 #include <stdio.h> 2 #include<stdlib.h> 3 int func(int, int); 4 5 int main() { 6 int k = 4, m = 1, p1, p2; 7 8 p1 = func(k, m); //func(4,1) i=2+0+1=3 p1=3+4+1=8 9 p2 = func(k, m); //func(4,8) i=3+8+1=12 p2=12+4+1=17 10 printf("%d, %d\n", p1, p2); 11 system("pause"); 12 return 0; 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 }
局部变量的值保持不变,程序计算过程如注释所示。
Task3
1 #include <stdio.h> 2 #include<stdlib.h> 3 long long func(int n); 4 5 int main() { 6 int n; 7 long long f; 8 9 while (scanf("%d", &n) != EOF) { 10 f = func(n); 11 printf("n = %d, f = %lld\n", n, f); 12 } 13 system("pause"); 14 return 0; 15 } 16 17 long long func(int n){ 18 if (n==0) 19 return 0; 20 else 21 return 2*(func(n-1)+1)-1; 22 }
Task4
4-1(迭代)
1 #include <stdio.h> 2 #include<stdlib.h> 3 //迭代 4 int func(int n, int m); 5 6 int main() { 7 int n, m; 8 9 while(scanf("%d%d", &n, &m) != EOF) 10 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 11 system("pause"); 12 return 0; 13 } 14 15 int func(int n, int m){ 16 int i=0,ans=1; 17 if (m>n) 18 return 0; 19 else 20 for(;i<m;i++) 21 { 22 ans*=(n-i); 23 ans/=(i+1); 24 } 25 return ans; 26 }
4-2(递归)
1 #include <stdio.h> 2 int func(int n, int m); 3 4 int main() { 5 int n, m; 6 7 while (scanf_s("%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 int func(int n, int m) { 13 if (m == 0 || m == n) 14 return 1; 15 else if (m > n) 16 return 0; 17 else 18 return func(n - 1, m - 1) + func(n-1, m ); 19 }
Task5
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 void hanoi(unsigned int n, char from, char temp, char to, int* m); 5 void moveplate(unsigned int n, char from, char to, int* m); 6 int main() 7 { 8 unsigned int n; 9 while (scanf_s("%u", &n) != EOF) 10 { 11 int m = 0; 12 hanoi(n, 'A', 'B', 'C', &m); 13 printf("\n一共移动了%d次.\n\n", m); 14 } 15 system("pause"); 16 return 0; 17 } 18 19 void hanoi(unsigned int n, char from, char temp, char to, int* m) 20 { 21 if (n == 1) 22 moveplate(n, from, to, m); 23 else 24 { 25 hanoi(n - 1, from, to, temp, m); 26 moveplate(n, from, to, m); 27 hanoi(n - 1, temp, from, to, m); 28 } 29 } 30 void moveplate(unsigned int n, char from, char to, int* m) 31 { 32 printf("%u:%c --> %c\n", n, from, to); 33 ++(*m); 34 }
Task6
1 #include <stdio.h> 2 #include <math.h> 3 #include<stdlib.h> 4 long func(long s); 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 system("pause"); 16 return 0; 17 } 18 19 long func(long s){ 20 int ans=0,x=1; 21 if (s==0) 22 return 0; 23 while(s>0) 24 { 25 int m=s%10; 26 if(m%2!=0){ 27 ans+=x*m; 28 x*=10; 29 } 30 s/=10; 31 } 32 return ans; 33 }
实验总结
需要理解迭代和递归的基本思路,认清问题的本质,学会用不同方法解决问题。
标签:10,return,函数,int,编程,long,C语言,func,include From: https://www.cnblogs.com/nuistjcy/p/18151807