头像

青影




离线:6天前



青影
7天前

题目描述

仅做笔记,值得注意的地方:
1 十进制数怎么转换为B进制
2 回文数的判断方法

样例

import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int b = sc.nextInt();
        for(int i = 1;i<=300;i++){
            String num = base(i*i,b);
            if(check(num)==true){
                System.out.println(base(i,b)+" "+num);
            }
        }
    }
    static char get1(int x){
        if(x <= 9) return (char)(x+'0');
        else return (char)(x-10+'A');
    }
    static String base(int n, int b){
        String num="";
        while(n != 0){
            num +=get1(n%b);
            n /= b;
        }
        String nums = new StringBuilder(num).reverse().toString();
        return nums;
    }
    static boolean check(String num){
        for(int i = 0,j=num.length() -1;i<j;i++,j--){
            if(num.charAt(i) != num.charAt(j)) return false;
        }
        return true;
    }
}



青影
4个月前

java

import java.util.*;

class Main{
    static int N = 1000010;
    static boolean[] st = new boolean[N];
    static int[] primes = new int[N];
    static int cnt = 0;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        getPrimes2(n);
        System.out.println(cnt);
    }
    //朴素筛法 nlogn
    static void getPrimes(int n){
        for(int i = 2;i <= n;i++){
            if(st[i] == false){//没有被筛过说明是质数
                //primes[cnt++] = n;
                cnt++;
            }
            for(int j = i+i;j<=n;j+=i){
                st[j] = true;
            }
        }
    }
    //朴素筛法改进 nloglogn
    static void getPrimes1(int n){
        for(int i = 2;i <= n;i++){
            if(st[i] == false){//没有被筛过说明是质数
                //primes[cnt++] = n;
                cnt++;
                for(int j = i+i;j<=n;j+=i){
                    st[j] = true;
                }
            }
        }
    }
    //线性筛法
    static void getPrimes2(int n){
        for(int i = 2;i <= n;i++){
            if(st[i] == false) primes[cnt++] =i; //如果没有筛过说明是质数,加到里面去
            for(int j = 0; primes[j] <=n/i;j++){
                st[primes[j]*i] =true;
                if(i%primes[j] == 0) break;
            }
        }
    }
}



青影
5个月前
java 没过 记录一下
import java.util.*;

class Main{
    static class Node{
        int value;
        int number;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while(T-- >0){
            int n = sc.nextInt();

            int[] a = new int[n];
            int[] b = new int[n];
            Node[] nodes = new Node[n];
            for(int i=0;i<n;i++){
                nodes[i] =new Node();
                a[i] =  sc.nextInt();
                //System.out.println(a[i]);
                nodes[i].value = a[i];
                nodes[i].number = i;
            }
            Arrays.sort(nodes,new Comparator<Node>(){
                public int compare(Node o1,Node o2){
                     if (o1.value!=o2.value){
                        return o1.value<o2.value?-1:1;
                    }
                    return 0;
                }
            });
            for(int i = 0;i < n;i++){
                Node c = nodes[i];
                int left = (c.number+n-1)%n;//左边的坐标
                int right = (c.number+1)%n;//右边的坐标

                int lv = 1; int rv = 1;  //左右两边的值线设置为1
                if(c.value>a[left])  lv = b[left]+1;
                if(c.value>a[right])  rv = b[right]+1;
                b[c.number] = Math.max(lv,rv);
            } 
            double res = 0;
            for(int i =0;i<n;i++){
                res+=b[i];
            }
            System.out.println((int)res);
        }


    }
}



青影
5个月前

java 代码

import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for(int i =0;i < n;i++){
            String s1 = sc.next();
            char[] s = s1.toCharArray();
            int k = 0;
            for(int j =0;j<s.length;j++){
                s[k++] = s[j];
                if(k>=3 && s[k-3]==s[k-2] &&s[k-2] == s[k-1]) k--;
                if(k>=4 && s[k-4]==s[k-3] &&s[k-2] == s[k-1]) k--;
            }

            System.out.println(new String(s).substring(0,k));
        }
    }
}



