==================================第一题===============================================
#include <stdio.h> long long fac(int n); // 函数声明 int main() { int i, n; printf("Enter n: "); scanf_s("%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("%d\n", p); p = p * n; return p; }
#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); }
==============================================================================第二题=============================
#include <stdio.h> void printSymbol(int n, char symbol); // 函数声明 int main() { int n; char symbol; while (scanf_s("%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); }
递归版
#include <stdio.h> void printSymbol(int n, char symbol); // 函数声明 int main() { int n; char symbol; while (scanf_s("%d %c", &n, &symbol) != EOF) { printSymbol(n, symbol); // 函数调用 printf("\n"); } return 0; } void printSymbol(int n, char symbol) { if (n == 0) return ; else printf("%c",symbol); return printSymbol(n - 1, symbol); }
迭代更好,递归晦涩难懂,日后难以调试
=============================================================第三题===============================================
#include <stdio.h> long long fun(int n); // 函数声明 int main() { int n; long long f; while (scanf_s("%d", &n) != EOF) { f = fun(n);// 函数调用 printf("n = %d, f = %lld\n", n, -f); } return 0; } long long fun(int n) { if (n == 0) return 0; else { return 2 * fun(n - 1) - 1; } }
=============================================================第四题=================================================================
#include<stdio.h> int isPrimeNumber(int n); int main() { int a=101; int line = 1; int sum = 0; while (a >= 101 && a <= 200) { if (isPrimeNumber(a)==0) { printf("%6d", a); if (line % 10 == 0) { printf("\n"); } line++; sum++; } a++; } printf("\n"); printf("一共有%d个非素数", sum); return 0; } int isPrimeNumber(int n) { int i; for (i = 2; i <= n / 2; i++) { if (n % i == 0) return 0; } return 1; }
=============================================================第五题====================================================================
#include <stdio.h> #include<math.h> long fun(long s); int main() { long s, t; printf("Enter a number: "); while (scanf_s("%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 a = 0, b = 1, num = 0; while (s > 0) { a = s % 10; if (a % 2 != 0) { num += a * b; b *= 10; } s /= 10; } return num; }
==================================================第六题=================================================
#include <stdio.h> double fun(int n); // 函数声明 int main() { int n; double s; printf("Enter n(1~10): "); while (scanf_s("%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; int i; int j = 1; int k = 1; int a[100]; for (i = 0; i < n; i++) { k = k * j; j++; a[i] = k; k = -k; } for (i = 0; i < n; i++) { sum = sum + 1.0 / a[i]; } return sum; }