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-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); }
task2-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) { while(n--) 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>0) { printf("%c",symbol); n--; printSymbol(n,symbol); } }
task 3
#include <stdio.h> long long fun(int n); int m; int main() { int n; long long f; while (scanf("%d", &n) != EOF) { m=1; f = fun(n); printf("n = %d, f = %lld\n", n, f); } return 0; } long long fun(int n) { if(n>0) { m*=2; n--; fun(n); } else return m-1; }
task 4
#include<stdio.h> #include<math.h> int isPrime(int n); int main() { int t=0; for(int i=101;i<=200;i++) { if(!isPrime(i)) { t++; printf("%d ",i); } } printf("\n101~200之间一共有%d个非素数",t); return 0; } int isPrime(int n) { int s=0; for(int j=2;j<=sqrt(n);j++) { if(n%j==0) s++; } if(s==0) return 1; else return 0; }
task 5
#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) { long m=0,n,p=0,j=0,s0;//m存储数值的位数 n存奇数数字 p存新数字 s0=s;//备份s的值 do { m++; s=s/10; }while(s!=0); for(long i=0;i<m;i++) { if(s0/(long)pow(10,i)%2!=0) //判断是奇数 { n=s0/(long)pow(10,i)%10; p+=n*pow(10,j); j++; } } return p; }
task 6
#include <stdio.h> double fun(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 mom=1,son=1,sum=0; for(double i=1;i<=n;i++) { mom=1;//考虑到mom会变化,所以每次循环进行一次初始化 for(double j=1;j<=i;j++) mom*=j; sum+=son/mom; son=-son; } return sum; }