实验3

task1.c

#include <StdAfx.h>
#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 _tmain() 
{
    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(" ");
}
// 打印n行空白行
void print_blank_lines(int n) {
    int i;
    
    for(i = 1; i <= n; ++i)
    printf("\n");
}
// 在第line行第col列打印一段文本
void print_text(int line, int col, char text[]) {
    print_blank_lines(line-1); // 打印(line-1)行空行
    print_spaces(col-1); // 打印(col-1)列空格
    printf("%s", text); // 在第line行、col列输出text中字符串
}

task2.1.c

#include <StdAfx.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.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;
  
    return p;
}

task2.2.c

//

#include <StdAfx.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.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;
    m = i + a + b;
    i += m + 1;
    
    return m;
}

task3.c

#include <StdAfx.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

long long func(int m); // 函数声明

int _tmain()
{
    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 m)
{
    static int i, t = 1;
    
    for(i = 0;i<m;i++);
        t = t*2;

    return t + 1;
}

task.4.c

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>

int func(int n, int m);

int _tmain(int argc, _TCHAR* argv[])
{
    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 i, t = 1, a = n;

    for(i=0;i<m;i++){
        t = t*a;
        a = a - 1;
    }
    for(i=1;i<=m;i++)
        t = t/i;
    
    return t;
}

task5.1.c

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.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 i, t = 1;
    
    for(double i = 0;i < abs(y);i++)
            t = t*x;

    if(y >= 0)
        return (double) t;
    else 
        return (double) 1/t;
}

task5.2.c

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.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 i, t = 1;
if(y >= 0)
for(double i = 0;i < y;i++)
t = t*x;
else
for(double i = 0;i < (-y);i++)
t = t/x;

return (double) t;
}

task6.c

#include<stdafx.h>
#include<stdio.h>
#include<math.h>

void hanoi(unsigned n ,char from ,char temp, char to);
void mp(unsigned nth, char from , char to);
int mypow(int n);

int main()
{
    unsigned n,k; char from='A',temp='B',to='C';
    
    while(scanf("%u",&n)!=EOF)
    {
        hanoi(n,from,temp,to);
        
        k = mypow(n)-1;
        
        printf("一共移动盘子%u次\n",k);
    
    }
    
    return 0;
}
void hanoi(unsigned n ,char from ,char temp, char to)
{
    if(n==1)
        mp(n,from,to);
    else
    {
        hanoi(n-1,from,to,temp);
        mp(n,from,to);
        hanoi(n-1,temp,from,to);
    }
}
void mp(unsigned nth, char from , char to)
{
    printf("%u: %c --> %c\n",nth,from,to);
}

int mypow(int n)
{
    int i, t = 1;

    for(i = 0;i < n;i++)
        t = t*2;

    return t;
}

task7.c

#include <stdafx.h>
#include <stdio.h> 
#include <cmath>
#include <stdlib.h>
int is_prime(int n);

int main()
{  
    int i,k,a[15],j=0,l;

    for(k=2;k<=20;k++)
    {
            if(is_prime(k))
                a[j++]=k;
    }
     for(i=2;i<=10;i++)
        for(l=1;l<=10;l++)
            if (is_prime(2*i-a[l-1])){
                printf("%d = %d + %d\n",(2*i),a[l-1],(2*i-a[l-1]));break;
            }

    system("pause");
    return 0;
}
int is_prime(int n)
{
    int i,flag=1;
    int sql = sqrt(1.0*n);

    for(i=2;i <= sql;i++)
    {
        if(n%i==0) {flag=0;break;}
    }
    return flag;
} 

task8.c

#include <stdafx.h>
#include <stdio.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: ");
    }

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

 

posted @ 2023-04-05 21:12  大帅浩淇  阅读(4)  评论(0编辑  收藏  举报