面试机试题

1.字符串最后一个单词的长度

import java.util.Scanner;
//计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。
// (注:字符串末尾不以空格为结尾)
public class t1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String str = scan.nextLine();
            System.out.println(Length(str));
        }

    }

    public static int Length(String str) {
       String[] s1 = str.split(" ");
       return s1[s1.length-1].length();
    }
}

2.计算某字符出现次数

import java.util.Scanner;
public class t2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String one = scan.nextLine().toLowerCase();
        String two = scan.nextLine().toLowerCase();
        int count = 0;
        for (int i = 0; i < one.length(); i++) {
            char c = one.charAt(i);
            Boolean b = two.equalsIgnoreCase(String.valueOf(one.charAt(i)));
            if (b) {
                count++;
            }
        }
        System.out.println(count);
    }
}

3. 明明的随机数

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class t3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //获取个数
        while (scanner.hasNext()) {
            int num = scanner.nextInt();
            TreeSet set = new TreeSet();
            //输入
            for (int i = 0; i < num; i++) {
                set.add(scanner.nextInt());
            }
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }
    }
}

4.字符串分隔

mport java.util.Scanner;
//连续输入字符串,请按长度为8拆分每个输入字符串并进行输出;
//•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
public class t4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            while (str.length() > 8) {
                System.out.println(str.substring(0, 8));
                str = str.substring(8);
            }
            while (str.length() < 8 && str.length() > 0) {
                str +=0;
            }
            System.out.println(str);
        }
    }
}

5.进制转换

import java.util.Scanner;
//写出一个程序,接受一个十六进制的数,输出该数值的十进制表示
public class t5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.nextLine();
            str = str.substring(2,str.length());
            System.out.println(Integer.parseInt(str,16));
        }
    }
}

6. 质数因子

import java.util.Scanner;
//输入一个正整数,按照从小到大的顺序输出它的所有质因子
public class t6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long num = scanner.nextLong();
        for (long i = 2; i <= num; ++i) {
            while (num % i == 0) {
                System.out.print(i + " ");
                num /= i;
            }
        }
        System.out.println();
    }
}

7.取近似值

import java.util.Scanner;
public class t7 {
    public static void main(String[] args) {
        Scanner scanner =  new Scanner(System.in);
        double num = scanner.nextDouble();
        System.out.println((int)(num + 0.5));
    }
}

8.合并表记录

mport java.util.Scanner;
import java.util.TreeMap;
//合并表记录
public class t8 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TreeMap<Integer, Integer> map = new TreeMap<>();
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                int a = scanner.nextInt();
                int b = scanner.nextInt();
                map.put(a, map.getOrDefault(a, 0) + b);

            }
        }
        for (Integer i : map.keySet()) {
            System.out.println(i + " " + map.get(i));
        }
    }
}

9.提取不重复的整数

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

// 提取不重复的整数
public class t9 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            //判断是否是不重复的
            HashSet<Integer> hs = new HashSet<>();
            int target = scanner.nextInt();
            while (target != 0) {
                int temp = target % 10;
                if (hs.add(temp)) {
                    System.out.print(temp);
                }
                target /= 10;
            }
            System.out.println();
        }
    }
}

10字符个数统计

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

//字符个数统计
public class t10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashSet<Integer> hs = new HashSet<>();
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            char[] chars = str.toCharArray();
            for (char c : chars) {
                int i = c;
                if (i > 0 && i < 127) {
                    hs.add(i);
                }
            }
            System.out.println(hs.size());
        }
    }
}

11数字颠倒

import java.util.Scanner;
//数字颠倒 输入一个整数,将这个整数以字符串的形式逆序输出
public class t11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        for(int i =str.length()-1;i>=0;i--){
            System.out.println(str.charAt(i));
        }
        StringBuffer strb = new StringBuffer(str);
        strb.reverse();
        System.out.println(strb.toString());
    }
}

12字符串反转

