Blue Bridge Cup Question 0410

1. Bill error

Title Description: A
certain secret-related unit issued a certain kind of bills, and all of them should be recovered at the end of the year.

Each bill has a unique ID number. The ID numbers of all bills are continuous throughout the year, but the starting number of the ID is randomly selected.

Because of the negligence of the staff, an error occurred when entering the ID number, which caused an ID to be broken and another ID to be renumbered.

Your task is to find out the ID of the broken number and the ID of the multiple number through programming.

It is assumed that the number breakage cannot occur at the maximum and minimum numbers.

The program is required to first enter an integer N (N <100) to indicate the number of data rows that follow.
Then read in N rows of data.
The data length of each line is unequal, and it is a number of positive integers (not greater than 100) separated by spaces (not greater than 100000).
Each integer represents an ID number.

The program is required to output 1 line, containing two integers mn, separated by spaces.
Among them, m represents broken ID, n represents multiple ID

For example:
User input:
2
5 6 8 11 9
10 12 9

Then the program output:
7 9

Another example:
user input:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

Then the program output:
105 120

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    private static Scanner input;

    public static void main(String[] args) {
        input = new Scanner(System.in);
        ArrayList list = new ArrayList<Integer>();

        int N = input.nextInt();
        input.nextLine(); // 去掉整数后面的换行符
        for (int i = 0; i < N; i++) {
            String line = input.nextLine();
            String[] split = line.split(" ");
            for (int j = 0; j < split.length; j++) {
                list.add(Integer.parseInt(split[j]));
            }
        }
        
        Collections.sort(list); // 对集合进行排序
        int a = 0, b = 0; // a接收断号的 b接收重号的
        for (int i = 1; i < list.size(); i++) {
            int cur = (int) (list.get(i));
            int pre = (int) (list.get(i - 1));
            if (cur - pre == 2) {
                a = (int)(list.get(i)) - 1;
            }
            if (cur - pre == 0) {
                b = (int)(list.get(i));
            }
        }
        System.out.println(a + " " + b);
    }

}

2. Lucky number

Title description The
lucky number was named by Polish mathematician Ulam. It is generated using a "sieve method" similar to generating prime numbers.

Start by writing the natural numbers 1, 2, 3, 4, 5, 6, ...

1 is the first lucky number.
We start with the number 2. Delete all items whose serial number is divisible by 2 to become:

1 _ 3 _ 5 _ 7 _ 9 …

Tighten them and reorder them as:

1 3 5 7 9…. At this time, 3 is the second lucky number, and then all the numbers that can be divided by 3 are deleted. Note that it is the serial number position, not whether the number itself is evenly divisible by 3 !! The deletion should be 5, 11, 17,…

At this time, 7 is the third lucky number, and then delete the number position that can be divided by 7 (19,39, ...)

The last remaining sequence is similar:

1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, …

This question requires:

Enter two positive integers mn, separated by spaces (m <n <1000 * 1000) The
program outputs the number of lucky numbers between m and n (excluding m and n).

For example:
User input:
1 20
Program output:
5

For example:
User input:
30 69
Program output:
8

Resource convention:
peak memory consumption (including virtual machine) <64M
CPU consumption <2000ms

Please output strictly according to the requirements, and do not print extra content like: "Please input ..."

All code is placed in the same source file. After debugging, copy and submit the source code.
Note: Do not use package statements. Do not use the features of jdk1.6 and above.
Note: The name of the main class must be: Main, otherwise it will be treated as invalid code.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class 幸运数 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        List<Integer> list = new ArrayList<>();
        for(int i=1;i<=m;i++){
            list.add(new Integer(i));
        }
        
        int luck = 2; //幸运数
        int k = 1;  //下标
        List<Integer> remove = new ArrayList<>();  //等待被移除的数字
        while(luck<=list.size()){
            int cnt = 0;//代表list中目前删除了几个数字
            for(int i=luck;i<=list.size();i++){  
                if(i%luck==0){
                    remove.add(i);
                }
            }
            
            
            for(int j=0;j<remove.size();j++){//将下标为remove中的数字移除
                list.remove(remove.get(j)-(cnt++)-1);
            }
            remove.clear();

            luck = list.get(k++);
        }
        int count = 0;
        for(int i=0;i<list.size();i++){
            if(list.get(i)>n&&list.get(i)<m){
                count++;
            }
        }
        System.out.println(count);
    }
}
import java.util.Scanner;  
public class Main {  
    static int G[]=new int [1000*1000+1];
    static int n;
    public static void main(String []args)  
    {  
        Scanner cs = new Scanner(System.in); 
        int m=cs.nextInt();
        n=cs.nextInt();
        //首先从1开始写出自然数1,2,3,4,5,6,....
        for(int i=1;i<=n;i++){
            G[i]=i;
        }
        //从2这个数开始。把所有序号能被2整除的项删除
        f(2);
        int xuhao=2;
        while(G[xuhao]<=pr1()){  //一直取幸运数,幸运数依次是数组中下标为2、3、4、5、6……对应的值
            f(G[xuhao]);
            xuhao++;
        }
        //计算结果
        int answer=0;
        for(int i=1;i<=n;i++){
            if(G[i]>=n)   //当G[i]超出n就不必要再算下去了
                break;
            if(G[i]>m)
                answer++;
        }
        System.out.println(answer);
    }
    static int pr1(){   //计算缩减后数组的长度,为了进一步加快算法的速度,避免每次都遍历整个n长度的数组
        int i=1;
        while(G[i]>0){
            i++;
        }
        return i-1;
    }
    static void f(int xuhao){  //删除对应位置的元素后缩紧
        for(int i=xuhao;i<=n;i+=xuhao){
                G[i]=0;
        }
        int j=1;
        for(int i=1;i<=n;i++){   //把它们缩紧,重新记序
            if(G[i]>0){
                int temp=G[i];
                G[i]=0;
                G[j++]=temp;
            }
        }
    }
}  
Published 44 original articles · Likes2 · Visits 540

Guess you like

Origin blog.csdn.net/qq_43699776/article/details/105442539