0117521-  

task 1

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define N 80
void printText(int line, int col, char text[]);
void printSpaces(int n); 
void printBlankLines(int n); 
int main()
{
int line, col, i;
char text[N] = "hi, May~";
srand(time(0)); 
for(i=1; i<=10; ++i)
{
line = rand()%25;
col = rand()%80;
printText(line, col, text);
Sleep(1000); 
}
system("pause");
return 0;
}
void printSpaces(int n)
{
int i;
for(i=1; i<=n; ++i)
printf(" ");
}
void printBlankLines(int n)
{
int i;
for(i=1; i<=n; ++i)
printf("\n");
}
void printText(int line, int col, char text[])
{
printBlankLines(line-1); 
printSpaces(col-1);
printf("%s",text);
}

程序功能:打印十个hi,May~,间隔的行数在0~24之间随机取值,列数在0~79之间随机取值

 

task2-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! = %lld\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;
}

static  变量

1.作为静态局部变量

静态存储,编译器在静态存储区为它分配内存单元。它的值不会因为函数调用结束而清除,下次被调用时,它的值是上次被调用后的值。

2.作为静态全局变量

可以被该文件所有函数访问。存储位置和局部变量特性相同

 

task3

    #include <stdio.h>
    #include<stdlib.h>
    long long fun(int n); 
    int main()
    {
        int n;
        long long f;
        while (scanf("%d", &n) != EOF)
        {
        f = fun(n); 
        printf("n = %d, f = %lld\n", n, f);
        }
        system("pause");
        return 0;
    }
    long long fun(int n)
    {   
        if(n==1)
        return 1;
    else
        return fun(n-1)*2+1;

}

 

task4

#include<stdio.h>
#include<stdlib.h>
int i;
void hanoi(unsigned n,char from,char to,char temp);
void move(unsigned n,char from,char to);
int main()
{   
    unsigned n;
    while(scanf("%u",&n)!=EOF)
    {    i=0;
        hanoi(n,'A','C','B');
    printf("一共移动了%d次",i);}
    
    system("pause");
}
void hanoi(unsigned n,char from,char to,char temp)
{ if(n==1)
   move(n,from,to);
else
    {
    hanoi(n-1,from,temp,to);
    move(n,from,to);
    hanoi(n-1,temp,to,from);}


}
void move (unsigned n,char from,char to)
{    i++;
    printf("%u:%c-->%c\n",n,from,to);



}

task3-5

 

#include<stdio.h>
#include<stdlib.h>
int is_Prime(int n);
int main()
{ int n,p,q,pflag=0,qflag=0;
  for(n=4;n<=20;n+=2)
  {
      for(p=2;p<=n;p++)
      {q=n-p;
       pflag=is_Prime(p);
       qflag=is_Prime(q);
 
    if(pflag==1&&qflag==1)
    {
        printf("%d=%d+%d\n",n,p,q);
        break;
    }

    }
  }
system("pause");
}
int is_Prime(int n)
{    int i;
    for(i=2;i<n-1;i++)
    {    if(n%i==0)
        break;
    }

    if(i>=n-1)
        return 1;
    else
        return 0;

}

 

task6

 

#include<stdio.h>
#include<stdlib.h>
long fun(long s);
int main()
{    long s,t;
    printf("Enter a number:");
    while(scanf("%ld",&s) != EOF)
    {    t=fun(s);
        printf("new number is:%ld\n\n",t);
        printf("Enter a number:");
    
    }


    system("pause");
}
long fun(long s)
{    long i,p,t=0,x=0,m;
    p=s;
    while(p!=0)
   {        
    i=p%10;
    if(i%2==1)
    t=t*10+i;
     p/=10;
   }
    while(t!=0)
  {    m=t%10;
    x=x*10+m;
    t/=10;
  }

    return x;


}

 

posted on 2022-04-19 15:42  董璇  阅读(106)  评论(4)    收藏  举报