import java.util.Scanner;
public class t12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        for(int i =str.length()-1;i>=0;i--){
            System.out.print(str.charAt(i));
        }
    }
}

13句子逆序

import java.util.Scanner;

//句子逆序 “I am a boy”,逆序排放后为“boy a am I”
public class t13 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] st = str.split(" ");
        for (int i = st.length - 1; i >= 0; i--) {
            if (i != 0) {
                System.out.print(st[i] + " ");
            } else {
                System.out.print(st[i]);
            }
        }
    }
}

14字符串排序

import java.util.Arrays;
import java.util.Scanner;

public class t14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int length = Integer.valueOf(scanner.nextLine());
            String[] strs = new String[length];
            for (int i = 0; i < strs.length; i++) {
                strs[i] = scanner.nextLine();
            }
            Arrays.sort(strs);
            for (String v : strs) {
                System.out.println(v);
            }
        }
    }
}

15求int型正整数在内存中存储时1的个数

import java.util.Scanner;
public class t15 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int result = 0;
        while (a > 0) {
            if (1 == a % 2) result++;
            a = a >>> 1;
        }
        System.out.println(result);
    }
}

16 购物单

17 坐标移动

18密码验证合格程序

19 简单密码

import java.util.*;

public class Main{
    //定义map容器存储按键对应数字字符的容器
    private static Map<String,String> map = new HashMap<>();
    //静态初始化、加载map容器
    static{
        map.put("1","1");
        map.put("abc","2");
        map.put("def","3");
        map.put("ghi","4");
        map.put("jkl","5");
        map.put("mno","6");
        map.put("pqrs","7");
        map.put("tuv","8");
        map.put("wxyz","9");
        map.put("0","0");
    }
    
    public static void main(String[] args){
       Scanner scanner = new Scanner(System.in);
       while(scanner.hasNext()){
           String str = scanner.nextLine();
           char[] chars = str.toCharArray();
           //构造buffer容器存储转换后的字符串
           StringBuffer buffer = new StringBuffer();
           for(char c : chars){
               //如果是正整数则不需要进行加密
               if(c>='0'&& c<='9'){
                    buffer.append(String.valueOf(c));
                }else if(c>='A'&& c<='Y'){ //如果是A~Y的大写字母则需要将其+32位转换成小写再向后移1位
                    char newChar = (char)(c+32+1);
                    buffer.append(String.valueOf(newChar));
                }else if(c=='Z'){ //如果是Z则加密成a
                    buffer.append("a");
                }else{
                //去除map容器中的key与字符进行校验并加密
                Set<String> keys = map.keySet();
                for(String k : keys){
                    if(k.contains(String.valueOf(c)))
                        buffer.append(map.get(k));
                }
               }
           }
           System.out.print(buffer.toString());
       }
    }
}

20汽水瓶

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int a = scanner.nextInt();
            if (a != 0) {
                System.out.println(a / 2);
            }
        }
    }
///第二种
import java.util.*;
public class Main{
       public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) { //第一层while循环,实现用户多次输入的需求
            int num = scanner.nextInt();
            if (num == 0) { //如果输入0,则直接终止输入
                break;
            }
            int j = 0; //和汽水的数量
            while (num >= 3) { //表示当前小明手里的空气水瓶数大于等于3的时候,则可以继续兑换(和老板借一瓶是特殊情况,最后处理)
                j += num / 3; //累计喝汽水的数量
                num = num / 3 + num % 3; //兑换之后,还剩的空汽水瓶
            }
            j = num == 2 ? j+1 :j; //如果最后还有2个空气水瓶,则进行向老板借一瓶,即还能再喝一瓶
            System.out.println(j);
        }
    }
}	

21删除字符串中出现次数最少的字符

