task1_1
#include<stdio.h> #include<stdlib.h> long long fac(int n); int main() { int i,n; printf("Enter n:"); scanf("%d",&n); for (i=1;i<=n;++i) printf("%di=%lld\n",i ,fac(i)); system("pause"); return 0; } long long fac(int n){ static long long p=1; p=p * n; return p; }
#include<stdio.h> #include<stdlib.h> long long fac(int n); int main() { int i,n; printf("Enter n:"); scanf("%d",&n); for (i=1;i<=n;++i) printf("%di=%lld\n",i ,fac(i)); system("pause"); return 0; } long long fac(int n){ static long long p=1; printf("p=%lld\n",p); p=p * n; return p; }
task1_2
#include<stdio.h> #include<stdlib.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); system("pause"); return 0; } int func(int a,int b){ static int m=0,i=2; i+=m+1; m=i+a+b; return (m); }
task2
迭代
#include<stdio.h> #include<stdlib.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"); } system("pause"); return 0; } void printSymbol(int n, char symbol) { while(n--) printf("%c", symbol); }
递归
#include<stdio.h> #include<stdlib.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"); } system("pause"); return 0; } void printSymbol(int n, char symbol) { if(n==1) printf("%c", symbol); else { printf("%c", symbol); printSymbol(n - 1, symbol); }
task3
#include<stdio.h> #include<stdlib.h> long long fun(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); } system("pause"); return 0; } long long fun(int n){ unsigned int z; if(n ==1) z=2; else z=2 *(fun(n-1)+1); z=z-1; return z; }
task4
#include<stdio.h> #include<math.h> #include<stdlib.h> long long isPrime(int n); int main() { int i,s=0; for(i=101;i<201;i++){ if(isPrime(i)==1) {printf("%d ",i); s++; } if(s%10==0) printf("\n"); } printf("\n"); printf("101~200之间一共有%d个非整数",s); system("pause"); return 0; } long long isPrime(int n){ int z,j; for(j=15;j>0;j--){ if(n%j==0) break;} if(j==1) z=0; else z=1; return z; }
task5
#include <stdio.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 i=1,j,a,b=0 ; do { a = s % 10; s = s / 10; if (a % 2 != 0) { b = b + i * a; i = 10 * i; } } while (s != 0); return b; }
task6
#include <stdio.h> double fun(int n); long long fac(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, s); printf("Enter n(1~10): "); } return 0; } double fun(int n) { long double r = 0; int i; for (i = 1; i <= n; i++) { r = r + 1.0 / fac(i); } return r; } long long fac(int n) { long long p=1 ; int i; for (i = 1; i <= n; i++) p = p * i; if (n % 2 == 0) p = (-1)*p; return p; }