To iterate over an element in an array

Source: Internet
Author: User

An array of shapes, all of which are positive integers, such as 2,1,3,4,5,7, now require the deletion of a number per m number, to the end of the array, and then to the array header, and so on until all the number of the array is deleted, the last number of the deleted in the array of the original subscript.

For example: For an array of arr = [2,1,3,4,5,7], if M = 2 o'clock, the entire delete process (with the deletion marked as-1) is as follows:

Array of elements: [2,1,3,4,5,7]

First time: [2, 1, 3,-1, 5, 7]

Second time: [-1, 1, 3,-1, 5, 7]

Third time: [-1, 1, 3,-1,-1, 7]

Fourth time: [-1, 1,-1,-1,-1, 7]

Fifth time: [-1, 1,-1,-1,-1,-1]

Sixth time: [-1,-1,-1,-1,-1,-1]

Therefore, the last number to be deleted is 1, and its subscript in the array is 1.

The specific code, implemented as follows (Java edition):

1 ImportJava.util.Scanner;2 /**3 * Array number of circular delete problem, an array, for example, 2,1,3,4,5,7, every 2 number of deleted, deleted to the tail and then loop to the header, so repeatedly, until the last number is deleted4 * Output the subscript for the last deleted number, 15  * @authorJiajoa6 * Input includes 3 lines, first row n is the length of the array, second behavior n positive integers, separated by spaces, third behavior interval m7 * For example:8 * 69 * 2 1 3 4 5 7Ten * 2 One * Output: 1 A  */ -  Public classRecurrnumber { -  the      Public Static voidMain (string[] args) { -Scanner in =NewScanner (system.in); -         intn = in.nextint ();//an array of length n -         int[] arr =New int[n]; +          for(inti=0;i<n;i++){ -Arr[i] =in.nextint (); +         } A          at         intm = In.nextint ();//Delete one number per interval for M -         intindex =GetResult (arr,m); - System.out.println (index); - in.close (); -     } -      in      Public Static intGetResult (int[] arr,intm) { -         intLen =arr.length; to         intStart = 1; +         intCount=0; -         intFlag =m; the          while(flag>0){ *             if(count==len) $                  Break;Panax Notoginseng             if(start>=Len) { -Start = start%Len; the             } +             if(Arr[start]!=-1){ Aflag--; the             } +start++; -             if(flag==0){ $                 if(start>=Len) { $Start = start%Len; -                 } -                  while(Arr[start]==-1){ thestart++; -                     if(start>=Len) {WuyiStart = start%Len; the                     } -                 } WuArr[start]=-1; -count++; Aboutflag=m; $             } -         } -         returnstart; -     } A}

To iterate over an element in an array

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.