实验一
task1-1(填代码前)
#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; p = p * n; return p; }
task1-1(增加代码后)
#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; }
task 1-2
#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变量为静态变量,即再次进入函数时,将保存上次运行的结果。
实验二
task 2-1
#include <stdio.h> void printSymbol(int n, char symbol); int main() { int n; char symbol; while( scanf("%d %c", &n, &symbol) != EOF ) { printSymbol(n, symbol); printf("\n"); } return 0; } void printSymbol(int n, char symbol){ int i; for(i=0;i<n;i++){ printf("%c",symbol); } }
task 2-2
#include <stdio.h> void printSymbol(int n, char symbol); int main() { int n; char symbol; while( scanf("%d %c", &n, &symbol) != EOF ) { printSymbol(n, symbol); printf("\n"); } return 0; } void printSymbol(int n, char symbol){ if(n==1){ printf("%c",symbol); }else{ printSymbol(n-1,symbol); printf("%c",symbol); } }
在这个问题中,迭代更好。不需像递归那样不断调用函数,使得代码运行过程中,效率更高。而且算法简单直观。
实验三
#include <stdio.h> long long fun(int n); long long mypow(int n); int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = fun(n); printf("n = %d, f = %lld\n", n, f); } return 0; } long long fun(int n){ int result; result=mypow(n)-1; return result; } long long mypow(int n){ int result=1; if(n==1){ result*=2; }else{ result=2*mypow(n-1); } return result; }
实验四
#include <stdio.h> int isPrime(int n); int main(){ int i,k=0,n=0; for(i=101;i<=200;i++){ if(isPrime(i)==0){ printf("%5d",i); k++; n++; if(k%10==0){ printf("\n"); k=0; } } } printf("\n"); printf("一共有%d个非素数\n",n); return 0; } int isPrime(int n){ int j; for(j=2;j<n;j++){ if(n%j==0){ break; } } if(j<n){ return 0; }else{ return 1; } }
实验五
#include <stdio.h> #include<math.h> long fun(long s); int main() { long s, t; printf("Enter a number: "); while (scanf("%ld", &s) != EOF) { t = fun(s); printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long fun(long s){ int a,i=0; long sum=0; while(s>0){ a=s%10; if(a%2!=0){ sum+=a*pow(10,i); i++; } s/=10; } return sum; }
实验六
#include <stdio.h> #include<math.h> double fun(int n); int factorial(int n); int main() { int n; double s; printf("Enter n(1~10): "); while (scanf("%d", &n) != EOF) { s = fun(n); printf("n = %d, s= %f\n\n", n, s); printf("Enter n(1~10): "); } return 0; } double fun(int n){ double sum=0; if(n==1){ sum=1; }else{ sum=pow(-1,n-1)/factorial(n)+fun(n-1); } return sum; } int factorial(int n){ int fac=0; if(n==1){ fac=1; }else{ fac=n*factorial(n-1); } return fac; }
实验收获:
1.函数的定义、调用等有更加深入的理解
2.区别递归和迭代
3.对于写题有更多的经验(注意细节、格式、题意等等)