📑前言
本文主要是【算法】——蓝桥杯练习题(三)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 📑前言
- 1331.二进制中1的个数
- 321.分发饼干
- 312.确定一个数是否为2的幂
- 203.确定字符串是否是另一个的排列
- 2141.山
- 204.压缩字符串
- 📑文章末尾
1331.二进制中1的个数
package 蓝桥杯第三次;import java.util.Scanner;public class 二进制中1的个数1 {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int n = sc.nextInt();long ans = 0;String s = Integer.toBinaryString(n);for(int i=0;i<s.length();i++) {if(s.charAt(i)=='1') {ans++;}}System.out.println(ans);}}
321.分发饼干
package 蓝桥杯第三次;import java.util.Arrays;
import java.util.Scanner;public class 分发饼干 {/*
3 2
1 2 3
1 1
1*/public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int m = sc.nextInt();int n = sc.nextInt();int num1[] = new int[m];int num2[] = new int[n];for(int i=0;i<m;i++) {num1[i] = sc.nextInt();}for(int i=0;i<n;i++) {num2[i] = sc.nextInt();}Arrays.sort(num1);Arrays.sort(num2);long ans=0;int i=0,j=0;while (true) {if(i<m&&j<n&&num1[i]<=num2[j]) {i++;j++;ans++;}else {j++;}if(i==m||j==n) {break;}}System.out.println(ans);}}
312.确定一个数是否为2的幂
package 蓝桥杯第三次;import java.util.Scanner;public class 确定一个数字是否是2的幂 {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int n = sc.nextInt();System.out.println(((n&(n-1))==0)?"YES":"NO");}}
203.确定字符串是否是另一个的排列
package 蓝桥杯第三次;import java.util.Arrays;
import java.util.Scanner;public class 确定字符串是否是另一个的排列 {
/*
acb
bac
YES*/public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);String str1 = sc.next();String str2 = sc.next();char a[] = str1.toCharArray();char b[] = str2.toCharArray();Arrays.sort(a);Arrays.sort(b);String string1 = Arrays.toString(a);String string2 = Arrays.toString(b);if(string1.equals(string2)) {System.out.println("YES");}else {System.out.println("NO");}}}
2141.山
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {// TODO Auto-generated method stubint ans = 0;for(int i=2022;i<=2022222022;i++) {if(dandiao(i)&&huiwen(i)) {ans++;}}System.out.println(ans);}public static boolean dandiao(int n) {int i=0;int a[] = new int[10];while(n>0) {a[i++]=n%10;n/=10;}int l,r;if(i%2==0) {l=i/2-1;r=i/2;}else {l=i/2;r=i/2;}//单调不减for(int j=1;j<=l;j++) {if(a[j]<a[j-1]) {return false;}}//单调不增for(int j=i-2;j>=r;j--) {if(a[j]<a[j+1]) {return false;}}return true;}public static boolean huiwen(int n) {int i=0;int a[] = new int[10];while(n>0) {a[i++]=n%10;n/=10;}int l=0,r=i-1;while(l<r) {if(a[l]!=a[r]) {return false;}l++;r--;}return true;}
}
204.压缩字符串
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String s = sc.next();char[] a = s.toCharArray();String ans = "";int k = 1;for(int i=0;i<a.length-1;i++) {if(a[i]==a[i+1]) {k++;}else {if(k>1) {ans+=a[i]+""+k+"";k=1;}else {ans+=String.valueOf(a[i]);}}}if(k>1) {ans+=a[a.length-1]+""+k+"";}else {ans+=a[a.length-1];}System.out.println(ans);}
}