首页 > 其他分享 >实验三

实验三

时间:2023-03-31 11:56:39浏览次数:37  
标签:return int long 实验 func ans include

任务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;
 }
 
 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);         
}

回答:每间隔1000ms,随机打印0~25个空白行和0~80个空白列再打印hi,April~。总共反复操作10次。

任务2:

代码1:

#include <stdio.h>
#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! = %lld\n", i, fac(i));

    return 0;
}

long long fac(int n) {
    static long long p = 1;
    printf("p=%lld\n",p);
    p = p * n;

    return p;
}

截图

代码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可以使局部变量在该函数结束时不会被系统回收空间,而是继续存在并保留原有值。

任务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 = %lld\n", n, f);
    }

    return 0;
}

long long func(int n){
    long long f;
    if(n==1)
       f=1;
    else
       f=2*func(n-1)+1;
    return f;
}

截图

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

截图

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

截图

代码2:

#include <stdio.h>
#include <math.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 ans;
    if(y==0)
        ans=1;
    else
        ans=x*mypow(x,fabs(y)-1);
    if(y<0)
        ans=1/ans;    
    return ans;
}

截图

任务6:

代码:

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

截图

任务7:

代码:

#include <stdio.h>
#include <math.h>
int is_prime(double n);
int sovle(int ans);
int main(){
    int n,ans;
    while(scanf("%d",&ans)!=EOF){
        n=sovle(ans);
        printf("%d=%d+%d\n",ans,ans-n,n);
    }
    return 0;
} 
int sovle(int ans){
    double n=ans-2.0;
    while(is_prime(n)==0||is_prime(ans-n)==0){
        n--;
    }
    return (int)n;
}
int is_prime(double n){
    int m;
    for(m=2;;m++){
        if(m>sqrt(n))
            return 1;
        else
            if((int)n%m==0)
                return 0;
    }
}

截图

任务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 t=0;
    double i;
    while(s!=0){
        if(s%10%2==1)
            t=t+s%10*pow(10.0,i),i++;
        s=s/10;
    }
    return t;
}

截图

 

标签:return,int,long,实验,func,ans,include
From: https://www.cnblogs.com/FC000/p/17273740.html

相关文章

  • 202031607130-杨国周 实验一 软件工程准备—初识软件工程
    实验一软件工程准备项目内容班级博客链接https://edu.cnblogs.com/campus/xbsf/2020CSSE本次作业要求链接https://edu.cnblogs.com/campus/xbsf/2020CSSE/homework/12938我的课程学习目标学习软件工程的基本概念、方法和工具,提高软件开发的质量和效率。本......
  • c实验3
     实验任务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);intmain(){intline,col,i......
  • 实验3
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprintf_text(intline,intcol,chartext[]);voidprintf_spaces(intn);voidprintf_blanks(intn);intmain(){intline,col,i;......
  • 202031607202-李锋斌 实验一 软件工程准备 对软件工程的初步认识
    实验一软件工程准备项目内容班级博客链接2023春软件工程2020级计算机科学与技术本次作业要求链接实验一软件工程准备我的课程学习目标学习软件工程的基本概念和方法,提高软件开发能力。本次作业在哪些方面帮我实现学习目标通过完成任务1-任务5,我了解了博......
  • 实验3
    task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);7voidprint_blank_lines(intn);8intmain(){9intli......
  • 实验3
    实验任务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);intmain(){ intlin......
  • 实验3
    task1程序源码#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,co......
  • ChCore—实验 3:进程与线程、异常处理 部分记录
    思考题1:内核从完成必要的初始化到用户态程序的过程是怎么样的?尝试描述一下调用关系。内核启动到用户程序启动的流程:main├──uart_init├──mm_init├──arch_interrupt_init├──create_root_thread│├──create_root_cap_group│├──__create_......
  • 实验3
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数......
  • 实验3
    实验任务一:#include<time.h>#include<stdio.h>#include<stdlib.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,co......