import java.util.*;

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

        char[] arr = s.toCharArray();
        int[] charCount = new int[26];
        //先统计每个字符出现的个数
        for(char c : arr)
            charCount[c - 'a']++;

        //找出出镜率最低的字符出现的次数
        Integer minCount = Integer.MAX_VALUE;
        for(int i : charCount){
            //注意,没出现过的字符不算
            if(i > 0) minCount = Math.min(minCount, i);
        }


        StringBuffer sb= new StringBuffer(20);
        for(char c : arr){
            if(charCount[c - 'a'] != minCount)
                sb.append(c);
        }
        System.out.println(sb);
    }
}

22字符串排序

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
 * @Description 字符串排序
 * @Author haixiaofei
 * @Date 2022/2/15 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNextLine()){
            String str = input.nextLine();
            str = sort(str);
            System.out.println(str);
        }
    }

    public static String sort(String str) {
        List<Character> characters = new ArrayList<>();
        for (char ch : str.toCharArray()){
            if (Character.isLetter(ch)) {
                characters.add(ch);
            }
        }
        //将英文字母先排序好
        characters.sort(Comparator.comparingInt(Character::toLowerCase));
        //若是非英文字母则直接添加
        StringBuilder result = new StringBuilder();
        for (int i = 0, j = 0; i < str.length(); i++) {
            if (Character.isLetter(str.charAt(i))) {
                result.append(characters.get(j++));
            }
            else {
                result.append(str.charAt(i));
            }
        }
        return result.toString();
    }
}

23查找兄弟单词

24单词倒排

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] splitStr = scanner.nextLine().split("[^a-zA-Z]"); //非字母都用作分隔符
        StringBuilder str = new StringBuilder();
        for (int i = splitStr.length - 1; i >= 0; i--) {
            str.append(splitStr[i]);
            if(i != 0) {
                str.append(" ");
            }
        }
        System.out.println(str.toString());
    }

25图片整理

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.nextLine();
            char[] charArr = str.toCharArray();
            Arrays.sort(charArr);
            System.out.println(String.valueOf(charArr));
        }
    }
}

26蛇形矩阵

import java.util.Scanner;

public class t17 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n =  scanner.nextInt();
            for (int i  =1;i<=n;i++){
                for (int j = 1;j<=n+1-i;j++){
                    int m = (i+j-1)*(i+j)/2-(i-1);
                    System.out.println(m +" ");
                }
                System.out.println();
            }
        }
    }
}

27统计每个月兔子的总数

import java.util.Scanner;

public class t18 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int curMonth = scanner.nextInt();
            int sums = sumRabbit(curMonth);
            System.out.println(sums);
        }
    }

    static int sumRabbit(int curMonth) {
        if (curMonth <= 2) {
            return 1;
        }
        int fir = 1;
        int sec = 1;
        int count = 0;
        while (curMonth - 2 > 0) {
            count = fir + sec;
            fir = sec;
            sec = count;
            curMonth--;
        }
        return count;
    }
}

28统计字符

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str =  scanner.nextLine();
            int letternums = 0,blankNums = 0,digiNums = 0,otherNums = 0;
            for (int i = 0 ;i<str.length();i++){
                char chars = str.charAt(i);
                if((chars>='A'&& chars<='Z')||(chars>='a'&&chars<='z')){
                    letternums++;
                }else if(chars == ' '){
                    blankNums++;
                }else if(chars>='0'&&chars<='9' ){
                    digiNums++;
                }else {
                    otherNums++;
                }
            }
            System.out.println(letternums);
            System.out.println(blankNums);
            System.out.println(digiNums);
            System.out.println(otherNums);
            
        }
    }
}

29 输出单向链表中倒数第k个结点

//注意加上k=0的判断
import java.util.*;

public class Main {
    public static void insertNode(int value, Node head) {
        Node temp = head;
        Node newNode = new Node(value);
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int N = sc.nextInt(); // 链表结点个数
            Node head = new Node(sc.nextInt()); // 构造头结点
            // 构造单链表
            for (int i = 1; i < N; i++) {
                int value = sc.nextInt(); // 新结点的value
                //int prevValue = sc.nextInt(); // 该结点的前一个结点的value
                insertNode(value, head);
            }
            //removeNode(sc.nextInt(), head);
            Node output = head;
            int k = sc.nextInt();
            int i = 0;
            
            if(k==0){
                System.out.println(0);
            }else{
                while (i < N - k) {
                    i += 1;
                    output = output.next;
                }
                System.out.println(output.value);
            }
        }
    }
}

