You may not understand, but having too many pursuers can be quite troublesome.
Especially when you don't know who to choose.
At times like these, it might be helpful to consider the so-called "destined fate index."
Legend has it that everyone is born with a special destiny, which can be classified using a "special number."
The closer the special numbers are, the more compatible the individuals are said to be.
Add up the digits of the birth year, month, and day until you get a single-digit number, which is your special number.).
For example, if you were born on September 28, 1990, your special number would be 2.
1 + 9 + 9 + 0 + 9 + 2 + 8 = 38
3 + 8 = 11
1 + 1 = 2
Given the birthday of a certain cute girl and her suitors, please calculate who will be the lucky one to win the favor of the cute girl.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF input, the first line contains three integers, representing the cute girl's birth year, month, day (guaranteed she is still alive). Then there is an integer N (1 <= N <= 100), indicating how many suitors the cute girl has. Followed by N lines, each line contains three integers, representing the birth year, month, day of each suitor. | Output an integer K representing the suitor whose special number is closest to the cute girl's. If there are multiple closest matches, output the one that appears earlier in the input, with the first one winning. |
1990 9 28 1 1992 4 1 1991 10 15 2 1992 4 1 1992 3 2 | 1 1 |
Thought Process
Use strings to store the birth year, month, and day. When comparing the differences, use less than so that there won't be a need to check for identical difference values.
Sample Code-ZeroJudge A286: Destined?
#include <iostream>
using namespace std;
int calc (string a, string b, string c)
{
string arr[3] = {a, b, c};
int ans = 0;
for (int i = 0; i<3; i++)
{
for (int j = 0; j<arr[i].length(); j++)
{
ans += int(arr[i][j] - '0');
}
}
while (ans >= 10)
{
string str = to_string(ans);
ans = 0;
for (int i = 0; i<str.length(); i++)
{
ans += int(str[i] - '0');
}
}
return ans;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.sync_with_stdio(0);
cout.tie(0);
string a, b, c;
while (cin >> a >> b >> c)
{
int special = calc(a, b, c), N, difference = 999999, ans;
cin >> N;
for (int i = 1; i<=N; i++)
{
cin >> a >> b >> c;
int count = calc(a, b, c), variance = abs(count - special);
if (variance < difference)
{
difference = variance;
ans = i;
}
}
cout << ans << "\n";
}
}
//ZeroJudge A286
//Dr. SeanXD