The sword refers to the offer series 49--seeking 1+2+...+n and

Source: Internet
Author: User

"title" Beg 1+2+3+...+n,
* Requires no use of multiplication, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C).

1  PackageCom.exe10.offer;2 3 /**4 * "title" asks 1+2+3+...+n,5 * requires no use of multiplication, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C). 6  * @authorWGS7  *8  */9  Public classSumofn {Ten     intResult=0; One      A      Public BooleanCalcintN) { -result+=N; -         returnN!=0 && Calc (n-1);//as long as the n!=0 has been recursively performing the add operation. the     } -      -      Public intGetsum (intN) { -         if(n<0)return-1; +Calc (n);//as long as n==0 in the Calc function jumps out of the Calc function, executes the return result of the next step -         returnresult; +     } A      Public Static voidMain (string[] args) { atSumofn s=Newsumofn (); -         intN=s.getsum (-10); - System.out.println (n); -  -     } -  in}

The sword refers to the offer series 49--seeking 1+2+...+n and

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.