class Node {
    // field
    public int value;
    public Node next;

    // 构造函数
    public Node(int value) {
        this.value = value;
    }
}

30截取字符串

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str1 = scanner.nextLine();
            String str2 = scanner.nextLine();
            str1 = str1.substring(0, Integer.parseInt(str2));
            System.out.println(str1);
        }
    }
}

31杨辉三角的变形

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==1 || n==2){
                System.out.println(-1);
            }
            else if(n%4==3 || n%4==1){
                System.out.println(2);
            }
             else if(n%4==0){
                 System.out.println(3);
            }
             else if(n%4==2){
                 System.out.println(4);
            }
        }
    }
}

32完全数计算

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        
        while(in.hasNextInt()){
            int n = in.nextInt();
            int res = 0;
            //一个for循环只能判断一个数是否满足条件
            //所以需要两层for循环
            for(int i = 1;i <= n;i++){
                int temp = 0;
                for(int j = 1;j < i;j++){
                    if(i % j == 0){
                        //j为因数,所以加j
                        temp += j;
                    }
                }
                if(temp == i){
                    res++;
                }
            }
            System.out.println(res);
        }
        
    }

}

33输入n个整数,输出其中最小的k个


import java.util.Arrays;
import java.util.Scanner;

public class t23 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[] arr = new int[n];
            for(int i =0;i<n;i++){
                arr[i]= scanner.nextInt();
            }
            Arrays.sort(arr);
            for(int j = 0;j<k-1;j++){
                System.out.print(arr[j] + " ");
            }
           System.out.println(arr[k-1]);
        }
    }


}

34 查找组成一个偶数最接近的两个素数

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();
            for(int i=n/2;i>=2;i--){
                if(isPrime(i)&&isPrime(n-i)){
                    System.out.println(i);
                    System.out.println(n-i);
                    break;
                }
            }
        }
    }

    public static boolean isPrime(int n){
        for(int i=2;i<n;i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
}

35放苹果


import java.util.Scanner;

public class t24 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int m = in.nextInt();
            int n = in.nextInt();
            System.out.println(count(m, n));
        }
        in.close();
    }

    public static int count(int m, int n) {

        if (m < 0)
            return 0;
        if (m == 0 || n == 1)
            return 1;
        return count(m, n - 1) + count(m - n, n);
    }

}

36查找输入整数二进制中1的个数

import java.util.Scanner;

public class t25 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int count = 0;
            String str = Integer.toBinaryString(n);
            char[] chars = str.toCharArray();
            for (char cha : chars) {
                if (cha == '1') {
                    `在这里插入代码片`count++;
                }
            }
            System.out.println(count);
        }
    }
}

37百钱买百鸡问题

public class t26 {
    public static void main(String[] args) {
        for (int x =0;x<=20;x++){
            for (int y =0;y<=33;y++){
                int z =  100-x-y;
                if((z%3==0) && (5*x+3*y+z/3== 100 )&& (x+y+z ==100)){
                    System.out.println(x + " " + y + " " + z +" ");
                }
            }
        }
    }
}

38计算日期到天数转换

import java.util.Scanner;

public class t27 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int year = scanner.nextInt();
            int month =  scanner.nextInt();
            int day = scanner.nextInt();
            int days = 0;
            int[] monthDays = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
            for (int i = 0;i<month-1;i++){
                days = days+ monthDays[i];
            }
            days += day;
            if(year%4 == 0 && year%100!=0 || year%400 == 0){
                days++;
            }
            System.out.println(days);
        }
    }
}

39尼科彻斯定理

import java.util.Scanner;

