以下是一个百马百担问题的C语言编程实现:
- #include <stdio.h>
- int main() {
- int n, m, k;
- scanf("%d%d%d", &n, &m, &k);
- int a[n], b[m], c[k];
- for (int i = 0; i < n; i++) {
- scanf("%d", &a[i]);
- }
- for (int i = 0; i < m; i++) {
- scanf("%d", &b[i]);
- }
- for (int i = 0; i < k; i++) {
- scanf("%d", &c[i]);
- }
- int sum_a = 0, sum_b = 0, sum_c = 0;
- for (int i = 0; i < n; i++) {
- sum_a += a[i];
- }
- for (int i = 0; i < m; i++) {
- sum_b += b[i];
- }
- for (int i = 0; i < k; i++) {
- sum_c += c[i];
- }
- if (sum_a == sum_b && sum_b == sum_c) {
- printf("YES\n");
- } else {
- printf("NO\n");
- }
- return 0;
- }
这个程序首先读取三个整数n、m、k,分别表示马的数量、担的数量和人的数量。然后分别读取n个马的数量、m个担的重量、k个人的人数,存储在数组a、b、c中。接着计算马的总重量sum_a、担的总重量sum_b、人的总重量sum_c,如果三者相等则输出"YES",否则输出"NO"。
当然,这个程序可以根据需要进行一些改进。以下是一个可能的改进版本,它可以处理更一般的情况,并且输出更详细的信息:
- #include <stdio.h>
- int main() {
- int n, m, k;
- scanf("%d%d%d", &n, &m, &k);
- int a[n], b[m], c[k];
- for (int i = 0; i < n; i++) {
- scanf("%d", &a[i]);
- }
- for (int i = 0; i < m; i++) {
- scanf("%d", &b[i]);
- }
- for (int i = 0; i < k; i++) {
- scanf("%d", &c[i]);
- }
- int sum_a = 0, sum_b = 0, sum_c = 0;
- for (int i = 0; i < n; i++) {
- sum_a += a[i];
- }
- for (int i = 0; i < m; i++) {
- sum_b += b[i];
- }
- for (int i = 0; i < k; i++) {
- sum_c += c[i];
- }
- if (sum_a == sum_b && sum_b == sum_c) {
- printf("YES, all weights are equal.\n");
- } else {
- printf("NO, weights are not equal.\n");
- if (sum_a != sum_b) {
- printf("The total weight of horses is %d, but the total weight of donkeys is %d.\n", sum_a, sum_b);
- }
- if (sum_b != sum_c) {
- printf("The total weight of donkeys is %d, but the total weight of people is %d.\n", sum_b, sum_c);
- }
- }
- return 0;
- }
这个版本的程序在比较重量之前先分别计算了马、担和人各自的重量总和,这样如果重量不等,程序可以输出哪个总和与其它总和不相等,以便用户更好地理解问题。
好的,以下是对上述程序的进一步改进,增加了对输入数据的验证和错误处理:
- #include <stdio.h>
- int main() {
- int n, m, k;
- scanf("%d%d%d", &n, &m, &k);
- // 检查输入数据的有效性
- if (n <= 0 || m <= 0 || k <= 0) {
- printf("Invalid input: all counts must be positive.\n");
- return 0;
- }
- int a[n], b[m], c[k];
- for (int i = 0; i < n; i++) {
- scanf("%d", &a[i]);
- }
- for (int i = 0; i < m; i++) {
- scanf("%d", &b[i]);
- }
- for (int i = 0; i < k; i++) {
- scanf("%d", &c[i]);
- }
- // 检查输入数据的总和是否为整数,并且不超过整数的最大值
- long long sum_a = 0, sum_b = 0, sum_c = 0;
- for (int i = 0; i < n; i++) {
- sum_a += a[i];
- }
- for (int i = 0; i < m; i++) {
- sum_b += b[i];
- }
- for (int i = 0; i < k; i++) {
- sum_c += c[i];
- }
- if (sum_a != sum_b || sum_b != sum_c) {
- printf("The total weights are not equal.\n");
- if (sum_a != sum_b) {
- printf("The total weight of horses is %lld, but the total weight of donkeys is %lld.\n", sum_a, sum_b);
- }
- if (sum_b != sum_c) {
- printf("The total weight of donkeys is %lld, but the total weight of people is %lld.\n", sum_b, sum_c);
- }
- } else {
- printf("YES, all weights are equal.\n");
- }
- return 0;
- }
这个版本的程序增加了对输入数据的验证,包括检查输入数据的有效性和总和是否为整数,并且不超过整数的最大值。如果输入数据无效或总和不是整数,程序将输出错误信息。如果总和不等,程序将输出哪个总和与其它总和不相等,以便用户更好地理解问题。