Q & A of the "Baidu Star" Program Design Competition (implemented in Java)

Source: Internet
Author: User

I am a beginner in Java and have been learning Java for a while. In order to improve programming capabilities, I recently found some questions about the program design competition on the Internet.

Every time you make one, you will post your answers to your blog, and strive:

Enrich your first technical blog

Second, exercise your endurance.

The most important thing is to ask you to criticize your younger brother and help me improve it more quickly. Here, I would like to thank you!

All right, serve food!

/*** Apr 1, 2013 * copyright (c) jackwang * all rights reserve * @ author <a href = "mailto: wangchengjack@163.com "> jackwang </a> */package COM. example. baidupro;/*** requirement: Title Description: a positive integer may be expressed as the sum of n (n> = 2) continuous positive integers, for example: 15 = 1 + 2 + 3 + 4 + 5 15 = 4 + 5 + 6 15 = 7 + 8 * please write a program, according to any input positive integer, find all continuous positive integer sequences that meet this requirement. Input data: A Positive Integer provided to the program as a command line parameter. * Output data: all positive integer sequences that conform to the topic description are printed on the standard output. Each row has one sequence, each sequence starts from the smallest positive integer of the sequence and is printed in ascending order. If the result has multiple sequences * print each sequence according to the size of the smallest positive integer of each sequence from small to large. In addition, the sequence cannot be repeated. integers in the sequence are separated by a space. If there is no matching sequence, "NONE" is output ". * For example, for 15, the output result is: 1 2 3 4 5 4 5 6 7 8 for 16. The output result is: None train of thought: * 1, all positive and even numbers cannot meet this rule (because the continuous number addition is satisfied (except 0, positive integer): the continuous N number addition must be an odd number, no matter N is odd or even * all positive odd numbers satisfy * 2, any odd number N: accumulate from K (starting from k = 1). If sum = n exists, one is found, k ++. Otherwise, K ++ stops when k = (n + 1)/2 and outputs none * 3, loop nesting ** @ author Wangcheng **/public class integerlist {/*** @ Param ARGs */public static void main (string [] ARGs) {int n = 17; getallintelist (n);} public static void getallintelist (int n) {If (0 = n % 2) {// All even numbers do not meet system. out. println ("NONE"); return;} For (int K = 1; k <= (n + 1)/2; k ++) {stringbuilder sb = new stringbuilder (); //, A Sb is created at the beginning of each cycle; int sum = 0; For (INT I = K; I <= (n + 1)/2; I ++) {sb. append (I + ""); sum + = I; If (sum = N) {system. out. println (sb. deletecharat (sb. length ()-1 ). tostring (); // remove the last extra space} If (sum> N) {// If sum> N stops the loop in advance, enter the next break ;}}}}}

The questions and my analysis are in the notes. Please correct them!

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.