public class t28 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int m = scanner.nextInt();
            int firstNum =  m*(m-1)+1;
            System.out.print(firstNum);
            for (int i = 1;i<m;i++){
                int sum =  firstNum + 2*i;
                System.out.print( "+"+sum );
            }
            System.out.println();
        }
    }
}

40整型数组合并

package Test;

import java.util.Scanner;
import java.util.TreeSet;

public class t29 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int m = scanner.nextInt();
            int[] arrm = new int[m];
            TreeSet<Integer> setnum = new TreeSet<>();
            for (int i = 0; i < m; i++) {
                arrm[i] = scanner.nextInt();
                setnum.add(arrm[i]);
            }

            int n = scanner.nextInt();
            int[] arrn = new int[n];
            for (int i = 0; i < n; i++) {
                arrn[i] = scanner.nextInt();
                setnum.add(arrn[i]);
            }

            for (Integer i : setnum) {
                System.out.print(i);
            }
            System.out.println();

        }
    }
}

41字符串字符匹配

package Test;

import java.util.Scanner;

public class t30 {
    public static void main(String[] args) {
        Scanner scanner =  new Scanner(System.in);
        while (scanner.hasNext()){
            String first = scanner.nextLine();
            String second = scanner.nextLine();
            String[] arr = first.split("");
            int count = 0;
            
            for (int i = 0;i<first.length();i++){
               if(second.contains(arr[i])){
                   count++;
               }
            }

            if(count==second.length()){
                System.out.println(true);
            }else {
                System.out.println(false);
            }

        }

    }
}

42二维数组操作

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int m = scanner.nextInt();
            int n = scanner.nextInt();
            int x1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int y2 = scanner.nextInt();
            int x3 = scanner.nextInt();
            int y3 = scanner.nextInt();
            int x4 = scanner.nextInt();
            int y4 = scanner.nextInt();

            if(m<=9 && n<=9){
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            
            if((x1<m && x2 <n)&&(y1<m && y2 <n) ){
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            
            if(x3<m && m<9){
                System.out.println(0);
            }else {
                System.out.println(-1);
            }

            if(y3<n && n<9){
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            
            if(x4<m && y4< n){
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
        }
    }
}

43统计大写字母个数

import java.util.Scanner;
public class Main {
    public static void main(String[] arg ){       
        Scanner scanner =  new Scanner(System.in);
        while(scanner.hasNextLine()){
            String input = scanner.nextLine();
            String input2 = input.replaceAll("([A-Z])","");
            System.out.println(input.length()-input2.length());
        }
    }       
}

44最长回文子串

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            String s = in.nextLine();
            int max = 0;     // 最长回文子串的长度
            for(int i=0;i<s.length();i++){
            	// 从两头开始匹配
                for(int j=s.length();j>i;j--){   
                	// 两头的字符相等,即开始判断是否为回文串          
                    if(s.charAt(i) == s.charAt(j-1)){ 
                    	 // 截取待判断的子串str,将str分为两段,相等即为回文子串     
                        String str = s.substring(i,j);    
                        int n = str.length();
                         // 前半段
                        String s1 = str.substring(0,n/2); 
                        // 后半段,需要分为奇数长度和偶数长度
                        StringBuffer s2;  
                        // 偶数长度:直接截取剩下的后半部分    
                        // 奇数长度:不判断中间字符,越过中间字符截取剩下的后半部分             
                        if(str.length()%2 == 0){           
                            s2 = new StringBuffer(str.substring(n/2));
                        }else{                             
                            s2 = new StringBuffer(str.substring(n/2+1));
                        }
                        // 翻转后半段的字符串
                        String t = String.valueOf(s2.reverse()); 
                         // 翻转后的字符串与前半段相等即为回文串
                        if(t.equals(s1)){    
                        // 取长度的最大值                   
                            max = n>max ? n:max;           
                        }
                    }
                }
            }
            System.out.println(max);
        }
    }
}

45求最大连续bit数

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = Integer.toBinaryString(scanner.nextInt());
            String[] arr =  str.split("0");
            int aleng = 0;
            for (int i =0;i<arr.length;i++) {
                if (arr[i].length() > aleng) {
                    aleng = arr[i].length();
                }
            }
            System.out.println(aleng);
        }
    }
}

