TEST 1
源码:
#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);
}
功能:在第line行第col列随机生成10个hi,april~,其中line是1~24的随机数,col是1~79的随机数
TEST2
源码1:
#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>
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;
}
static特性:只读取一次数据,数据在程序运行过程中不断被刷新
TEST3
#include <stdio.h>long long func(int n); int main() {
int n;
long long f; while (scanf("%d", &n) != EOF) {
f = func(n);
printf("n = %d, f = %lld\n", n, f);
} return 0;
} long long func (int n){
int f;
if(n==1)
f=1;
else
f=(func(n-1)+1)*2-1;
return f;
}
![](/i/l/?n=23&i=blog/3121614/202303/3121614-20230330171622454-330030436.png)
TEST4
#include <stdio.h>
int func(int n, int m);
int main() {
int n, m;
while(scanf("%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 c;
if (m>n)
c=0;
else{if(m==0)
c=1;
else{
if(m==1)
c=n;
else
c=func(n-1,m)+func(n-1,m-1);
}}
return c;
}
![](/i/l/?n=23&i=blog/3121614/202303/3121614-20230331175415663-1414486578.png)
TEST5
不递归:
#include<stdio.h>
#include<stdlib.h>
double mypow(int x,int y);
int main(){
int x,y;
double ans;
while(scanf("%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 s=1;
int i;
for(i=1;i<=abs(y);i++)
{s=s*x;}
if(y>=0)
s=s;
else
s=1/s;
return s;
}
递归:
#include<stdio.h>
#include<stdlib.h>
double mypow(int x,int y);
int main(){
int x,y;
double ans;
while(scanf("%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 s;
if(y==0)
s=1.0;
else{if(y>0)
s=x*mypow(x,y-1);
else
s=1/mypow(x,abs(y));
}
return s;
}
TEST6
#include<stdio.h>
#include<stdlib.h>
void hanoi(int n,char A,char B,char C);
void move(int n,char A,char B);
static int t=0;
int main(){
char A,B,C;
int n;
while(scanf("%d",&n)!=EOF){
t=0;
hanoi(n,'A','B','C');
printf("一共移动了%d次\n",t);}
return 0;
}
void move(int n,char A,char C){
printf("%d:%c-->%c\n",n,A,C);
t++;
}
void hanoi(int n,char A,char B,char C){
if(n==1)
move (n,A,C);
else{
hanoi(n-1,A,C,B);
move(n,A,C);
hanoi(n-1,B,A,C);
}
}
TEST 7
#include<stdio.h>
#include<stdlib.h>
int is_prime(int n);
int main(){
int i,n=3,m=4,sign;
for(i=1;i<=20;i++)
{sign=1;
if(i%2==1)
continue;
else
{for(n=2;n<=i/2;n++)
{if(sign==0)
break;
else
{
for(m=2;m<=i;m++)
{if(m+n==i&&is_prime(m)+is_prime(n)==2&&m!=n)
{printf("%d=%d+%d",i,n,m);
printf("\n");
sign=0;
break;
}}}}}}
return 0;
}
int is_prime(int n){
int i,sign;
for(i=2;i<n;i++)
{if(n%i==0)
break;}
if(i>=n)
sign=1;
else
sign=0;
return sign;
}
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("%ld",&s)!=EOF){
t=func(s);
printf("new number is:%ld\n\n",t);
printf("Enter a number:");
}
return 0;
}
long func(long s){
long t=0;
int rem,n=0,m;
for (m=0;s>=1;)
{if(s%10%2==1)
{rem=s%10;
t=t+rem*pow(10,n);
n++;}
s=s/10;
m++;
}
return t;
}
标签:return,int,long,char,实验,func,printf From: https://www.cnblogs.com/cwpdpk/p/17273514.html