C程序-PAT-1011 A+B 和 C

给定区间 [−2​^31​​,2​^31​​] 内的 3 个整数 A、B 和 C,请判断 A+B 是否大于 C。

输入格式:

输入第 1 行给出正整数 T (≤10),是测试用例的个数。随后给出 T 组测试用例,每组占一行,顺序给出 A、B 和 C。整数间以空格分隔。

输出格式:

对每组测试用例,在一行中输出 Case #X: true 如果 A+B>C,否则输出 Case #X: false,其中 X 是测试用例的编号(从 1 开始)。

输入样例:

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

输出样例:

Case #1: false
Case #2: true
Case #3: true
Case #4: false
#include <iostream>
#include <stdio.h>

int main( ) 
{
	int T,i;
	long long A,B,C;
	scanf("%d",&T);
	for(i=1;i<=T;i++)
	{
		scanf("%lld%lld%lld",&A,&B,&C);
		printf("Case #%d: ",i);
		if(A+B>C)
			printf("true\n");
		else
			printf("false\n");
	}
	return 0;
}

 

```c #include <stdio.h> #include <stdlib.h> #define MAX_N 100000 #define MAX_K 5000 struct Student { int t_score; // 天梯赛分数 int pat_score; // PAT分数 int flag; // 是否达到面试分数线 } students[MAX_N]; int cmp(const void *a, const void *b) { struct Student *s1 = (struct Student *)a; struct Student *s2 = (struct Student *)b; if (s1->t_score != s2->t_score) { return s1->t_score - s2->t_score; } else if (s1->pat_score != s2->pat_score) { return s1->pat_score - s2->pat_score; } else { return 0; } } int main() { int n, k, s; int i, j, count; int max_count[MAX_K + 1]; // dp数组 int max_index[MAX_K + 1]; // 记录dp数组中最大值的下标 scanf("%d %d %d", &n, &k, &s); for (i = 0; i < n; i++) { scanf("%d %d", &students[i].t_score, &students[i].pat_score); students[i].flag = (students[i].pat_score >= s) ? 1 : 0; } qsort(students, n, sizeof(struct Student), cmp); count = 0; for (i = 0; i < n; i++) { if (students[i].t_score >= 175) { count++; } } for (i = 1; i <= k; i++) { max_count[i] = 0; max_index[i] = 0; } for (i = 0; i < n; i++) { if (students[i].t_score < 175) { continue; } for (j = k; j >= 1; j--) { if (students[i].flag == 1 && students[i].t_score >= max_index[j] && max_count[j - 1] + 1 > max_count[j]) { max_count[j] = max_count[j - 1] + 1; max_index[j] = students[i].t_score; } else if (students[i].t_score > max_index[j] && max_count[j - 1] + 1 > max_count[j]) { max_count[j] = max_count[j - 1] + 1; max_index[j] = students[i].t_score; } } } printf("%d\n", max_count[k]); return 0; } ``` 其中,`struct Student` 表示学生的天梯赛分数、PAT分数和是否达到面试分数线。程序先读入参赛学生的信息,然后按照要求排序,统计得分不低于 175 分的学生人数。接下来,使用动态规划求解最多能向企业推荐的学生人数。`max_count` 数组表示前i个学生中选j个并且最后一个学生的天梯赛分数为xi时的最大选取数,`max_index` 数组表示对应的最后一个学生的天梯赛分数。对于每个学生,从后往前遍历`max_count` 数组,更新最大值和最后一个学生的天梯赛分数。最后输出`max_count[k]` 即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值