Gold coins
Time limit:1000 ms |
|
Memory limit:30000 K |
Total submissions:20913 |
|
Accepted:13098 |
Description
The King pays his loyal knight in gold coins. on the first day of his service, the knight has es one gold coin. on each of the next two days (the second and third days of service), the knight waits es two gold coins. on each of the next three days (the fourth, th, and sixth days of service), the knight es three gold coins. on each of the next four days (the seventh, eighth, ninth, and tenth days of service), the knight waits es four gold coins. this pattern of payments will continue indefinitely: After processing ing n gold coins on each of N consecutive days, the knight will receive N + 1 gold coins on each of the next n + 1 consecutive days, where N is any positive integer.
Your program will determine the total number of gold coins paid to the knight in any given number of days (starting from day 1 ).
Input
The input contains at least one, but no more than 21 lines. each line of the input file (could t the last one) contains data for one test case of the problem, consisting of exactly one INTEGER (in the range 1 .. 10000), representing the number of days. the end of the input is signaled by a line containing the number 0.
Output
There is exactly one line of output for each test case. this line contains the number of days from the corresponding line of input, followed by one blank space and the total number of gold coins paid to the knight in the given number of days, starting with Day 1.
Sample Input
106711151610010000100021220
Sample output
10 306 147 1811 3515 5516 61100 94510000 9428201000 2982021 9122 98
Source
Rocky Mountain 2004
Solution:
Obtain one coin on the first day, two coins each day in the next two days, and three coins each day in the next three days. For example, the number of coins obtained every day in the first six days is 1, 2, 3, 3.
Ask how many coins you get on the nth day. The idea is to judge whether day n includes several consecutive time periods (for example, the day when the coin is obtained, the two days when the coin is obtained, and the three days when the coin is obtained) and the days when the extra time is not a complete time period. Use extra in the Code and record the days that are the first consecutive time periods. Total Gold Coins = those obtained in the complete time period + those obtained in the remaining days
Code:
# Include <queue> # include <iostream> # include <string. h >#include <stack >#include <iomanip> # include <cmath> using namespace STD; int CNT (int n) {int totalday = 0; int get = 0; int I; int extra; // not the day of a complete continuous I day, for example, 1 2 2 3, extra is 1, the day on which 3 Coins are obtained for (I = 1; I <= N; I ++) {totalday + = I; If (totalday> N) {Extra = N-(totalday-I); break;} Get + = I * I;} Get + = extra * I; return get;} int main () {int N; while (CIN >>n & N) {cout <n <"" <CNT (n) <Endl ;}return 0 ;}