task1
#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,April~";
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);
}
功能:在屏幕中随机打印“hi,April~”
task2
#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;
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);
printf("%d,%d\n", p1, p2);
return 0;
}
int func(int a, int b)
{
static int m = 0, i = 2;
i = i + m + 1;
m = i + a + b;
return m;
}
是一致的
static是固定一个局部变量,使其在每次运算中不会被清除
task3
#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)
{
long long ans;
if (n == 1)
ans = 1;
else
ans = 2 * func(n-1) + 1;
return ans;
}
task4
#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)
{
int ans;
if (n == 1 || m == 1||m==0)
{
if (m == n||m==0)
ans = 1;
else
{
if (m == 1)
ans = n;
else
ans = 0;
}
}
else
{
if (n > m)
ans = func(n - 1, m) + func(n - 1, m - 1);
else
{
if (m = n)
ans = 1;
else
ans = 0;
}
}
return ans;
}
task5
#include<stdio.h>
double mypow(int x, int y);
int main()
{
int x, y;
double ans;
while (scanf_s("%d%d", &x, &y) != EOF)
{
ans = mypow(x, y);
printf("%d的%d次方:%g\n\n", x, y, ans);
}
return 0;
}
double mypow(int x, int y)
{
int i;
double ans;
ans = 1;
if (y >= 0)
{
for (i = 1; i <= y; i++)
{
ans = ans * x;
}
}
else
{
y = -y;
for (i = 1; i <= y; i++)
{
ans = ans * x;
}
ans = 1 / ans;
}
return ans;
}
#include <stdio.h>
double mypow(int x, int y);
int main() {
int x, y;
double ans;
while (scanf_s("%d%d", &x, &y) != EOF) {
ans = mypow(x, y);
printf("%d的%d次方: %g\n\n", x, y, ans);
}
return 0;
}
double mypow(int x, int y)
{
double ans;
if (y == 0)
ans = 1;
if (y > 0)
ans = x * mypow(x, y - 1);
if (y < 0)
ans = 1 / mypow(x, -y);
return ans;
}
task6
#include<stdio.h>
#include<stdlib.h>
void hanoi(int n, char A, char B, char C);
void DAYING(int n, char A, char C);
int jisuan(int i);
int main()
{
int n, m;
while(scanf_s("%d",&n) !=EOF)
{
m = jisuan(n);
hanoi(n, 'A', 'B', 'C');
printf("一共移动了%d\n",m);
}
return 0;
}
void hanoi(int n, char A, char B, char C)
{
if (n == 1)
DAYING(n, A, C);
else
{
hanoi(n - 1, A, C, B);
DAYING(n, A, C);
hanoi(n - 1, B, A, C);
}
}
void DAYING(int n, char A, char C)
{
printf("%d:%c-->%c\n", n, A, C);
}
int jisuan(int i)
{
int ans;
if (i > 1)
ans = jisuan(i - 1) + 1 + jisuan(i - 1);
else
ans = 1;
return ans;
}
task7
#include<stdio.h>
#include<stdlib.h>
int is_prime(int a);
void DAYING(int A);
int main()
{
int i, shifou;
shifou = 0;
for (i = 2;i<=20; i = i + 2)
{
DAYING(i);
}
return 0;
}
int is_prime(int a)
{
int i;
i = 2;
if (a >= 2)
{
for (; a > i; i=i+1)
{
if (a % i == 0)
return 0;
}
return 1;
}
else
return 0;
}
void DAYING(int A)
{
int m, n, flag;
if (A >= 2)
{
m = A - 1;
n = A - m;
flag = is_prime(m) + is_prime(n);
for (; (flag != 2) && (m != 0);)
{
m = m - 1;
n = n + 1;
flag = is_prime(m) + is_prime(n);
}
if (flag == 2)
{
printf("%d=%d+%d\n", A, m, n);
}
}
}
task8
#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, ans, n;
ans = 0;
for (n=0;s!=0;)
{
i = s % 10;
s = s / 10;
if (i % 2 == 0)
;
else
{
ans = i * pow(10, n) + ans;
n = n + 1;
}
}
return ans;
}
标签:return,int,ans,long,实验,func,printf From: https://www.cnblogs.com/UNSCcruiser117/p/17290450.html