当前位置:网站首页>[编程题]删数
[编程题]删数
2022-04-22 01:45:00 【河工大某某】
[编程题]删数
有一个数组 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),为数组成员数
输出描述:
一行输出最后一个被删掉的数的原始下标位置。
输入例子1:
8
输出例子1:
6
输入例子2:
1
输出例子2:
0
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
int n = sc.nextInt();
LinkedList<Integer> queue = new LinkedList<>();
for(int i = 0; i < n; i++) {
queue.addLast(i);
}
while(queue.size() > 1) {
queue.addLast(queue.getFirst());
queue.removeFirst();
queue.addLast(queue.getFirst());
queue.removeFirst();
queue.removeFirst();
}
System.out.println(queue.getFirst());
}
}
}
版权声明
本文为[河工大某某]所创,转载请带上原文链接,感谢
https://blog.csdn.net/diOSyu/article/details/124309400
边栏推荐
- Common evaluation indexes of medical image segmentation
- Can SQL server remotely manipulate a set procedure or function and return results without using account login?
- Isla obscura deep sea biological series NFT market platform
- 真羡慕哪些不用发朋友圈的人
- pycharm实现设置自动的参数注释标识
- The sandbox and design egg have reached a cooperation to jointly establish a virtual space Como small world
- Code source daily question div1 (501-507)
- In depth understanding Construction mode of thread synchronization in. Net (1) user mode construction
- DVWA series - XSS (cross site script injection (reflective, storage, DOM))
- 彩色飘带字体怎么制作?AE教程教给你
猜你喜欢

How to make color ribbon font? AE tutorial teaches you

(一)postman遇到报错:There was an error connecting to XXXX

Isla obscura deep sea biological series NFT market platform
![[Lua program to control the mobile phone to chat] - it is used to keep the number. You can put down your hand for automatic chat and keep the number](/img/0e/883fc94b5db0dd0a16343746717d0d.png)
[Lua program to control the mobile phone to chat] - it is used to keep the number. You can put down your hand for automatic chat and keep the number

Station B charging | charging more than 1500 a week, Yinfa up is the new dark horse of station B?

VMware虚拟机安装详细教程网络NAT、网桥配置

(1) There was an error connecting to XXXX

Oracle 在视图对象中看不见视图,但用select语句可查询到视图数据

阿里大牛教你自己写Android第三方库,2021最新阿里Android面试流程

PHY6222国产低功耗多协议蓝牙SoC芯片
随机推荐
代码源每日一题 div1(101-109)
Ali Daniel collates, Android interview multiple-choice questions
基础30讲 第九讲 一元函数积分学的几何应用
Thales
What are the good products of gold insurance in 2022?
分布式事务解决方案
[several common buttons in swing]
Common evaluation indexes of medical image segmentation
VMware虚拟机安装详细教程网络NAT、网桥配置
Code source daily question div1 (501-507)
Network protocol analysis summary
DVWA series - XSS (cross site script injection (reflective, storage, DOM))
VMware virtual machine installation detailed tutorial network NAT and bridge configuration
Educator -- IP packet fragmentation
MySQL queries the data from the 20th of last month to the 19th of the current system month (this month)
QT校园易物系统
Test d'automatisation de l'extrémité mobile appium - - mise en place d'un simulateur et d'un environnement réel
【无标题】
gin的回调函数语法的js实现 & es6的类用法 &js click回调函数打印e怎么实现的
Isla obscura deep sea biological series NFT market platform