java--Sword Point Offer (10)

Source: Internet
Author: User

46. Every year, children's Day will prepare some small gifts to visit the children of the orphanage, and this year is also the case. HF as a senior veteran of the cattle, naturally also prepared some small games. Among them, there is a game like this: first, let the children surround a big circle. Then, he randomly assigned a number m, so that the number of children numbered 0 began to count. Every time the child shout to M-1 to sing a song, and then can be in the Gift box arbitrary selection of gifts, and no longer back to the circle, from his next child, continue to 0...m-1 count off .... Go on like this .... Until the last child, you can not perform, and get the valuable "Detective Conan" Collector Edition (limited number of places!!) ^_^). Please try to think, which one of the children will get the present? (Note: The child's number is from 0 to n-1).

 Public classSolution { Public intLastremaining_solution (intNintm) {if(N < 1 | | m < 1)            return-1; intLast = 0;  for(inti = 2; I <= N; i + +) Last= (last + m)%i; returnLast ; }}

47. Ask for 1+2+3+...+n, you can not use the multiplication method, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C).

 Public class Solution {    int res= 0;      Public int Sum_solution (int  n) {        check (n);         return res;    }          Private boolean check (int  n) {        = res+n;         return N!=0&&check (n-1);}    }

java--Sword Point Offer (10)

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.