Huawei Machine Test 4[programming question] Delete number

Source: Internet
Author: User

[Programming questions] The delete has an array of a[n] order to store 0~n-1, requiring every two number to delete a number, to the end of the loop to the beginning of the continuation of the last deleted number of the original subscript position. Take 8 numbers (n=7) as an example: {0,1,2,3,4,5,6,7},0->1->2 (delete)->3->4->5 (delete), 6->7->0 (delete), so loop until the last number is deleted. Input Description:
Each set of data is one line of an integer n (less than or equal to 1000), the number of members of the array, and if greater than 1000, the a[999] is evaluated.

Output Description:
The original subscript position of the last deleted number line output.
Input Example:
8
Output Example:
6

Ideas:


ImportJava.util.*; Public classmain{ Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in);  Do{            intn =Sc.nextint (); int[] arr =New int[n];  for(inti=0;i<n;i++) Arr[i]=i; Final intDelflag = n + 1;//Remove Flag bit            intCurrentSize = n;//records the current effective length of the array (that is, the number of elements that are not set to the delete flag), and finally becomes 0            Final intSTEP = 2;//Step Size            intCount = 0;//Step Count            intLastdelindex = 0;//Record the subscript of the last deleted element            inti = 0;//cyclic subscript               while(CurrentSize! = 0) {                  if(Arr[i]! = Delflag) {//To interpret whether the current element equals a delete flag                    if(count++ = = STEP) {//when the step count satisfies the step sizeArr[i] = Delflag;//To place an element as a delete flag bitLastdelindex = i;//record the subscript of the placecurrentsize--;//effective array length minus 1Count = 0;//Step Count Zeroing//System.out.println ("Deleted index is" + i% n); }} I= (i + 1)% n;//Subscript take-over to achieve cyclic subscript} System.out.println (Lastdelindex); } while(Sc.hasnext ()); }}

Huawei Machine Test 4[programming question] Delete number

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.