注:最近这一系列ACM的内容,都是2年多之前的代码,自己回顾一下。 PATULJCI Submit: 1156 Accepted:704 Time Limit: 1000MS Memory Limit: 65535K Description
Every day, while the dwarves are busy in the mines, Snow White prepares dinner for them; seven chairs, seven plates, seven forks and seven knives for seven hungry dwarves.
One day nine dwarves came from the mines instead of seven (nobody knows how or why), each of them claiming to be one of Snow White's seven dwarves.
Luckily, each dwarf wears a hat with a positive integer less than 100 written on it. Snow White, a famous mathematician, realised long ago that the sum of numbers on the hats of her seven dwarves was exactly 100.
Write a program which determines which dwarves are legit, i.e. pick seven of nine numbers that add to 100.
Input
There are 9 lines of input. Each contains an integer between 1 and 99 (inclusive). All of the numbers will be distinct.
Note: The test data will be such that the solution is unique.
Output
Your program must produce exactly seven lines of output – the numbers on the hats of Snow White's seven dwarves. Output the numbers in any order.
Sample Input
7
8
10
13
15
19
20
23
25
Sample Output
7
8
10
13
19
20
23
Source
Croatian Open Competition in Informatics
七个小矮人突然变成九个小矮人了,但真正的七个小矮人头上的编号加起来刚好是100,计算出哪些是真正的小矮人。
方法很简单,先算出9个数之和,减100,则为多出来那两个数的和,然后计算是哪两个数之和。
#include <iostream>
using namespace std;
int main()
{
int N[9];
int sum = 0, n1, n2, flag = 1;
for (int i = 0; i < 9; i++)
{
cin >> N[i];
sum += N[i];
}
sum -= 100;
for (int i = 0; i < 9 && flag; i++)
for (int j = i + 1; j < 9 && flag; j++)
if ( (N[i] + N[j]) == sum)
{
n1 = i;
n2 = j;
flag = 0;
}
for (int i = 0; i < 9; i++)
if (i != n1 && i != n2)
cout << N[i] << endl;
// system("pause");
return 0;
}
o.boj 1059 PATULJCI
最新推荐文章于 2012-03-26 17:47:29 发布