字符串排序 、求int型正整数在内存中存储时1的个数 题目

HJ14 字符串排序

描述

给定 n 个字符串,请对 n 个字符串按照字典序排列。

数据范围: 1≤𝑛≤1000 1≤n≤1000 ,字符串长度满足 1≤𝑙𝑒𝑛≤100 1≤len≤100

输入描述:

输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。

输出描述:

数据输出n行,输出结果为按照字典序排列的字符串。

示例:

输入:9
	 cap
	 to
	 cat
	 card
	 two
	 too
	 up
	 boat
	 boot

输出:boat
	 boot
	 cap
	 card
	 cat
	 to
	 too
	 two
	 up

 

分析:

​ 1.使用Arrays.sort给字符串排序。

 

代码:

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num=sc.nextInt();
        if(num<1 ||num>1000){
            System.out.println("输入非法!");
            return;
        }
        String s;
        String[] str=new String[num];

        for (int i = 0; i < num; i++) {
            s=sc.next();
            if(s.length()>100){
                System.out.println("输入非法!");
                return;
            }
            for (int j = 0; j < s.length(); j++) {
                if(!(s.charAt(j)>=65 &&s.charAt(j)<=90 || s.charAt(j)>=97 &&s.charAt(j)<=122)){
                    System.out.println("输入非法!");
                    return;
                }
            }
            str[i]=s;
        }

        Arrays.sort(str);
        for (String element : str) {
            System.out.println(element);
        }

    }
}

 
 
 

大佬代码:

使用原理求解。

import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int num = Integer.parseInt(str);
        String[] words = new String[num];
        String temp = "";
        for(int i = 0; i < num; i++) {
            words[i] = br.readLine();
        }
        for(int j = 0; j < num; j++) {
            for(int k = j + 1; k < num; k++) {
                if(isSwap(words[j], words[k])) {
                    temp = words[j];
                    words[j] = words[k];
                    words[k] = temp;
                }
            }
            System.out.println(words[j]);
        }
    }
 
    public static boolean isSwap(String word1, String word2) {
         
        for (int i = 0; i < Math.min(word1.length(), word2.length()); i++) {
            if (word1.charAt(i) - word2.charAt(i) > 0) {
               return true;
            }else if(word1.charAt(i) - word2.charAt(i) < 0) {
                return false;
            }
        }
        if(word1.length() <= word2.length()) {
            return false;
        }else {
            return true;
        }
    }
}

 
 

 

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

描述

输入一个 int 型的正整数,计算出该 int 型数据在内存中存储时 1 的个数。

数据范围:保证在 32 位整型数字范围内

输入描述:

输入一个整数(int类型)

输出描述:

这个数转换成2进制后,输出1的个数

示例:

输入:5
输出:2
输入:0
输出:0

 

分析:

​ 1.把整数转成二进制字符串。

​ 2.再使用计数思想计数。

 

代码:

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String s=Integer.toBinaryString(num);
        int count=0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '1') {
                count++;
            }
        }
        System.out.println(count);
    }
}

 
 
 

大佬代码:

使用无符号右移。

import java.util.Scanner;
 
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();    //读取数字
        int n = 0;    //计数变量
        for(int i=0; i < 32; i++){
            if((num&1) == 1)    //如果末位为1则计数
                n++;
            num = num >>> 1;    //无符号右移
        }
        System.out.println(n);
    }
}

 
 
 

使用replaceAll方法把1换成空字符。

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String str = Integer.toBinaryString(num);
        int length = str.length();
        String newStr = str.replaceAll("1", "");
        System.out.println(length - newStr.length());
    }
}

 
 
 

使用原理来求解。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            int num = in.nextInt();
            int result=0;
            while(true){
                if(num==0||num<0){
                    break;
                }
                if(num==1){
                    result++;
                    break;
                }
                 
                if(num%2==1)
                {
                    result++;
                    num=(num-1)/2;
                }else{
                    num=(num)/2;
                }
            }
             
             
            System.out.println(result);
         
    }
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值