C + + outputs all and consecutive positive sequences of S (Ox-Guest sword offer)

Source: Internet
Author: User
Xiao Ming likes maths very much, one day when he is doing mathematics homework, ask to calculate 9~16 and,// 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//////////////////////////////////////////outputs all successive positive sequences for S. Sequence in order from small to large, the sequence of starting numbers from small to large order//the Bull Sword Point offer perfect through. #include <iostream>using namespace std;void grial (int x) {int i = 0; int A;int b;int Data[x];int k = 0; for (i=1;i<x;i++) {int sum = 0;for (int j = i;j<x;j++) {sum+=j;if (sum==x) {A=i;b=j;bre AK;} if (sum>x) {break;}} if (sum==x)   {for (int j = a;j<=b;j++) {data[k++]=j;}}} for (i = 0;i<k;i++)//equivalent to Vector<vector<int> > in the offer of the sword. {cout<<data[i]<< "  ";} Cout<<endl;} int main () {grial (9); return 0;}

C + + outputs all and consecutive positive sequences of S (Ox-Guest sword offer)

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.