Topic 1354: Sum of consecutive positive sequence time limits for S: 2 seconds Memory limit: 32 Mega Special: No submission: 1685 resolution: 511 title Description: Xiao Ming is very fond of mathematics, one day when he was doing math homework, asked to calculate the sum of 9~16, he immediately wrote the correct answer is 100. But he was not satisfied with it, and he wondered how many successive positive sequences of sum were 100 (including at least two numbers). Before long, he got another set of consecutive positive numbers and a sequence of 100:18,19,20,21,22. Now give you the question, can you also quickly find all and for the continuous positive sequence of s? Good luck! input: input has multiple sets of data. Each group of data consists of only 1 integers S (s<=1,000,000). If S is negative, the input is ended. Output: corresponding to each set of data, if not present and for the continuous positive sequence of s, then output "pity!"; Otherwise, the sequential positive sequence of all and S is output according to the starting number from small to large. The end of each set of data ends with a "#" sign. Sample input: 45100-1 sample output: pity! #2 3#9 10 11 12 13 14 15 1618 19 20 21 22#
#include <iostream> #include <stdio.h>using namespace std;void printcontinuesequence (int small,int big) { for (int i=small;i<big;i++) {printf ("%d", I); } printf ("%d\n", Big);} BOOL Findcontinuesequence (int sum) {bool found = false; if (sum<3) {return found; } int small = 1; int big = 2; int middle = (1+sum)/2; int cursum = Small+big; while (Small<middle) {if (cursum==sum) {found = true; Printcontinuesequence (SMALL,BIG); } while (Cursum>sum&&small<middle) {cursum-=small; small++; if (cursum==sum) {found = true; Printcontinuesequence (SMALL,BIG); }} big++; Cursum+=big; } return found;} int main () {int n; while (scanf ("%d", &n)!=eof&&n>=0) {if (!findcontinuesequence (n)) {printf ("pity!\n"); } printf ("#\n"); } return 0;}
Sword refers to the offer series source code-and a continuous positive sequence for s