"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