题号 | 题目 | 知识点 | 难度 | 通过率 | |
---|---|---|---|---|---|
HJ1 | 字符串最后一个单词的长度 | 字符串 | 简单 | 32.49% | |
HJ2 | 计算某字符出现次数 | 字符串 哈希 | 简单 | 30.43% | |
HJ4 | 字符串分隔 | 字符串 | 简单 | 28.60% | |
HJ5 | 进制转换 | 字符串 | 简单 | 33.28% | |
HJ6 | 质数因子 | 排序 | 简单 | 27.01% | |
HJ8 | 合并表记录 | 简单 | 35.02% | ||
HJ10 | 字符个数统计 | 字符串 哈希 | 简单 | 47.31% | |
HJ11 | 数字颠倒 | 字符串 | 简单 | 58.51% | |
HJ12 | 字符串反转 | 字符串 | 简单 | 59.14% | |
HJ13 | 句子逆序 | 数组 | 简单 | 40.02% | |
HJ14 | 字符串排序 | 字符串 排序 | 简单 | 41.54% | |
HJ15 | 求int型正整数在内存中存储时1的个数 | 位运算 | 简单 | 57.06% | |
HJ21 | 简单密码 | 字符串 模拟 | 简单 | 41.47% | |
HJ22 | 汽水瓶 | 模拟 | 简单 | 28.19% | |
HJ23 | 删除字符串中出现次数最少的字符 | 字符串 哈希 | 简单 | 33.23% | |
HJ31 | 单词倒排 | 字符串 排序 | 简单 | 24.03% | |
HJ34 | 图片整理 | 字符串 排序 | 简单 | 46.95% | |
HJ35 | 蛇形矩阵 | 数组 | 简单 | 43.86% | |
HJ37 | 统计每个月兔子的总数 | 排序 | 简单 | 43.87% | |
HJ40 | 统计字符 | 字符串 哈希 | 简单 | 44.43% | |
HJ51 | 输出单向链表中倒数第k个结点 | 链表 双指针 | 简单 | 26.49% | |
HJ53 | 杨辉三角的变形 | 基础数学 | 简单 | 48.85% | |
HJ54 | 表达式求值 | 字符串 栈 基础数学 | 简单 | 56.06% | |
HJ56 | 完全数计算 | 基础数学 | 简单 | 50.33% | |
HJ60 | 查找组成一个偶数最接近的两个素数 | 穷举 基础数学 | 简单 | 44.77% | |
HJ61 | 放苹果 | 递归 动态规划 | 简单 | 47.80% | |
HJ62 | 查找输入整数二进制中1的个数 | 位运算 | 简单 | 50.24% | |
HJ72 | 百钱买百鸡问题 | 简单 | 50.25% | ||
HJ73 | 计算日期到天数转换 | 字符串 | 简单 | 42.46% | |
HJ76 | 尼科彻斯定理 | 基础数学 | 简单 | 46.29% | |
HJ80 | 整型数组合并 | 数组 哈希 排序 | 简单 | 34.02% | |
HJ81 | 字符串字符匹配 | 字符串 哈希 | 简单 | 31.83% | |
HJ83 | 二维数组操作 | 数组 | 简单 | 30.56% | |
HJ84 | 统计大写字母个数 | 字符串 | 简单 | 43.80% | |
HJ85 | 最长回文子串 | 字符串 穷举 | 简单 | 40.91% | |
HJ86 | 求最大连续bit数 | 位运算 | 简单 | 45.56% | |
HJ87 | 密码强度等级 | 字符串 模拟 | 简单 | 32.74% | |
HJ91 | 走方格的方案数 | 动态规划 基础数学 | 简单 | 47.52% | |
HJ94 | 记票统计 | 哈希 | 简单 | 29.77% | |
HJ96 | 表示数字 | 字符串 模拟 | 简单 | 33.74% | |
HJ97 | 记负均正 | 数组 | 简单 | 22.04% | |
HJ99 | 自守数 | 简单 | 39.10% | ||
HJ100 | 等差数列 | 基础数学 | 简单 | 40.99% | |
HJ102 | 字符统计 | 字符串 哈希 排序 | 简单 | 31.74% | |
HJ106 | 字符逆序 | 字符串 | 简单 | 45.10% | |
HJ108 | 求最小公倍数 | 递归 基础数学 | 简单 | 37.30% |
1.字符串最后一个单词的长度
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] s = str.split(" ");
int length = s[s.length - 1].length();
System.out.println(length);
}
}
2.计算某字符出现次数
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input1= s.nextLine();
String input2 = s.nextLine();
String res = input1.toLowerCase().replaceAll(input2.toLowerCase(), "");
System.out.println(input1.length() - res.length());
}
}
3.字符串分隔
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();//牢记字符串缓冲区的建立语法
sb.append(str);//字符串缓冲区的加入
int addZero = 8 - str.length() % 8;//addzero的可能值包括8
while((addZero > 0)&&(addZero<8)){//注意边界调节,避免addzero=8
sb.append("0");//使用‘’或“”都可
addZero--;
}
String str1 = sb.toString();
while(str1.length()>0){
System.out.println(str1.substring(0,8));
str1 = str1.substring(8);
}
}
}
}
4.进制转换
public class Main {
private final static int BASE = 16;
private static Map<Character, Integer> map = new HashMap<Character, Integer>()
{{
put('0', 0);
put('1', 1);
put('2', 2);
put('3', 3);
put('4', 4);
put('5', 5);
put('6', 6);
put('7', 7);
put('8', 8);
put('9', 9);
put('A', 10);
put('B', 11);
put('C', 12);
put('D', 13);
put('E', 14);
put('F', 15);
put('a', 10);
put('b', 11);
put('c', 12);
put('d', 13);
put('e', 14);
put('f', 15);
}};
public static int getDecimal(String number) {
int res = 0;
for (char ch : number.toCharArray()) {
res = res * BASE + map.get(ch);
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String number = in.next();
int res = getDecimal(number.substring(2));
System.out.println(res);
}
}
}
5.质数因子
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long num = scanner.nextLong();
long k = (long) Math.sqrt(num);
for (long i = 2; i <= k; ++i) {
while (num % i == 0) {
System.out.print(i + " ");
num /= i;
}
}
System.out.println(num == 1 ? "" : num + " ");
}
}
6.合并表记录
public class Main{
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
TreeMap<Integer, Integer> map = new TreeMap<>(); // 输出结果要求有序!
while(sc.hasNextInt()){
int n = sc.nextInt();
for(int i = 0; i < n; ++i){
int a = sc.nextInt();
int b = sc.nextInt();
map.put(a,map.getOrDefault(a,0) + b);
}
}
for (Integer i : map.keySet()) {
System.out.println(i + " " + map.get(i));
}
}
}
7.字符个数统计
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.next();
HashSet<Character> hs=new HashSet<>();
for(int i=0;i<str.length();i++)
hs.add(str.charAt(i));
System.out.println(hs.size());
}
}
8.数字颠倒
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = String.valueOf(in.nextInt());
StringBuffer strb = new StringBuffer(str);
strb.reverse();
System.out.println(strb.toString());
}
}
9.字符串反转
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
StringBuilder strb = new StringBuilder(str);
strb.reverse();
System.out.println(strb.toString());
}
}
10.求int型正整数在内存中存储时1的个数
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) {
n++;
}
num = num >>> 1;
}
System.out.println(n);
}
}
11.简单密码
public class Main {
private static Map<String, String> map = new HashMap<String, String>() {
{
put("a", "2");
put("b", "2");
put("c", "2");
put("d", "3");
put("e", "3");
put("f", "3");
put("g", "4");
put("h", "4");
put("i", "4");
put("j", "5");
put("k", "5");
put("l", "5");
put("m", "6");
put("n", "6");
put("o", "6");
put("p", "7");
put("q", "7");
put("r", "7");
put("s", "7");
put("t", "8");
put("u", "8");
put("v", "8");
put("w", "9");
put("x", "9");
put("y", "9");
put("z", "9");
}
};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 'A' && arr[i] <= 'Z') {
if (arr[i] == 'Z') {
arr[i] = 'a';
} else {
int c = (int) String.valueOf(arr[i]).toLowerCase().charAt(0);
arr[i] = (char) ++c;
}
} else if (arr[i] >= 'a' && arr[i] <= 'z') {
arr[i] = map.get(String.valueOf(arr[i])).charAt(0);
}
}
System.out.println(String.valueOf(arr));
}
}
12.汽水瓶
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int bottle = sc.nextInt();
if(bottle==0){
break;
}
System.out.println(bottle/2);
}
}
}
13.删除字符串中出现次数最少的字符
public class Main {
public String delete(String str) {
// Map记录每个字母的次数
Map<Character, Integer> map = new HashMap<>();
for (char ch : str.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
// 快速找出最少次数
int min = Integer.MAX_VALUE;
for (int times : map.values()) {
min = Math.min(min, times);
}
StringBuilder res = new StringBuilder();
for (char ch : str.toCharArray()) {
if (map.get(ch) != min) {
res.append(ch);
}
}
return res.toString();
}
public static void main(String[] args) {
Main solution = new Main();
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = in.nextLine();
String res = solution.delete(str);
System.out.println(res);
}
}
}
14.单词倒排
public class Main {
public static String reverse(String str) {
// 匹配非字母的字符进行分割
String[] words = str.split("[^A-Za-z]");
StringBuilder result = new StringBuilder();
// 逆序添加分割完的单词
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]).append(" ");
}
return result.toString().trim();
}
public static void main(String[] args) {
Main solution = new Main();
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = in.nextLine();
String res = reverse(str);
System.out.println(res);
}
}
}
15.图片整理
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
char[] chars = str.toCharArray();
Arrays.sort(chars);
StringBuilder resultStr = new StringBuilder();
for (char aChar : chars) {
resultStr.append(aChar);
}
System.out.println(resultStr);
}
}
}
16.蛇形矩阵
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextInt()){
int n = in.nextInt(); //读入正整数n
int y = 1;
int yCnt = 1;
for(int i=1; i<=n; i++) {
int x = y;
int xCnt = i + 1;
for (int j = 1; j<=n-i+1; j++) {
System.out.print(x+" ");//循环输出x
x += xCnt++; //等差数列每次+1
}
System.out.println("");//换行
y+=yCnt; //等差数列每次加1
yCnt++;
}
}
}
}
17.统计每个月兔子的总数
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
System.out.println(solution(scanner.nextInt()));
}
}
private static int solution(int month) {
// 第一个月初始化
// 一月龄兔子总数
int oneMonth = 1;
// 二月龄兔子总数
int twoMonth = 0;
// 三月龄及以上兔子总数
int threeMonth = 0;
// 下个月将繁殖的兔子数量
int addVal = 0;
// 第二个月开始递推, i表示第i个月
for(int i = 2; i <= month; i++) {
// 三月龄及以上兔子总数 = 二月龄兔子总数 + 原本三月龄及以上兔子总数
threeMonth += twoMonth;
// 二月龄兔子总数 = 上个月的一月龄兔子总数
twoMonth = oneMonth;
// 一月龄(即这个月出生)兔子总数 = 上个月将繁殖的兔子数量
oneMonth = addVal;
// 下个月将出生的兔子 = 下个月成为三月龄及以上的兔子数量
addVal = twoMonth + threeMonth;
}
return (oneMonth + twoMonth + threeMonth);
}
}
18.统计字符
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
while(in.hasNext()){
String str=in.nextLine();
String s1=str.replaceAll("[A-Z]+|[a-z]+", "");
System.out.println(str.length()-s1.length());
String s2=s1.replaceAll(" ", "");
System.out.println(s1.length()-s2.length());
String s3=s2.replaceAll("[0-9]+", "");
System.out.println(s2.length()-s3.length());
System.out.println(s3.length());
}
}
}
19.输出单向链表中倒数第k个结点
public class Main {
static class ListNode {
int val;
ListNode next;
public ListNode(int n) {
this.val = n;
this.next = null;
}
}
public static ListNode FindKthToTail(ListNode pListHead, int k) {
if (pListHead == null || k <= 0)
return null;
ListNode target = pListHead;
ListNode temp = pListHead;
for (int i = 0; i < k; i++) {
temp = temp.next;
}
while (temp != null) {
target = target.next;
temp = temp.next;
}
temp = null;
target.next = null;
return target;
}
public static void main(String[] args) {
int n = 0;
Scanner scan = new Scanner(System.in);
// System.out.println("请输入节点个数: ");
while (scan.hasNext()) {
n = scan.nextInt();
ListNode head = new ListNode(-1);
ListNode temp = head;
// System.out.println("请输入n个节点: ");
for (int i = 0; i < n; i++) {
temp.next = new ListNode(scan.nextInt());
temp = temp.next;
}
// System.out.println("请输入要查找的倒数第k个节点: ");
int k = scan.nextInt();
ListNode target = FindKthToTail(head.next, k);
if (target != null)
System.out.println(target.val);
else
System.out.println(0);
}
}
}
20.杨辉三角的变形
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = in.nextInt();
if(n<=2)
System.out.println(-1);
else if(n%2==1){
System.out.println(2);
}else{
if(n%4==0)
System.out.println(3);
else
System.out.println(4);
}
}
in.close();
}
}
21.表达式求值
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
//将其他括号,替换成小括号
s=s.replace("{","(");
s=s.replace("[","(");
s=s.replace("}",")");
s=s.replace("]",")");
System.out.println(slove(s));
}
public static int slove(String s) {
Stack<Integer> stack = new Stack<>();
int n = s.length();
char[] chs = s.toCharArray();
int index = 0;
char sign = '+';
int number = 0;
for(int i=0; i<n; i++) {
char ch = chs[i];
if (ch == ' ') continue;
if (Character.isDigit(ch)) {
number = number * 10 + ch - '0';
}
if (ch == '(') {
int j = i + 1;
// 统计括号的数量
int cnt = 1;
while(cnt > 0) {
if (chs[j] == ')')
cnt--;
if (chs[j] == '(')
cnt++;
j++;
}
number = slove(s.substring(i+1, j-1));
i = j-1;
}
// 遇到符号,将数字处理后放进栈中
if (!Character.isDigit(ch) || i == n-1) {
if (sign == '+') {
stack.push(number);
} else if(sign == '-') {
stack.push(-1 * number);
} else if(sign == '*') {
stack.push(stack.pop() * number);
} else if(sign == '/') {
stack.push(stack.pop() / number);
}
sign = ch;
number = 0;
}
}
int ans = 0;
while(!stack.isEmpty()) {
ans += stack.pop();
}
return ans;
}
}
22.完全数计算
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int N = in.nextInt();
int count = 0;
for(int i=1;i<=N;i++){
int sum=0;
// 判断素数的方法
for(int j=1;j<i/2+1;j++){
if(i%j==0){
sum+=j;
}
}
if(sum==i){
count++;
}
}
System.out.println(count);
}
}
}
23.查找组成一个偶数最接近的两个素数
public class Main {
public Main() {
}
// 判断是否是素数
private boolean isPrime(int num) {
for (int i = 2; i <= num/i; i++) {
if (num % i == 0) return false;
}
return true;
}
public int count(int n) {
int i = n/2, j = n - i;
while (!isPrime(i) || !isPrime(j)) {
i++;
j--;
}
return j;
}
public static void main(String[] args) {
Main solution = new Main();
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = Integer.parseInt(in.next());
int res = solution.count(n);
System.out.println(res);
System.out.println(n - res);
}
}
}
24.放苹果
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
System.out.println(count(sc.nextInt(), sc.nextInt()));
}
sc.close();
}
public static int count(int m, int n) {
if (m < 0 || n <= 0)
return 0;
//细分到苹果数为一或盘子数为一的情况返回一
if (m == 1 || n == 1 || m == 0)
return 1;
//将此事件无线细分
// 一是留一个空盘
// 二是全满盘,每个盘先放一个苹果
return count(m, n - 1) + count(m - n, n);
}
}
25.查找输入整数二进制中1的个数
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int cnt = 0;
while (n > 0) {
if ( (n & 1) == 1) {
cnt++;
}
n = n >>> 1;
}
System.out.println(cnt);
}
}
}
26.百钱买百鸡问题
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int num = sc.nextInt();
// 5x+3y+z/3=100;
// x+y+z=100;
// 简化得 7x+4y=100;
int x, y, z, middle;
for (x = 0; x <= 14; x++) {
if ((100 - 7 * x) % 4 == 0) {
y = (100 - 7 * x) / 4;
z = 100 - x - y;
System.out.print(x + " ");
System.out.print(y + " ");
System.out.print(z + " ");
System.out.println();
}
}
}
}
}
27.计算日期到天数转换
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int[] month_day = {31,28,31,30,31,30,31,31,30,31,30,31};
int sum = 0;
for(int i = 0; i < month - 1; i++){
sum += month_day[i];
}
sum += day;
if(month > 2 && isLeap(year)){
sum += 1;
}
System.out.println(sum);
}
}
// 判断是都闰年
public static boolean isLeap(int n){
if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0){
return true;
}else{
return false;
}
}
}
28.尼科彻斯定理
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
long sum = (long)Math.pow(n,3);
int a1 = (int)sum/n - (n - 1);
StringBuilder sb = new StringBuilder(Integer.toString(a1));
for(int i = 1; i < n; i++){
a1 = a1 + 2;
sb.append("+");
sb.append(a1);
}
System.out.println(sb);
}
}
}
29.整型数组合并
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num1 = sc.nextInt();
TreeSet<Integer> set = new TreeSet<>();
for(int i=0;i<num1;i++){
set.add(sc.nextInt());
}
int num2 = sc.nextInt();
for(int i=0;i<num2;i++){
set.add(sc.nextInt());
}
for(int temp:set){
System.out.print(temp);
}
System.out.println();
}
sc.close();
}
}
30.字符串字符匹配
public class Main {
public Main() {
}
public boolean isAllCharExist(String s1, String s2) {
Set<Character> set = new HashSet<>();
for (char ch : s2.toCharArray()) {
set.add(ch);
}
for (char ch : s1.toCharArray()) {
if (!set.contains(ch)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Main solution = new Main();
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String s1 = in.nextLine();
String s2 = in.nextLine();
boolean res = solution.isAllCharExist(s1, s2);
System.out.println(res);
}
}
}
31.二维数组操作
public class Main {
public static void main(String[] args) throws IOException {
fx();
}
//region 83 二维数组操作
private static void fx() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = br.readLine()) != null) {
String[] strs = str.split(" ");
//1. 行和列
int row = Integer.parseInt(strs[0]);
int column = Integer.parseInt(strs[1]);
if(row >= 0 && row <= 9 && column >= 0 && column <= 9) {
System.out.println("0");
} else {
System.out.println(-1);
}
//2.单元格的 行 列值
String[] values = br.readLine().split(" ");
int rowValue1 = Integer.parseInt(values[0]);
int columnValue1 = Integer.parseInt(values[1]);
// values = br.readLine().split(" ");
int rowValue2 = Integer.parseInt(values[2]);
int columnValue2 = Integer.parseInt(values[3]);
if(rowValue1 >= 0 && rowValue1 < row && rowValue2 >= 0 && rowValue2 < row
&& columnValue1 >= 0 && columnValue1 < column && columnValue2 >= 0 && columnValue2 < column) {
System.out.println(0);
} else {
System.out.println(-1);
}
//3. 插入的行 的值
int insertRowValue = Integer.parseInt(br.readLine());
if(insertRowValue >= 0 && insertRowValue < row && (row + 1) <= 9) {
System.out.println(0);
} else {
System.out.println(-1);
}
//4. 插入的列 值
int insertColumnValue = Integer.parseInt(br.readLine());
if(insertColumnValue >= 0 && insertColumnValue < column && (column + 1) <= 9) {
System.out.println(0);
} else {
System.out.println(-1);
}
//5. 运动轨迹的单元格
strs = br.readLine().split(" ");
int x = Integer.parseInt(strs[0]);
int y = Integer.parseInt(strs[1]);
if(x >= 0 && x < row && y >= 0 && y < column) {
System.out.println(0);
} else {
System.out.println(-1);
}
}
br.close();
}
}
32.统计大写字母个数
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String s = input.nextLine();
System.out.println(count(s));
}
}
public static int count(String s){
int count=0;
char[] cs=s.toCharArray();
for(char c:cs){
if(c>='A' && c<='Z'){
count++;
}
}
return count;
}
}
33.最长回文子串
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int max = 0;
/**
*双指针遍历找到最长子串
*/
for (int i = 0; i < s.length(); i++) {
for (int j = s.length(); j > i; j--) {
String cur= s.substring(i, j);
if (isPalindromeString(cur)) {
max = Math.max(max, j - i);
}
}
}
System.out.print(max);
}
/**
*判断一个字符串是否是回文字符串的方法
*/
static boolean isPalindromeString(String s) {
return s.equals(new StringBuilder(s).reverse().toString());
}
}
34.求最大连续bit数
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextInt()){
int num = in.nextInt();
int max = 0;
int count = 0;
while(num != 0){
if((num&1) == 1){
count++;
max = Math.max(max,count);
}
else{
count = 0;
}
num >>>= 1;
}
System.out.println(max);
}
}
}
35.密码强度等级
public class Main {
/**
*
* score 分数
* upCount 大写字母数目
* lowCount 小写字母数目
* numCount 数字数目
* sigCount 符号数目
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
char[] ch = in.nextLine().toCharArray();
int score = 0;
//统计长度
int len = ch.length;
if(len <= 4) score += 5;
else if(len >4 && len < 8) score += 10;
else score += 25;
//遍历获取大小写字母、数字、符号数目
int upCount = 0;
int lowCount = 0;
int numCount = 0;
int sigCount = 0;
for (int i = 0; i < len; i++) {
if(Character.isUpperCase(ch[i])) ++upCount;
else if(Character.isLowerCase(ch[i])) ++lowCount;
else if(Character.isDigit(ch[i])) ++numCount;
else ++sigCount;
}
//字母分数
if((upCount > 0 && lowCount == 0) || (upCount == 0 && lowCount > 0)) score += 10;
else if(upCount > 0 && lowCount > 0) score += 20;
else score += 0;
//数字分数
if(numCount == 1) score += 10;
else if(numCount > 1) score += 20;
else score += 0;
//符号分数
if(sigCount == 1) score += 10;
else if(sigCount > 1) score += 25;
else score += 0;
//奖励分数
if (numCount > 0 && upCount > 0 && lowCount > 0 && sigCount > 0) score += 5;
else if(numCount > 0 && sigCount > 0 &&(upCount >0 || lowCount >0)) score += 3;
else if(numCount > 0 &&(upCount >0 || lowCount >0)) score += 2;
//评分
if(score >= 90) System.out.println("VERY_SECURE");
else if(score >= 80) System.out.println("SECURE");
else if(score >= 70) System.out.println("VERY_STRONG");
else if(score >= 60) System.out.println("STRONG");
else if(score >= 50) System.out.println("AVERAGE");
else if(score >= 25) System.out.println("WEAK");
else System.out.println("VERY_WEAK");
}
}
}
36.走方格的方案数
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int m = sc.nextInt();
int n = sc.nextInt();
System.out.println(cal(m,n));
}
}
private static int cal(int m,int n){
if(m==1 || n== 1){
return m+n;
}
return cal(m-1,n)+cal(m,n-1);
}
}
37.记票统计
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
//初始化
int numOfCandidates = in.nextInt();
in.nextLine();//指针移到下一行开头
String[] nameOfCandidates = in.nextLine().split(" ");
int numOfVotes = in.nextInt();
in.nextLine();//指针移到下一行开头
String[] voteFor = in.nextLine().split(" ");
Map<String,Integer> dict = new HashMap<>();
int invalid = 0;
for(int i = 0; i < numOfCandidates; i++){
dict.put(nameOfCandidates[i],0);
}
//计数
for(int j = 0; j < numOfVotes; j++){
if(!dict.containsKey(voteFor[j])){
invalid++;
}
else{
Integer pre = dict.get(voteFor[j]);
dict.put(voteFor[j],pre + 1);
}
}
//输出结果
StringBuilder res = new StringBuilder();
for(String s : nameOfCandidates){
res.append(s);
res.append(" : ");
res.append(dict.get(s));
res.append("\n");
}
res.append("Invalid : ");
res.append(invalid);
System.out.println(res);
}
}
}
38.表示数字
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String input = scanner.next();
//把所有的数字段提取出来,前后加上星号再放回去
System.out.println(input.replaceAll("([0-9]+)", "*$1*"));
}
}
}
39.记负均正
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int sum=Integer.parseInt(sc.nextLine());
String[] strs=sc.nextLine().trim().split(" ");
List<Integer> list=new ArrayList<Integer>();
for(String i:strs){
list.add(Integer.parseInt(i));
}
int count=0;
int total=0;
int n=0;
for(int j=0;j<list.size();j++){
if(list.get(j)>0){
count=count+list.get(j);
n++;
}else if(list.get(j)<0){
total++;
}
}
System.out.print(total+" "+Math.round(count*10.0/n)/10.0);
System.out.println();
}
}
}
40.自守数
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);
}
}
}
41.等差数列
通项公式:
an=a1+(n−1)d
前n
项和:
Sn=n(a1+an)/2
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int length = sc.nextInt(); // n
int first = 2, last = 3 * length - 1; // a1, an
System.out.println((first + last) * length / 2);
}
sc.close();
}
}
42.字符统计
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
System.out.println(count(sc.nextLine()).toString());
}
}
public static StringBuilder count(String str) {
char[] strArray = str.toCharArray();
int[] chArray = new int[129];
//字符对应ascll码值下标元素自增来统计数量
for (char i : strArray)
chArray[(int)i]++;
int max = 0;
//找出字符数量最多的ascll码值
for (int i = 0; i < chArray.length; i++)
if (max < chArray[i])
max = chArray[i];
StringBuilder sb = new StringBuilder();
//按数量从大到小添加到可变字符序列sb
while (max != 0) {
for (int i = 0; i < chArray.length; i++)
if (chArray[i] == max)
sb.append((char)i);
max--;
}
return sb;
}
}
43.字符逆序
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
StringBuilder res = new StringBuilder(input);
System.out.println(res.reverse());
}
}
44.求最小公倍数
欧几里得求最大公约数gcd(a,b),根据公式:最小公倍数lcm=a*b / gcd(a,b)
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.println(a*b/gcd(a,b));
}
private static int gcd(int a, int b) {
return b==0?a:gcd(b,a%b);
}
}
45.句子逆序
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.nextLine();
String s[]=str.split(" ");
for(int i=s.length-1;i>=0;i--)
if(i!=0)
System.out.print(s[i]+" ");
else
System.out.print(s[i]);
}
46.字符串排序
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
withPriorityQueue();
}
// 方法一: 调用API Arrays.sort
public static void withArraysAPI() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] ss = new String[n];
for (int i = 0; i < n; i++) {
ss[i] = br.readLine();
}
br.close();
Arrays.sort(ss);
for(int i = 0; i < n; i++) {
System.out.println(ss[i]);
}
}
// 方法二: 使用PriorityQueue
public static void withPriorityQueue() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
PriorityQueue<String> pq = new PriorityQueue<>();
String s = "";
while ((s = br.readLine()) != null) {
pq.offer(s);
}
br.close();
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
}
// 方法三: 使用list并自己实现Comparator, 比较能体现算法的思路
public static void withComparator() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<String> list = new ArrayList<>();
String s = "";
while ((s = br.readLine()) != null) {
list.add(s);
}
br.close();
list.sort(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int i = 0;
while (i < s1.length() && i < s2.length()) {
if (s1.charAt(i) != s2.charAt(i)) {
return (s1.charAt(i) > s2.charAt(i))? 1: -1;
}
i++;
}
if (s1.length() == s2.length()) {
return 0;
} else {
return (s1.length() > s2.length())? 1: -1;
}
}
});
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}