C++实验报告3

习题4-4

#include <stdio.h>
int main(){
    int a,b,c,maxn;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b) maxn=a;
    else maxn=b;
    if(c>maxn) maxn=c;
    printf("%d",maxn);
    return 0;
}

习题4-5

#include <stdio.h>
#include <math.h>
int main(){
    int a;
    do{
        printf("请输入一个小于1000并大于0的正整数\n");
        scanf("%d",&a);
    }while(a>=1000||a<=0);
    float ans=sqrt(a);
    printf("它的平方根为%f",ans);
    return 0;
}

习题4-6

#include <stdio.h>
int main(){
    int y,x;
    scanf("%d",&x);
    if(x<1) y=x;
    else if(1<=x&&x<10) y=2*x-1;
    else y=3*x-11;
    printf("%d",y);
    return 0;
}

习题4-7

#include <stdio.h>
int main(){
    int y,x;
    scanf("%d",&x);
    if(x<0) y=-1;
    else if(x==0) y=0;
    else y=1;
    printf("%d",y);
    return 0;
}   

习题4-8

#include <stdio.h>
int main(){
    int x;
    scanf("%d",&x);
    if(x>90) putchar('A');
    else if(x>80) putchar('B');
    else if(x>70) putchar('C');
    else if(x>60) putchar('D');
    else putchar('E');
    return 0;
}

习题4-9

#include <stdio.h>
int main(){
    int n,t,a,b;
    int digit;
    scanf("%d",&n);
    t=n;
    a=b=digit=0;
    while(t>0){
        digit++; //digit记录n的位数
        a=a*10+t%10; //a记录n的倒序写法
        t/=10;
    }
    printf("%d\n",digit);
    while(digit>0){ 
        printf("%d ",a%10); //将倒序数从低位到高位输出即为正序
        a/=10;
        digit--;
    }
    printf("\n");
    b=n; //a记录n的正序写法
    while(b>0){
        printf("%d ",b%10); //将正序数从低位到高位输出即为倒序
        b/=10;
    }
    return 0;
}

附加1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
    srand(time(0));
    int a=rand()%100+1;
    int b;
    printf("请输入一个数\n");
    while(1){
        scanf("%d",&b);
        if(a==b){
            printf("你猜中了\n");
            return 0;
        }
        else if(a<b) printf("你猜大了\n");
        else printf("你猜小了\n");
    }
    return 0;
}

附加2

#include <stdio.h>
int main(){
    float a,b;
    char sign;
    scanf("%f%c%f",&a,&sign,&b);
    switch (sign)
    {
        case '+':
            printf("%f",a+b);
            return 0;
        case '-':
            printf("%f",a-b);
            return 0;
        case '*':
            printf("%f",a*b);
            return 0;
        case '/':
            printf("%f",a/b);
            return 0;
    }
    return 0;
}

转载于:https://www.cnblogs.com/kangyupl/p/10621341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值