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~"
task 2.1
代码:
#include <stdio.h>
#include <stdlib.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!=%11d\n",i,fac(i));
system("pause");
return 0;
}
long long fac(int n){
static long long p=1;
printf("p = %11d\n",p);
p = p*n;
return p;
}
task2.2
#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;
}
运行结果:
task3
#include <stdio.h>
#include <stdlib.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 = %11d\n",n,f);
}
system("pause");
return 0;
}
long long func(int n)
{
long long ans;
if(n==0)
ans=0;
else
ans=2*(func(n-1)+1)-1;
return ans;
}
task4
代码:
#include <stdio.h>
#include <stdlib.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));
system("pause");
return 0;
}
int func(int n,int m)
{
int result;
if(m>n)
result=0;
else
if(m==n||m==0)
result=1;
else
result=func((n-1),m)+func((n-1),(m-1));
return result;
}
task5.1
代码:
#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);
}
system("pause");
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
for(i=1;i<=abs(y);i++)
ans=ans/x;
return ans;
}
task5.2
代码:
#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);
}
system("pause");
return 0;
}
double mypow(int x,int y)
{
double ans;
if(y==0)
ans=1;
else
if(y>0)
ans=x*mypow(x,(y-1));
else
ans=1/mypow(x,-y);
return ans;
}
task6
代码:
#include <stdio.h>
#include <stdlib.h>
int Hanoita(int n, char A, char B, char C);
int times(int n);
int main()
{
double sum;
int n;
while (scanf_s("%d", &n) != EOF)
{
Hanoita(n, 'A', 'B', 'C');
sum = times(n);
printf("一共移动了%.0f次。\n", sum);
}
system("pause");
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 times(int n)
{
double sum;
int t;
sum = 1;
t = 1;
while (t <= n)
{
sum = sum * 2;
t = t + 1;
}
sum = sum - 1;
return sum;
}
task 7
代码:
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n);
int main()
{
int n;
n=4;
while(n<=20){
int i,x,y;
x=0;
y=0;
i=n-1;
while(x*y==0){
i--;
x=0;
y=0;
x=is_prime(i);
y=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 i;
for (i = 2; i <= n; i++) {
if (n % i == 0) {
return 0;}
else {
return 1; }
}
}
task8
代码:
#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:");
}
system("pause");
return 0;
}
long func(long s)
{
long i,t;
i=0;
t=0;
while(s>0){
t=s%10;
if(t%2==1)
i=10*i+t;
s=s/10;
}
long m,n;
m=0;
n=0;
while(i>0)
{
m=i%10;
n=10*n+m;
i=i/10;}
return n;
}
标签:return,int,printf,long,实验,ans,include From: https://www.cnblogs.com/Suimiaotingfeng/p/17273721.html