pta基础编程题目集 编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bobo1356/article/details/68068156

题目链接

5-7 12-24小时制

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int hour,minute;
    scanf("%d:%d",&hour,&minute);
    if(hour>12 && hour<24)  printf("%d:%d PM\n",hour-12,minute);
    else if(hour < 12)          printf("%d:%d AM\n",hour,minute);
    else if(hour == 12)         printf("12:%d PM\n",minute);
    else if(hour == 24)         printf("0:%d AM\n",minute);
    return 0;
}

5-8 超速判断

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int V;
    scanf("%d",&V);
    if(V > 60)  printf("Speed: %d - Speeding\n",V);
    else        printf("Speed: %d - OK\n",V);
    return 0;
}

5-9 用天平找小球

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int a,b,c;
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);

    if(a == b)  printf("C\n");
    if(a == c)  printf("B\n");
    if(b == c)  printf("A\n");
    return 0;
}

5-10 计算工资

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int years,hours,base;
    float salary;

    scanf("%d",&years);
    scanf("%d",&hours);

    if(years >= 5)  base = 50;
    else            base = 30;

    if(hours > 40)  salary = base*40 + 1.5*base*(hours-40);
    else salary = base*hours;

    printf("%.2f\n",salary);
    return 0;
}

5-11 分段计算居民水费

#include <stdio.h>

int main(int argc, const char * argv[]) {
    double x,y;
    scanf("%lf",&x);
    if(x <= 15) y = 4*x/3;
    else y = 2.5*x - 17.5;
    printf("%.2lf\n",y);
    return 0;
}

5-12 两个数的简单计算器

 #include <stdio.h>

int main(int argc, const char * argv[]) {
    char c;
    int a,b;

    scanf("%d %c %d",&a,&c,&b);
    if(c == '+')    printf("%d\n",a+b);
    else if(c == '-')    printf("%d\n",a-b);
    else if(c == '*')    printf("%d\n",a*b);
    else if(c == '/')    printf("%d\n",a/b);
    else if(c == '%')    printf("%d\n",a%b);
    else printf("ERROR\n");
    return 0;
}

5-13 日K蜡烛图

#include <stdio.h>

int main(int argc, const char * argv[]) {
    double open,high,low,close;
    scanf("%lf %lf %lf %lf",&open,&high,&low,&close);
    if(close < open)        printf("BW-Solid");
    else if(close > open)   printf("R-Hollow");
    else if(close == open)  printf("R-Cross");
    if(low<open && low<close && high>open && high>close)
        printf(" with Lower Shadow and Upper Shadow");
    else if(low<open && low<close)
        printf(" with Lower Shadow");
    else if(high>open && high>close)
        printf(" with Upper Shadow");

    printf("\n");
    return 0;
}

5-14 求整数段和

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int a,b,length,i,sum = 0;
    scanf("%d %d",&a,&b);
    length = b-a;
    for(i=0; i<=length; i++){
        sum += (a+i);
        printf("%5d",a+i);
        if((i+1)%5 == 0)
            printf("\n");
    }
    if((length+1)%5 != 0)   printf("\n");
    printf("Sum = %d\n",sum);

    return 0;
}

5-15 计算圆周率

#include <stdio.h>
int main()
{
    double i=1.0,threshold,a=1.0,b=1.0,result,tmp;
    scanf("%lf",&threshold);
    while (1) {
        a *= i;
        b *= (2*i+1);
        tmp = a/b;
        result += tmp;
        if(tmp < threshold){
            break;
        }
        i++;
    }
    printf("%.6lf\n",(result+1)*2);
    return 0;
}
展开阅读全文

没有更多推荐了,返回首页