46等差数列

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scannner =  new Scanner(System.in);
        while(scannner.hasNext()){
            int n = scannner.nextInt();
            int firstNum =2;
            int lastNum = 3*n-1;
            int sum = n*(firstNum+lastNum)/2    ;
            System.out.println(sum);     
        }
        
    }
    
}

47字符统计

import java.util.*;

public class Main {
    public static void main(String [] args){
        Scanner sc=new Scanner(System.in);
        StringBuilder sb=new StringBuilder();
         //使用HashMap来存储字符和字符在字符串中出现的次数,字符作为键,次数作为值
        Map<Character,Integer> map=new HashMap<Character,Integer>();
        while(sc.hasNext()){
            String s=sc.nextLine();
            //调用String类的toCharArray()方法将输入的字符串转换为字符数组
            char [] charArray=s.toCharArray();
           //遍历字符数组,利用HashMap的特性存入键值,这是实现该算法的核心代码
            for(char c:charArray){
                if(map.containsKey(c)){
                    map.put(c,map.get(c)+1);
                }else{
                    map.put(c,1);
                }
            }
        }
        //获取键值对并转换为List进行操作
        List<Map.Entry<Character,Integer>> list=new ArrayList<>(map.entrySet());
        //定义比较器进行排序规则的编辑
        Collections.sort(list,new Comparator<Map.Entry<Character,Integer>>(){
            
            public int compare(Map.Entry<Character,Integer> me1,Map.Entry<Character,Integer> me2){
                return me2.getValue()-me1.getValue()==0?me1.getKey()-me2.getKey():me2.getValue()-me1.getValue();
            }
        });
        //遍历完成排序的集合,并进行拼接
        for(Map.Entry<Character,Integer> me:list){
            sb.append(me.getKey());
        }
        sb.append("\n");
        System.out.println(sb);
    }
}

48字符逆序

import java.util.*;

public class Main {

    private String reverse(String str) {
        StringBuilder res = new StringBuilder(str);
        return res.reverse().toString();
    }

    public Main() {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            String res = reverse(str);
            System.out.println(res);
        }
   }

    public static void main(String[] args) 
    {
        Main solution = new Main();
    } 
}   

49求最小公倍数

import java.util.Scanner;

/**
 * HJ108 求最小公倍数 - 简单
 */
public class HJ108 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n1 = sc.nextInt();
            int n2 = sc.nextInt();
            for(int i = 1; i <= n1*n2;i++){
                if(i % n1 == 0 && i % n2 == 0){
                    System.out.println(i);
                    break;
                }
            }
        }
        sc.close();
    }
}

50 记票统计

//照它的逻辑写就行
import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=null;
        while((s=br.readLine())!=null){
            int n=Integer.parseInt(s);
            String[] str=br.readLine().split(" ");
            int voteNums=Integer.parseInt(br.readLine());
            String[] voteStr=br.readLine().split(" ");
            int a[]=new int[n];
            int inValid=0;
            for(int i=0;i<voteNums;i++){
                int flag=0;
                for(int j=0;j<n;j++){
                    if(voteStr[i].equals(str[j])){
                        flag=1; 
                        a[j]++;
                        break;
                    } 
                }
                if(flag==0) inValid++;
            }
            for(int i=0;i<n;i++){
                System.out.println(str[i]+" : "+a[i]);
            }
            System.out.println("Invalid : "+inValid);
        }
    }
}

52自守数

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();
                int count = 0;
                for(int i=0;i<=n;i++){
                    int sum =i*i;
                    String s1 = String.valueOf(i);
                    String s2 = String.valueOf(sum);
                    //从平方的尾部往前截取到与当前数长度相同的子串
                    if(s2.substring(s2.length()-s1.length()).equals(s1)){
                        count++;
                    }
                }
                System.out.println(count);
            }
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值