POJ 6187:称体重

“ Ctrl AC!一起 AC!”

题目:忘题戳这

分析:遍历,枚举每个人的体重,但是可以判断大人和小孩,缩小枚举范围。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node {
	char name;
	int age;
}a[4];
bool cmp(struct node a, struct node b) {
	return a.age < b.age;//根据年龄排序
}
int main() {
	for (int i = 10; i <= 30; i += 10) {
		for (int j = 10; j <= 30; j += 10) {
			for (int k = j + 10; k <=50; k += 10) {
				for (int e = j + 10; e <= 50; e += 10) {
					if (i + j<k && i + e>k + j&&i!=j&&k!=e&&i+k==j+e) {
						a[0].name = 'z', a[0].age = i;
						a[1].name = 'q', a[1].age = k;
						a[2].name = 's', a[2].age = j;
						a[3].name = 'l', a[3].age = e;
					}
				}
			}
		}
	}
	sort(a, a + 4, cmp);
	for (int i = 0; i < 4; i++) {
		cout << a[i].name << " " << a[i].age << endl;
	}
	return 0;
}

感谢阅读!!!

“ Ctrl AC!一起 AC!”

上一篇:刷题-力扣-面试题 02.08. 环路检测


下一篇:POJ 简单dp(1)