实验3
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void printText(int line, int col, char text[]); void printSpaces(int n); void printBlankLines(int n); int main() { int line, col, i; char text[N] = "hi, May~"; srand(time(0)); for (i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; printText(line, col, text); Sleep(1000); } return 0; } void printSpaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); } void printBlankLines(int n) { int i; for (i = 1; i <= n; ++i) printf("\n"); } void printText(int line, int col, char text[]) { printBlankLines(line - 1); printSpaces(col - 1); printf("%s", text); }
#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; }
#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;}
#include<stdio.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); } return 0; } long long fun(int n) { long long result; if(n==0) result=0; else result=2*fun(n-1)+1; return result; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int i; void hanoi(unsigned n, char from, char to, char temp); void move(unsigned n, char from, char to); int main() { unsigned n; while (scanf("%u", &n) != EOF) { i = 0; hanoi(n, 'A', 'C', 'B'); printf("一共移动了%d次", i); } system("pause"); } void hanoi(unsigned n, char from, char to, char temp) { if (n == 1) move(n, from, to); else { hanoi(n - 1, from, temp, to); move(n, from, to); hanoi(n - 1, temp, to, from); } } void move(unsigned n, char from, char to) { i++; printf("第%u个盘子: %c-->%c\n", n, from, to); }
#include<stdio.h> #include<math.h> #include<stdlib.h> int is_prime(int n); int main() { int m,j; for (m = 4; m <= 20; ++m) { if (m % 2 != 0) continue; else { for (j = 2; j < m - 1; ++j) { if (is_prime(j) && is_prime(m - j)) { printf("%d=%d+%d\n", m, j, m - j); break; } } } } return 0; } int is_prime(int n) { int i; if (n == 0 || n == 1) return 0; else { for (i = 2; i < n - 1; ++i) { if (n % i != 0) return 1; break; } while (i == n - 1) return 0; } }
#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) { long i,p=0,n=1; while(s!=0) { i=s%10; if(i%2!=0) { p=n*i+p; n*=10; } s=s/10; } return p; }