Use C # To obtain a continuous sequence with the sum of several Integers of 500 (web interview questions)

Source: Internet
Author: User

I accidentally saw this computing question on the Internet. I wrote it in my spare time. I want to write more easily.

1 private void button#click (object sender, EventArgs e)
2 {
3 // first, find the maximum number of Integers to add. Obviously starting from 1
4 int a = 0;
5 int bignum = 0;
6 for (int I = 1; I <= 500; I ++)
7 {
8 if (a <= 500)
9 {
10 a = a + I;
11 bignum = I;
12}
13}
14 string s = "";
15
16 for (int j = 1; j <= 500; j ++)
17 {
18 int c = 0;
19 int d = j;
20 for (int k = 1; k <= bignum; k ++)
21 {
22 c = j + k;
23 d + = c;
24 if (d = 500)
25 {
26 s = j. ToString () + "-" + k. ToString () + ",";
27}
28}
29}
30 MessageBox. Show (s );
31}

 

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.