-
Description:
-
James liked mathematics very much. One day when he was doing his math homework, he asked to calculate 9 ~ He immediately wrote the correct answer to the sum of 16: 100. But he is not satisfied with this. He is wondering how many continuous positive number sequences the sum of 100 (including at least two numbers ). It wasn't long before he got another set of continuous positive numbers and 100 sequences:, 22. Now, I will give you the question. Can you quickly find all the continuous positive number sequences for S? Good luck!
-
Input:
-
Multiple groups of data are input.
Each data group contains only one INTEGER (S <= 1,000,000 ). If S is a negative number, end the input.
-
Output:
-
Corresponding to each group of data. If there is no continuous positive number sequence for S, "pity!" Is output !"; Otherwise, output all continuous positive numbers for S in the ascending order of the Start number. The end of each data group ends.
-
Sample input:
-
45100-1
-
Sample output:
-
Pity!#2 3#9 10 11 12 13 14 15 1618 19 20 21 22#
Ideas:
Use two pointers: low and high. Initially, low points to 1 and high points to 2. If the value from low accumulates to high is smaller than S, the value of high increases by 1. Otherwise, low increases by 1, until low is more than half of S.
Code:
/* The continuous positive number sequence for S by rowandjj2014/8/14 */# include <stdio. h> # include <stdlib. h> bool findsequence (INT s) {If (S <3) {return false;} bool flag = false; int low = 1, high = 2; int curnum = 3; int middle = (S + 1)> 1; while (low <middle) // to find all sequences that meet the conditions {int I; If (curnum = s) // find a result {flag = true; for (I = low; I <= high; I ++) // print. Note the format {if (I! = High) {printf ("% d", I);} else {printf ("% d", I) ;}} printf ("\ n ");} while (curnum> S & low <middle) // The current sum is greater than S, so you need to increase low {curnum-= low; low ++; If (curnum = s) {flag = true; for (I = low; I <= high; I ++) {if (I! = High) {printf ("% d", I) ;}else {printf ("% d", I) ;}} printf ("\ n ");}} high ++; curnum + = high;} return flag;} int main () {int N; while (scanf ("% d", & N )! = EOF & n> = 0) {If (! Findsequence (N) {printf ("pity! \ N ");} printf (" # \ n ");} return 0 ;}