#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中字符串
};
![]()
// 利用局部static变量的特性,计算阶乘
#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("p = %lld\n", p);
p = p * n;
return p;
}
![]()
// 练习:局部static变量特性
#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>
long long func(int n); // 函数声明
int main() {
int n;
long long f;
while (scanf_s("%d", &n) != EOF) {
f = func(n); // 函数调用
printf("n = %d, f = %lld\n", n, f);
}
return 0;
}
// 函数定义
// 待补足。。。
long long func(int n)
{
if (n == 0)
return 0;
else
return (2*func(n-1)+1);
}
![]()
#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 (n < m)
return 0;
if (m == 0||n==1)
return 1;
else
return func(n - 1, m - 1) + func(n - 1, m);
}
![]()
#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)
{
if (n == 1)
{
return m;
}
else
return mul(n - 1, m) + m;
}
![]()
#include<stdio.h>
#include<stdlib.h>
int hanoi(unsigned int n, char from, char temp, char to);
int moveplate(unsigned int n, char from, char to);
int i=0;
int main()
{
unsigned int n;
while (1)
{scanf_s("%u", &n);
hanoi(n, 'A', 'B', 'C');
printf("一共移动了%d次\n", i);
i = 0;
}
system("pause");
return 0;
}
int 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 moveplate(unsigned int n, char from, char to)
{
printf("%u:%c-->%c\n", n, from, to);
i++;
//printf("次数:%d\n", i);
}
![]()
#include<stdio.h>
#include<math.h>
int is_prime(int n);
int main()
{
int x, y, n;
scanf_s("%d", &n);
for (int i=2; i <= n; i++)
{
x = n - i;
y = i;
if (is_prime(x) && is_prime(y))
break;
}
printf("%d=%d+%d", n, y, x);
}
int is_prime(int n)
{
for (int i = 2; i <= n; i++)
{
if (i == n)
{return 1;
break;
}
if (n % i == 0)
{
return 0;
break;
}
}
}
![]()
#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)
{
int m = 0,j=0,n=1,i=1,t=1;
int y=0,x;
int v[100];
//for (; y%10!= 0;i++)
while (t!=0)
{
t = s / (int)pow(10, i);
/*for (j=1;j<=i-j+1;j++)
{
n *= 10;
}*/
y = (s-y) % (int)pow(10,i);
x= y / (int)pow(10, i - 1);
if (x % 2 != 0||x==1)
{
v[j] = x;
j++;
}
i++;
}
for (int j = 0;j<=100; j++)
if(v[j]<10&&v[j]>=1)
m += v[j]*(int)pow(10,j);
return m;
}
![]()