美文网首页
001--20200408刷题

001--20200408刷题

作者: 糖纸疯了 | 来源:发表于2020-04-08 00:34 被阅读0次

1、答案链接

有一个数组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(删除),如此循环直到最后一个数被删除。

package com.enzoism.springboot.niuke.date20200408;

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

/**
 * Created with IntelliJ IDEA.
 * User: enzoism
 * Date: 2020/4/8- 0:38
 */
public class TestNArrayDelete {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = i;
            }
//            System.out.println(delete(arr));
            System.out.println(deleteAnalysis(n));
        }
        scanner.close();
    }

    public static int delete(int[] arr) {
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < arr.length; i++) {
            queue.add(arr[i]);
        }
        System.out.println(queue.toString());
        while (queue.size() != 1) {
            int index = 0;
            while (index != 2) {
                queue.add(queue.peek());
                System.out.println("--->构造前:" + queue.toString());
                Integer poll = queue.poll();
                System.out.println("--->构造移除:" + poll);
                System.out.println("--->构造后:" + queue.toString());
                index++;
            }
            Integer poll2 = queue.poll();
            System.out.println("--->移除:" + poll2);
        }
        return queue.element();
    }

    // 当前方法为上述方法的分析版本
    public static int deleteAnalysis(int count) {
        // 1、因为只求下标,所以数据对我们没有用,直接将数据转化成容易识别的索引(真实下标就是元素的数值)
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < count; i++) {
            queue.add(i);
        }
        System.out.println(queue.toString());
        //* 2、构造LinkedList
        //  1)每次遍历,将不移除的数据,添加到数组的尾部(模拟环状数据)
        //  2)该移除的数据,直接进行移除
        //  3)index为2,就是我们要间隔的位数(跳出循环条件)
        //  4)真实下标就是元素的数值
        // */

        while (queue.size() > 1) {
            int index = 0;
            while (index < 2) {
                // 如果不移除,继续往下走
                queue.add(queue.peek());
                System.out.println("--->构造1:" + queue.toString());
                Integer poll = queue.poll();
                System.out.println("--->构造移除:" + poll);
                System.out.println("--->构造2:" + queue.toString());
                index++;
            }
            // 如果移除
            Integer poll2 = queue.poll();
            System.out.println("--->移除:" + poll2);
        }
        return queue.element();
    }
}

  • 官方简单答案
import java.util.*;
 
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            if (n > 1000) {
                n = 999;
            }
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < n; i++) {
                list.add(i);
            }
            int i = 0;
            while (list.size() > 1) {
                i = (i + 2) % list.size();
                list.remove(i);
            }
            System.out.println(list.get(0));
        }
    }
}

2、答案见解析

每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            char[] c = in.next().toCharArray();
            StringBuffer sb = new StringBuffer();
            Set<Character> set = new HashSet<>();
            for (int i = 0; i < c.length; i++) {
                if (set.add(c[i]))
                    sb.append(c[i]);
            }
            System.out.println(sb.toString());
        }
    }
}

3、答案见解析

数独是一个我们都非常熟悉的经典游戏,运用计算机我们可以很快地解开数独难题,现在有一些简单的数独题目,请编写一个程序求解

package com.enzoism.springboot.niuke.date20200408;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: enzoism
 * Date: 2020/4/8- 1:52
 */
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int[][] data=new int[9][9];
            ArrayList<HashSet<Integer>> row=new ArrayList<HashSet<Integer>>();
            ArrayList<HashSet<Integer>> col=new ArrayList<HashSet<Integer>>();
            ArrayList<HashSet<Integer>> squ=new ArrayList<HashSet<Integer>>();

            for(int i=0;i<9;i++){
                row.add(new HashSet<Integer>());
                col.add(new HashSet<Integer>());
                squ.add(new HashSet<Integer>());
            }

            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    data[i][j]=sc.nextInt();
                    if(data[i][j]!=0){
                        row.get(i).add(data[i][j]);
                        col.get(j).add(data[i][j]);
                        squ.get(i/3*3+j/3).add(data[i][j]);
                    }
                }
            }

            dfs(data,row,col,squ,0);

            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    if(j!=8)
                        System.out.print(data[i][j]+" ");
                    else
                        System.out.println(data[i][j]);
                }
            }
        }
        sc.close();
    }


    public static boolean dfs(int[][] data,ArrayList<HashSet<Integer>> row,ArrayList<HashSet<Integer>> col,ArrayList<HashSet<Integer>> squ,int index){
        if(index==81)
            return true;
        int m=index/9;
        int n=index%9;
        int k=m/3*3+n/3;

        if(data[m][n]!=0){
            return dfs(data,row,col,squ,index+1);
        }
        else{
            for(int i=1;i<=9;i++){
                if(!row.get(m).contains(i) && !col.get(n).contains(i) && !squ.get(k).contains(i)){
                    data[m][n]=i;
                    row.get(m).add(i);
                    col.get(n).add(i);
                    squ.get(k).add(i);
                    if(dfs(data,row,col,squ,index+1))
                        return true;
                    data[m][n]=0;
                    row.get(m).remove(i);
                    col.get(n).remove(i);
                    squ.get(k).remove(i);
                }
            }
            return false;
        }
    }
}

相关文章

  • 001--20200408刷题

    1、答案链接 有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一...

  • 刷题刷题

    时间紧迫,任务繁重,又有疫情影响,搞的人心惶惶,一时间复习得不安宁,又舍不得摆烂。 在焦灼、惶恐的情绪中,紧张急迫...

  • 2022-09-16

    刷题,刷题还是刷题

  • 2018-07-16

    刷题,祸害的何止是教育? 报班,刷题;买练习册,刷题;家教,刷题;跟不上,刷题;学得好,刷题;为了抢跑,刷题;为了...

  • 刷题啊刷题

    因为月底又要考试,所以最近几天一直在刷题。按说是看了书再看视频再刷题效果比较好才是。可是来不及了啊。 上次考试,就...

  • 刷题啊刷题

    刷题啊刷题 因为11月中旬要举行期中考试,所以最近几天,学校精心组织,一直在刷题。按说是看了书再看PPT课件或教师...

  • 2020-02-01关于刷题的几个建议

    算法刷题 针对性刷题,刻意练习。刻意刷题!不是麻木刷题!刷题前一定要先看书,清楚明白为什么要刷这些题,这些题刷完能...

  • 刷题

    清早起来刷题 吃饭也在刷题 上厕所也在刷题 中午也在刷题 下午也在刷题 晚上也在刷题 一天到晚都在刷题 考试马上到...

  • 程序猿刷题网站你知道吗?

    Coderbyte 刷题网LeetCode 刷题网Stack Overflow 刷题网

  • 教你怎么做一只考试锦鲤

    考试前14天疯狂刷题,各个平台疯狂刷题,刷题就对了。 刷的越多,大脑记得越多,也许你刷10道题,能记住的只有1道题...

网友评论

      本文标题:001--20200408刷题

      本文链接:https://www.haomeiwen.com/subject/rnwaphtx.html