PTA-练习1

目录

实验2-3-8 计算火车运行时间

实验2-4-4 求简单交错序列前N项和

实验2-4-5 输出华氏-摄氏温度转换表

实验3-4 统计字符[2]

实验3-5 查询水果价格

实验3-11 求一元二次方程的根

实验4-1-1 统计数字字符和空格


实验2-3-8 计算火车运行时间

时钟数有两种情况:hh>=10    hh>=0&&hh<10

分钟数有三种情况:mm<0      mm>=0&&mm<10      mm>=10

分别讨论一共2*3=6种情况,如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){

    int a,b;
    scanf("%d %d",&a,&b);

    int hh=b/100-a/100;
    int mm=b%100-a%100;
    if(hh>10&&mm>10){
        printf("%d:%d",hh,mm);
    }else if(hh<10&&mm>10){
        printf("0%d:%d",hh,mm);
    }else if(hh<10&&mm>=0&&mm<10){
        printf("0%d:0%d",hh,mm);
    }else if(hh<=10&&mm<0){
        printf("0%d:0%d",hh-1,mm+60);
    }else if(hh>10&&mm>=0&&mm<10){
        printf("%d:0%d",hh,mm);
    }else if(hh>10&&mm<0){
        printf("%d:0%d",hh-1,mm+60);
    }

    return 0;
}

%d,整数输出1,如果需要固定位数,比如需要输出两位%2d,这个时候输出变成了 ‘ 1’

如果需要固定字符串部位的话,就是在前面加上%0.2d,使用0补位‘01’

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){

    int a,b;
    scanf("%d %d",&a,&b);

    int hh=b/100-a/100;
    int mm=b%100-a%100;
    if(mm<0){
        mm=mm+60;
        hh--;
    }
    printf("%0.2d:%0.2d",hh,mm);
    
    

    return 0;
}

 


实验2-4-4 求简单交错序列前N项和

******计算浮点数加减法的时候,一点要使用小数!!*****

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){

    int N;
    scanf("%d",&N);
    int i=1;
    double sum=0;

    while(i<=N){

        if(i&1){//奇数
            sum=sum+1.0/(3*i-2);
        }else{
            sum=sum-1.0/(3*i-2);//使用小数
        }
        i++;
    }
    
    printf("sum = %.3lf",sum);
    
    return 0;
}

 实验2-4-5 输出华氏-摄氏温度转换表

C语言学习--占位符-CSDN博客

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){

    int lower,upper;
    scanf("%d %d",&lower,&upper);

    if(upper<lower||upper>100||lower<0||upper<0){
        printf("Invalid.\n");
    }else{
        int i=lower;
        float celsius;
        printf("fahr celsius\n");
        while(i<=upper){
            printf("%d%6.1f\n",i,5.0*(i-32)/9.0);
            i=i+2;
        }
    }

    return 0;
}

实验3-4 统计字符[2]

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){

    int N=0;
    char c;
    scanf("%d%c",&N,&c);//最后这里还要输入一个回车用于换行
    
    int letter=0,blank=0,digit=0,other=0;
    for(int i=1;i<=N;i++){
        scanf("%c",&c);
        if(c>='a'&&c<='z'||c>='A'&&c<='Z'){
            letter++;
        }else if(c==' '||c=='\n'){//空格或者回车
            blank++;
        }else if(c>='0'&&c<='9'){
            digit++;
        }else{
            other++;
        }
    }
    
    printf("letter = %d, blank = %d, digit = %d, other = %d\n",letter,blank,digit,other);
    
    return 0;
}

实验3-5 查询水果价格

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){

    int n;
    printf("[1] apple\n");
    printf("[2] pear\n");
    printf("[3] orange\n");
    printf("[4] grape\n");
    printf("[0] exit\n");

    for(int i=1;;i++){
        scanf("%d",&n);
        if(i>5||n==0){
            break;
        }
        switch(n){
            case 1:
                printf("price = 3.00\n");
                break;
            case 2:
                printf("price = 2.50\n");
                break;
            case 3:
                printf("price = 4.10\n");
                break;
            case 4:
                printf("price = 10.20\n");
                break;
            default:
                printf("price = 0.00\n");
                break;    
        }       
    }//of for

    return 0;
}


实验3-11 求一元二次方程的根

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){

    double a,b,c;
    scanf("%lf %lf %lf",&a,&b,&c);

    if(a==b&&a==c&&a==0){
        printf("Zero Equation");
    }
    if(a==b&&a==0&&c!=0){
        printf("Not An Equation");
    }
    
    float derta=b*b-4.0*a*c;
    if(a==0&&b!=0){//a=0,一元一次方程
        printf("%.2f",-1.0*c/b);
    }else if(a!=0&&derta==0){//derta=0,只有一个根
        printf("%.2f",(-1.0*b)/(2*a));
    }else if(a!=0&&derta>0){//derta>0,有2个实数根
        
        printf("%.2f\n",((-1.0*b)+sqrt(derta))/(2*a));
        printf("%.2f\n",((-1.0*b)-sqrt(derta))/(2*a));
    }else if(a!=0&&derta<0){//derta<0,有2个虚数数根
        float real=(-1.0*b)/(2*a);
        if(b==0){
            real=0;
        }
        printf("%.2f+%.2fi\n",real,sqrt(derta*(-1.0))/(2.0*a));
        printf("%.2f-%.2fi\n",real,sqrt(derta*(-1.0))/(2.0*a));
    }
    
    return 0;
}

 实验4-1-1 统计数字字符和空格

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){


    char s;
    scanf("%c",&s);
    //字符串本身就是一个数组(指针,不需要&传递地址)
    //字符串最后一个字符默认为空格
    int blank=0,digit=0,other=0;
    while(s!='\n'){//一直输入单个字符,直到换行为止。
        switch(s){
            case' ':
                blank++;
                break;
            case'0':
            case'1':
            case'2':
            case'3':
            case'4':
            case'5':
            case'6':
            case'7':
            case'8':
            case'9':
                digit++;
                break;
            default:
                other++;
                break;
        }
        scanf("%c",&s);
        
    }

    printf("blank = %d, digit = %d, other = %d",blank,digit,other);
    return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){


    char s[100];
    gets(s);
    //字符串本身就是一个数组(指针,不需要&传递地址)
    //字符串最后一个字符默认为空格
    int blank=0,digit=0,other=0;
    int i=0;
    while(i<strlen(s)){
        switch(s[i]){
            case' ':
                blank++;
                break;
            case'0':
            case'1':
            case'2':
            case'3':
            case'4':
            case'5':
            case'6':
            case'7':
            case'8':
            case'9':
                digit++;
                break;
            default:
                other++;
                break;
        }
            i++;
    }
    printf("blank = %d, digit = %d, other = %d",blank,digit,other);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西柚小萌新

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值