#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <time.h> #include<windows.h> void print_spaces(int n); void print_blank_lines(int n); void print_text(int line, int col, char text[]); #define N 80 int main() { int line, col, i; char text[N] = "hi,Novermber~"; srand(time(0)); for (i = 1; i <= 10; ++i) { line = rand()% 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); } return 0; } void print_spaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); } void print_blank_lines(int n) { int i; for (i = 1; i <= n; ++i) printf("\n"); } void print_text(int line, int col, char text[]) { print_blank_lines(line-1); print_spaces(col - 1); printf("%s", text); }
随机打印空格和空行,从而实现在屏幕中随机打印一串字符串
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int 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; } int fac(int n) { static long long p=1; printf("\np=%lld\n", p); p = p * n; return p; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int fac(int,int); int main() { int k = 4, m = 1, p1, p2; p1 = fac(k, m); p2 = fac(k, m); printf("%d,%d\n", p1, p2); return 0; } int fac(int a,int b) { static int m = 0, i = 2; i += m + 1; m = i + a + b; return m; }
static局部变量在编译时赋值,数据每改变一次,下次的数值即改变后的数值,不会再重新初始化
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int func(int n); int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = func(n); printf("n=%d,f=%lld\n", n, f); } return 0; } int func(int n) { int f; if (n ==0) return f = 0; else return f = 2 * func(n - 1) + 1; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int func(int n, int m); int main() { int n, m; while (scanf("%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) { int s; if (n == m || m == 0) s = 1; else if (n < m) s = 0; else if (m == 1) s = n; else s = func(n - 1, m) + func(n - 1, m - 1); return s; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int mul(int a, int b); int main() { int m, n; while (scanf("%d %d", &n, &m) != EOF) printf("n=%d,m=%d,ans=%d", n, m, mul(n, m)); return 0; } int mul(int a, int b) { int s; if (a == 0 || b == 0) s = 0; else if (a == 1) s = b; else s = mul(a - 1, b) + b; return s; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void hanoi(unsigned int n, char from, char temp, char to); void moveplate(unsigned int n, char from, char to); int count = 0; int main() { unsigned int n; while (scanf("%d", &n) != EOF) { hanoi(n, 'A', 'B', 'C'); printf("一共移动了%d次", count); count = 0; } return 0; } void hanoi(unsigned int n, char from, char temp, char to) { if (n == 1) moveplate(n, from, to); else { hanoi(n - 1, from, to, temp); moveplate(n, from, to); hanoi(n - 1, temp, from, to); } } void moveplate(unsigned int n, char from, char to) { printf("%u:%c-->%c\n", n, from, to); count++; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<math.h> int is_prime(int j); int main() { int m,n, i; while (scanf("%d", &i) != EOF) { for (m = 1;; m++) { if (is_prime(m)) { if (is_prime(n = i - m)) { printf("%d=%d+%d\n", i, m, n); break; } } } } return 0; } int is_prime(int j) { int i; for (i = 2; i < j; i++) { if (j % i == 0) break; } if (i == j) return 1; else return 0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<math.h> long fun(long s); int main() { long int 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, b = 1; long m = 0; while (s > 0) { a = s % 10; if (a % 2 != 0) { m = a * b + m; b *= 10; } s = s / 10; } return m; }
标签:return,int,char,实验,printf,include,define From: https://www.cnblogs.com/wr236/p/16862830.html