[编程题]删数

有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。

输入描述:

每组数据为一行一个整数n(小于等于1000),为数组成员数,如果大于1000,则对a[999]进行计算。

输出描述:

一行输出最后一个被删掉的数的原始下标位置。 
示例1 
输入


输出

6

参考:https://blog.csdn.net/u012507032/article/details/77945981

https://blog.csdn.net/qunqunstyle99/article/details/94405117

 1 #include <iostream>
 2 #include<bits/stdc++.h>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n;
 9     while (cin >> n)
10     {
11         if (n > 1000)
12             n = 999;
13         queue<int> qu;
14         for (int i = 0; i < n; i++)
15         {
16             qu.push(i);
17         }
18 
19         while (n > 1)
20         {
21             int temp = qu.front();
22             qu.push(temp);
23             qu.pop();
24             temp = qu.front();
25             qu.push(temp);
26             qu.pop();
27             qu.pop();//隔两个数后删掉一个
28             n--;
29         }
30         cout<<qu.front()<<endl;
31 
32     }
33 
34     return 0;
35 }
 1 package text;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Scanner;
 6 
 7 public class Text {
 8     public static void main(String[] args) {
 9         Scanner scanner = new Scanner(System.in);
10         while (scanner.hasNext()) {
11             int n = scanner.nextInt();
12             if (n>1000) {
13                 n = 999;
14             }
15             
16             List<Integer> list = new ArrayList<Integer>();
17             for (int i = 0; i < n; i++) {
18                 list.add(i);
19             }
20             
21             for (int i = 2; list.size() > 1; i = (i+2)%list.size()) {//注意这里用的是list.size(),不是n
22                 list.remove(i);
23             }
24             System.out.println(list.get(0));
25         }
26     }
27 }
原文地址:https://www.cnblogs.com/hemeiwolong/p/12285513.html