青影
5个月前

java 代码

import java.util.*;

class Main{

    static class Node {
        int value ;
        int number;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); //表示书架的排数
        int k = sc.nextInt(); //每排书架上书的数量
        int[][] arr =new int[n][k];

        int[] cnt = new int[n]; //存储第i行的逆序对数量
        //int[] index = new int[n];
        Node[] nodes = new Node[n]; // number 表示第几行,value 表示那一行的逆序对数量

        for(int i = 0; i < n;i++){
             nodes[i] =new Node();
            for(int j = 0;j < k;j++){   //先将每一组数据读入
                arr[i][j] = sc.nextInt();
            }
            for(int j = 0;j < k;j++){   //计算逆序对个数
                for(int m = j+1;m <k;m++){
                    if(arr[i][j]>arr[i][m]) cnt[i]++;
                }
            }
            nodes[i].value = cnt[i];
            nodes[i].number=i;
        }
        Arrays.sort(nodes, new Comparator<Node>() {  //排序采用稳定排序
            @Override
            public int compare(Node o1, Node o2) {
                if (o1.value!=o2.value){
                    return o1.value<o2.value?-1:1;
                }else {
                    return o1.number<o2.number?-1:1;
                }

            }
        });

        System.out.print("[");
        for(int i = 0;i < n;i++){
            System.out.print("[");
            for(int j =0;j < k;j++){
                System.out.print(arr[nodes[i].number][j]);
                if(j != k-1)  System.out.print(", ");
            }
            System.out.print("]");
            if(i != n-1)  System.out.print(", ");
        }
        System.out.print("]");
    }
}



青影
5个月前

java 代码 链表的方式

import java.util.*;

class Main{
    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<=n;i++)
            list.add(i);
        int c=(m-1)%n;  
        int i = 1; 
        while(list.size()!=1) {
            list.remove(c);
            i *= m;
            c=(c + m*i -1)%list.size();  
        }

     System.out.println(list.get(0));

    }

}



青影
5个月前

java 代码

import java.util.Scanner;
class Main{
   static String[] nums=new String[]{"zero","one","two","three","four","five","six","seven","eight","nine"};
   static String[] cnts=new String[]{"","","double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple","decuple"}; 

    static void print(String s1) {
        char[] s = s1.toCharArray();
        for (int i=0;i<s1.length();i++) {//遍历这个字符串
            int j=i+1;
            while (j<s1.length() && s[i]==s[j]) j++;//计算重复的数
            int len=j-i;
            if (len >=2 && len<=10) System.out.print(cnts[len]+" "+nums[s[i]-'0']+" ");
            else if (len>10) {
                for (int k=0;k<len;k++) {
                    System.out.print(nums[s[i]-'0']+" ");
                }
            }
            else System.out.print(nums[s[i]-'0']+" ");
            i=j-1;//更新i的位置
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();    //读入共有多少组测试数据
        for(int c = 1;c<=T;c++){  //对每组数据进行操作
            String s = sc.next();  //读入前面的字符串

            String seq1 = sc.nextLine(); //读入后面的
            String seq = seq1.trim();    //去掉前面的空格
            //System.out.println(seq);

            String[] e = seq.split("-");  //分离出每个数字
            int[] len = new int[e.length]; //将分离出的数字放入数组中,备用
            for(int i =0;i<len.length;i++){

               len[i] = Integer.parseInt(e[i]); //将string转为int存入数组
                //System.out.println(len[i]);
            }
            System.out.print("Case #" + c+":  ");

            int start = 0;
            for(int l : len){
                print(s.substring(start,start+l));//先对前几个数进行操作
                start+=l;
            }
            System.out.println();
        }
    }
}



青影
5个月前
import java.util.Scanner;
class Main{
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){ 

            int n = sc.nextInt();
            int m = sc.nextInt();
            int[][] dp = new int[101][11];
             for(int i = 1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    dp[i][j] = dp[i-1][j] + dp[i-1][j-1] +1;
                }
                if(dp[i][m]>=n){
                    System.out.println(i);
                    break;
                }
            }
        }
    }
}