Sword refers to the offer series source code-and a continuous positive sequence for s

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.