PAT 黄金级(题目及答案)

//之前用数字一直存在测试点过不了,后来看到说要用字符串,因为位数太多了。
#include<stdio.h>
#include<string.h>
int main()
{
    char n[50];
    scanf("%s", &n);
    int num = strlen(n);
    
    int i = 0; int cnt = 0;
    for (i = 0; i <num; i++)
    {
        if (n[i] == '2')
        {
            cnt++;
        }
    }
    if (n[0] == '-')
    {
        num -= 1;
    }
    double result;
    result = cnt * 1.0 / num;
    if (n[0] == '-')
    {
        result *= 1.5;
        if ((n[num ]-'0') % 2 == 0)
        {
            result *= 2;
         }
    }
    else if ((n[num-1]-'0') % 2 == 0)
    {
        result *= 2;
    }
    result *= 100;
    printf("%.2lf%%", result);
    return 0;

}

之前的用整型存储

#include<stdio.h>
#include<math.h>
int main()
{
    long n,_n;
    int cnts=0,cntp = 0;
    scanf("%ld", &n);
    _n = n;
    while (fabs(_n) > 0)
    {
        if (fabs(_n%10)==2)
        {
            cntp++;
        }
        cnts++; _n /= 10;
    }
    double result = (cntp * 1.0 / cnts);
    
    if (n < 0)
    {
        result *= 1.5;
    }
    if (n % 2 == 0) {
        result *= 2;
    }
    result *= 100;
    printf("%.2lf%%", result);

    return 0;
}

也希望大家能指点一下

这题要用到结构体数组

#include<stdio.h>

struct student {
    int sex;
    char name[9];
};
int main()
{
    struct student students[50];
    int i,j, n;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d %s", &students[i].sex,&students[i].name);
    }

    for (i = 0; i < n; i++)
    {
        for (j = n - 1; j > 0; j--)
        {
            if (students[i].sex != students[j].sex&&students[j].sex!=3)
            {
                printf("%s %s\n", students[i].name, students[j].name);
                students[j].sex = 3; students[i].sex = 3;
                break;
            }
        }
    }
    return 0;
}

前面三题过于简单,这里不再赘述

#include<stdio.h>
int main()
{
    int i,n;
    char a[3];
    int num1 = 0, num2 = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%s", a);
            
            scanf("%d %d", &num1, &num2);
    
            if ((num1 >= 15 && num1 <= 20) && (num2 >= 50 && num2 <= 70))
            {
                continue;
            }
            else printf("%s\n", a);
    }

    return 0;
}
#include<stdio.h>
int main()
{
    int num1 = 0,num2 = 0;
    scanf("%d %d", &num1, &num2);
    int sum = num1 + num2;
    int i = 0;
    for (i = 0; i < sum; i++)
    {
        printf("Wang!");
    }


    return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
    int i, n;
    double tall, height;
    double sheight;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%lf %lf", &tall, &height);
        sheight = (tall - 100) * 0.9;
        height /= 2.0;
        if (fabs(sheight - height) < sheight * 0.1)
        {
            printf("You are wan mei!\n");
        }
        else if (height > sheight)
        {
            printf("You are tai pang le!\n");
        }
        else printf("You are tai shou le!\n");
    }

    return 0;
}
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值