实验三
11111111111
#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); // 暂停1000ms } return 0; } // 打印n个空格 void printSpaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); } // 打印n行空白行 void printBlankLines(int n) { int i; for (i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列打印一段文本 void printText(int line, int col, char text[]) { printBlankLines(line - 1); // 打印n-1行空行 printSpaces(col - 1); // 打印n-1列空格 printf("%s", text); }
间隔1s打印hi~may~
22222222222222222
// 利用局部static变量的特性,计算阶乘 #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; printf("%lld\n", p); p = p * n; return p; }
static 变量:静态变量 函数退出时,变量始终存在,但不能被其它函数使用,当再次进入该函数时,将保存上次的结果。
2.2
// 练习:局部static变量特性 #include <stdio.h> int func(int, int); // 函数声明 int main() { int k = 4, m = 1, p1, p2; p1 = func(k, m); // 函数调用 i 3 m 8 p2 = func(k, m); // 函数调用 i 12 m 17 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; }
33333333333333333
#include <stdio.h> long long fun(int n); long long fun(int n) { long long res = 1; for (int i = 0; i < n; i++) { res *= 2; } return res - 1; } 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; }
4444444444444444
#include <stdio.h>
void hanoi(unsigned int n, char from, char temp, char to);
int moveplate(unsigned int n, char from, char to);
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);
}
}
int hanoi_move;
int moveplate(unsigned int n, char from, char to)
{
printf("第%u个盘子:%c-->%c\n", n, from, to);
hanoi_move++;
}
int main()
{
unsigned int n;
while ((scanf("%u", &n)) != EOF)
{
hanoi_move = 0;
hanoi(n, 'A', 'B', 'C');
printf("一共移动%d次\n", hanoi_move);
printf("\n");
}
return 0;
}
555555555555555
#include <stdio.h> #include <stdlib.h> #include <math.h> int is_prime(int m) { int i; for (i = 2; i < m; i++) if (m % i == 0) break; if (i == m) return 1; else return 0; } int main() { for (int n = 4; n <= 20; n += 2) { { for (int t = 2; t < n / 2; t++) { if (is_prime(t) && is_prime(n - t)) { printf("%d=%d+%d\n", n, t, n - t); break; } } } } return 0; }
6666666666666
#include <stdio.h> long fun(long s); long fun(long s) { int i; long new_num = 0; long n = 1; //顺序推入数据 记录位次 while (s != 0) { i = s % 10; if (i % 2 != 0) { new_num = n * i + new_num; n *= 10; } s /= 10; } return new_num; } 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; }
实验总结:
总结了顺序推入数据的方法。、
了解了其他Windows.h的函数