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)