码迷,mamicode.com
首页 > 编程语言 > 详细

java 实例

时间:2017-03-15 21:36:01      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:and   span   个数   rom   rgs   空格   system.in   包含   list   

题目要求如下:

第一行包含一个整数N,初始的元素的数量为L;

第二行包含N个空格分隔的整数表示L;

第三行包含一个整型的Q(查询的数量);

2Q随后的行表示查询,和每个查询超过两行。

1<=N<=4000

1<=Q<=4000

每个元素都是一个32位整数

输入:

5
12 0 1 78 12  //L
2     //需要输入的个数
Insert    
5 23    //将23放到位置5
Delete
0        //删除0

输出:

0 1 78 12 23

输出最后的结果。

代码如下:

import java.util.Scanner;
import java.util.LinkedList;

public class Solution {
    public static void main(String[] args) {
        /* Create and fill Linked List of Integers */
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < N; i++) {
            int value = scan.nextInt();
            list.add(value);
        }
        
        /* Perfrom queries on Linked List */
        int Q = scan.nextInt();
        for (int i = 0; i < Q; i++) {
            String action = scan.next();
            if (action.equals("Insert")) {
                int index = scan.nextInt();
                int value = scan.nextInt();
                list.add(index, value);
            } else { // "Delete"
                int index = scan.nextInt();
                list.remove(index);
            }
        }
        scan.close();
        
        /* Print our updated Linked List */
        for (Integer num : list) {
            System.out.print(num + " ");
        }
    }
}
    

 

java 实例

标签:and   span   个数   rom   rgs   空格   system.in   包含   list   

原文地址:http://www.cnblogs.com/Angella/p/6556543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!