test1
代码
#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);
}
system("pause");
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);
}
截图
随机在某行某列打印出所给字符
test2
代码
#include <stdio.h>
#include<stdlib.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;
printf("p = %lld\n", p);
system("pause");
return p;
}
截图
代码
#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;
}
截图
test3
代码
#include <stdio.h>
#include<stdlib.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);
}
system("pause");
return 0;
}
long long func(int n)
{
long long int sum;
if (n == 1)
sum = 1;
else
sum = (func(n - 1) + 1) * 2 - 1;
return sum;
}
截图
test4
代码
#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 x;
x = 0;
if (n >= m)
{
if (m > 1)
x = func(n - 1, m - 1) + func(n - 1, m);
else
{
if (m == 0)
x = 1;
else
x = n;
}
}
else
x = 0;
return x;
}
截图
test5
代码
#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;
}
截图
test6
代码
#include<stdio.h>
#include<stdlib.h>
int Hanoita(int n, char A, char B, char C);
int cishu(int n);
int main()
{
double sum;
int n;
while (scanf_s("%d", &n) != EOF)
{
Hanoita(n, 'A', 'B', 'C');
sum = cishu(n);
printf("一共移动了%.0f次。\n", sum);
}
return 0;
}
int Hanoita(int n, char A, char B, char C)
{
if (1 == n)
printf("%d:%c->%c\n",n, A, C);
else
{
Hanoita(n - 1, A, C, B);
printf("%d:%c->%c\n", n, A, C);
Hanoita(n - 1, B, A, C);
}
return 0;
}
int cishu(int n)
{
double sum;
int t;
sum = 1;
t = 1;
while (t <= n)
{
sum = sum * 2;
t = t + 1;
}
sum = sum - 1;
system("pause");
return sum;
}
截图
test7
代码
#include<stdio.h>
#include<stdlib.h>
int is_prime(int n);
int main()
{
int n;
n = 4;
while (n < 21)
{
int i, a1, a2;
a1 = 0;
a2 = 0;
i = n - 1;
while (a1 * a2 == 0)
{
i = i - 1;
a1 = 0;
a2 = 0;
a1 = is_prime(i);
a2 = is_prime(n - i);
}
printf("%d = %d + %d\n", n, i, n - i);
n = n + 2;
}
system("pause");
return 0;
}
int is_prime(int n)
{
int t, a;
t = 2;
a = 1;
while(t<n)
{
if (n % t == 0)
a = 0;
t = t + 1;
}
return a;
}
截图
test8
代码
#include <stdio.h>
#include<stdlib.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: ");
}
system("pause");
return 0;
}
long func(long s)
{
long t, a;
t = 0;
a = 0;
while(s>0)
{
a = s % 10;
if (a % 2 == 1)
t = 10 * t + a;
s = s / 10;
}
long x, y;
x = 0;
y = 0;
while(t>0)
{
x = t % 10;
y = y * 10 + x;
t = t / 10;
}
return y;
}
截图
标签:return,int,sum,long,实验,func,printf From: https://www.cnblogs.com/nuyoah720/p/17291165.html