本文主要是【算法】——蓝桥杯练习题(三)的文章,如果有什么需要改进的地方还请大佬指出??
🎬作者简介:大家好,我是听风与他🥇
??博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
package 蓝桥杯第三次;
import java.util.Scanner;
public class 二进制中1的个数1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner 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);
}
}
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 stub
Scanner 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);
}
}
package 蓝桥杯第三次;
import java.util.Scanner;
public class 确定一个数字是否是2的幂 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(((n&(n-1))==0)?"YES":"NO");
}
}
package 蓝桥杯第三次;
import java.util.Arrays;
import java.util.Scanner;
public class 确定字符串是否是另一个的排列 {
/*
acb
bac
YES
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner 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");
}
}
}
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int 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;
}
}
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);
}
}