题目
- HJ14 字符串排序
- 描述
- 输入描述:
- 输出描述:
- 示例:
- 分析:
- 代码:
- 大佬代码:
- HJ15 求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给字符串排序。
代码:
java">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);
}
}
}
大佬代码:
使用原理求解。
java">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.再使用计数思想计数。
代码:
java">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);
}
}
大佬代码:
使用无符号右移。
java">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换成空字符。
java">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());
}
}
使用原理来求解。
java">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);
}
}