#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void print_text(int line, int col, char text[]); // 函数声明 void print_spaces(int n); // 函数声明 void print_blank_lines(int n); // 函数声明 int main() { int line, col, i; char text[N] = "hi, November~"; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); // 暂停1000ms } return 0; } // 打印n个空格 void print_spaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); }// 打印n行空白行 void print_blank_lines(int n) { int i; for(i = 1; i <= n; ++i) printf("\n"); }// 在第line行第col列打印一段文本 void print_text(int line, int col, char text[]) { print_blank_lines(line-1); // 打印(line-1)行空行 print_spaces(col-1); // 打印(col-1)列空格 printf("%s", text); // 在第line行、col列输出text中字符串 }
#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", fac(i)); return 0; }long long fac(int n) { static long long p =1; p = p * n; return p; }
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("p = %lld\t", 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);//diaoyong 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> long long p = 1; long long f(long long n); int main() { long long n; printf("\n请输入n\n"); while (scanf_s("%lld", &n) != EOF) { p = 1; printf("n=%lld", n); printf("f=%lld\n", f(n)); } return 0; } long long f(long long n) { if (n > 0) { n--; p = p * 2; return f(n); } else { p = p - 1; return p; } }
#include <stdio.h> int func(int n, int m); int main() { int n, m; while(scanf_s("%d%d", &n, &m) != EOF) printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); return 0; } int func(int n, int m) { if (m == 0) { return 1; } if (m > n) { return 0; } if (m == n) { return 1; } if (m == 1) { return n; } else { return func(n-1, m ) + func(n-1, m - 1); } }
#include <stdio.h> int mul(int n, int m); int main() { int n, m; while(scanf_s("%d%d", &n, &m) != EOF) printf("%d * %d = %d\n", n, m, mul(n, m)); return 0; } int mul(int n, int m) { int sum=0; for (m; m > 0; m--) { sum += n; } return sum; }
#include <stdio.h> #include <math.h> char p1 = 'A', p2 = 'B', p3 = 'C', pt = 't'; void move(int n); int main() { int n=1; while (scanf_s("%d", &n) != EOF) { printf("\n%f\n",pow(2*1.0,n*1.0)-1.0); int count = 0; move(n); } return 0; } void move(int n) { if (n >= 2) { pt = p2; p2 = p3; p3 = pt; n--; move(n); pt = p2; p2 = p3; p3 = pt; printf("\nmove %c to %c\n", p1, p3); pt = p1; p1 = p2; p2 = pt; move(n); pt = p1; p1 = p2; p2 = pt; n++; } if (n < 2) { printf("\nmove %c to %c\n", p1, p3); n++; } }
#include<stdio.h> int is_prime(int n); int main() { int m, n, i; for (i = 4; i <= 20; i++) { m = 2; n = i - 2; while (is_prime(m) == 0 || is_prime(n) == 0) { m++; n--; } printf("\n%d=%d+%d\n", i, m, n); } return 0; } int is_prime(int n) { int i, tf=1; for (i = 2; i < n; i++) { if (n % i == 0) { tf = 0; break; } } return tf; }
#include <stdio.h> #include <math.h> long func(long s); // 函数声明 int main() { long s, t; printf("Enter a number: "); while (scanf_s("%ld", &s) != EOF) { t = func(s); // 函数调用 printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long func(long s) { int i; long sum=0; int count=0; while (s > 0) { i = s % 10; if (i % 2!= 0) { sum += i * pow(10, count); count++; } s = s / 10; } return sum; }