第三次上机作业

task 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); 
    }
    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); 
}

 程序实现的功能:在0~24行与0~79列范围内随机打印十次“hi,April~”。

task 2

#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;

    p = p * n;
    printf("p = %11d\n",p);
    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;// (1) i = 2 + 0 + 1 = 3 (2) i = 3 + 8 + 1 = 12
    m = i + a + b;// (1) m = 3 + 4 +1 = 8 (2) m = 12 + 4 + 1 = 17

    return m;
}

总结 :1.局部static变量在程序执行到声明处时候被首次初始化,以后的函数调用将不再进行初始化

         2.它始终驻留在全局数据区,直到程序运行结束。但其作用域为局部作用域,当定义它的函数或语句结束时,其作用域随之结束。

task 3

#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 = %lld\n", n, f);
    }
    system("pause");
    return 0;
}
long long func(int n)
{
    if (n==1)
    return 1;
    else
    return func(n-1)*2 + 1;
}

task 4

#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)
{
    if(n == m||m == 0)
    return 1;
    if(m>n)
    return 0;
    else
    return func(n-1,m)+func(n-1,m-1);
}

task 5

#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 s=1,i,k;
    double ans = 1;
    if(y>=0){
    for(i=1;i<=y;i++)
    {
        s = s*x;
    }
    return s;}
    else
    for(k=1;k<=-y;k++)
        ans = ans/x; 
    return ans;
}

#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)
{
    if (y>=0){
    if (y == 0)
    return 1;
    else
    return x*mypow(x,y-1);
    }
    if(y<0)
    if (y == 0)
    return 1;
    else
    return 1/mypow(x,-y);
}

task 6

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

task 7

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int is_prime(int x);
int main()
{
    int s,i;
    while(scanf("%d",&s)!=EOF){
    for(i=2;i<=s*1.0/2;i++)
    {
        if(is_prime(i)&&is_prime(s-i)){
        printf("%d = %d + %d\n",s,i,s-i);
        break;
        }
    }
}
    system("pause");
    return 0;
}

int is_prime(int x)
{
    int i,k;
    k = sqrt(1.0*x);
    for(i=2;i<=k;i++)
    if(x%i==0)
        return 0;
        if(i>k)
            return 1;
}

task 8

#include <stdio.h>
#include <math.h>
#include<stdlib.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 ans,z=0,q=0,j;
    for(;s!=0;)
    {
        ans = s % 10;
        s = s / 10;
        if(ans%2 != 0)
          z = z*10 +ans;    
    }
    for(;z!=0;)
    {
        j = z % 10;
        z = z/10;
        q = q*10 + j;
        
    }
    return q;
}

 

posted @ 2023-03-31 13:14  chen,,  阅读(27)  评论(0编辑  收藏  举报