首页 > 其他分享 >实验三

实验三

时间:2023-04-02 14:44:51浏览次数:30  
标签:return int long 实验 func printf include

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);
    }
    
    return 0;
}

//打印n个空格
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);
    print_spaces(col-1);
    printf("%s", text);
} 

作用:在空(line-1)行,空(col-1)列的一个随机位置打印“hi,April~”这个字符,整个过程持续1000ms

 

task.2.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! =%11d\n", i ,fac(i));
    
    return 0;
} 

long long fac(int n){
    static long long p = 1;
    
    p=p*n;
    printf("p = %11d\n" ,p);
    return p;
}

 

task.2.2

#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特性:在一个函数被调用结束后不清空内部的赋值,而是保存下来,等待下一次被使用

 

task.3

#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=%11d\n", n, f);
    }
    return 0;
} 
long long func(int n){
    int i,t,m;
    t=2;
    m=2;    
    for(i = 1; i < n; i++){
        m*=t;
    }
    m=m-1;
    return m;
}

 

 

 

task.4

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

 

 

task.5.1

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

 

task.5.2

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

 

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);
int cnt=0;
int main(){
    unsigned int n;
    while(scanf("%u" , &n) != EOF){
    hanoi(n,'A','B','C');
    printf("一共移动了%d次\n",cnt);
    cnt=0;
}
    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);
    cnt++;
}

 

 

task.7

#include<stdio.h>
#include<math.h>
int is_prime(int);
int main(){
    int yuanshu;
    int i;
    while(scanf("%d", &yuanshu)!=EOF){
    for(i = 2;;i++){
        if(is_prime(i)==1){
        if(is_prime(yuanshu-i)==1){
            printf("%d=%d+%d",yuanshu,i,yuanshu-i);
            break;
        }
    }
    }
}
    return 0;
} 

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

 

 

task.8

#include<stdio.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){
    int n=0;
    int sum=0;
    while(s!=0){
        n=n*10+s%10;
        s=s/10;
    }
    int a;
    while(n!=0){
        a=n%10;
        if(a%2!=0){
            sum=sum*10+a;
        }
        n=n/10;
    }
    return sum;
} 

 

标签:return,int,long,实验,func,printf,include
From: https://www.cnblogs.com/autism-lotus1/p/17280449.html

相关文章

  • 实验一-密码引擎-3-加密API研究
    密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客链接和代......
  • 实验三
    1.实验任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn);1......
  • 202031607221-王彦润 实验一 软件工程准备—博客园技巧与博客首秀
    1、项目和内容简介项目内容班级博客链接2023年春软件工程本次作业要求链接实验一我的课程学习目标注册博客园和Github账号,学习使用博客园,了解Github的基本操作本次作业在哪些方面帮我实现学习目标1.初步了解博客园软件和Github的基本操作;初步了解学......
  • 202031603143-郭思彤 实验一 软件工程准备-对软件工程的初步了解
    项目内容班级博客链接20级卓越班本次作业要求链接实验一软件工程准备我的课程学习目标1.深入使用博客园进行学习2.了解Github的基本操作3.拓展关于软件工程的知识本次作业在哪些方面帮我实现学习目标1.学会了使用markdown编辑器的基本操作2.通过提问......
  • 202031607128-张政文 实验一 软件工程准备
    1、项目和内容简介项目内容班级博客链接2023年春软件工程(2020级计算机科学与技术)(西北师范大学-计算机科学与工程学院)本次作业要求链接实验一软件工程准备我的课程学习目标注册博客园和Github账号,学习使用博客园,了解Github的基本操作。本次作业在哪些......
  • 202031607230-王格 实验一 软件工程准备--构建之法与博客首秀
    实验一软件工程准备项目内容班级博客链接2023年春软件工程本次作业要求链接实验一软件工程准备我的课程学习目标1.学习博客园软件开发者学习社区使用技巧和经验。2.了解Github工具的基本操作3.阅读《现代软件工程—构建之法》,深入了解什么是软件工程......
  • 实验一 软件工程准备
    1、项目和内容简介 项目内容班级博客链接https://edu.cnblogs.com/campus/xbsf/2020CSSE本次作业要求链接https://edu.cnblogs.com/campus/xbsf/2020CSSE/homework/12938我的课程学习目标注册博客园和Github账号,学习使用博客园,了解Github的基本操作。本次作......
  • 实验三
    实验三task.1 程序源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(i......
  • 实验三 函数
    实验1.#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intma......
  • 三月三十日第三次实验
    实验一:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,c......