题目一:
字符串压缩
package 算法提高;
import java.util.HashMap;
import java.util.Scanner;
public class 字符串压缩 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder bu = new StringBuilder();
//声明一个数组 用于计数 ,
int a[] = new int[180];
for (int i = 0; i < str.length(); i++) {
int ff = str.charAt(i);
a[ff]++;
//看相应的字符对应的数组是否符合要求
if (a[ff] == 1 || a[ff] == 6 || a[ff] == 3 || ff == 32) {
bu = bu.append(str.charAt(i) + "");
}
}
System.out.println(bu.toString());
}
}
统计1的个数:
import java.util.Scanner;
public class 二进制数种1的个数 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
count1(num);
count2(num);
count3(num);
}
//1,用Integer.biCount()方法统计1的个数(推荐)
public static void count1(int num){
int sum = Integer.bitCount(num);//直接统计1的个数
System.out.println(sum);
}
//2,转化成二进制,然后挨个去找1
public static void count2( int num){
int sum = 0 ;
System.out.println(Integer.toString(num,2));
String ff = Integer.toString(num,2);
for(int i = 0 ; i<ff.length();i++){
sum+=ff.charAt(i)=='1'?1:0;
}
System.out.println(sum);
}
//3,将二进制的0替换0成"",然后统计字符串长度
public static void count3( int num){
int sum = 0 ;
String ff = Integer.toString(num,2);
ff=ff.replaceAll("0","");
sum=ff.length();
System.out.println(sum);
}
// 4运用右移动,
public static void count4( int num){
int sum = 0 ;
while (num>0){
if((num&1)==1) sum++;
num>>=1;
}
System.out.println(sum);
}
//总结 :&1结果是1才返回
// >>1二进制减小,1001,100,10,1,0
}
字符串拼接
方法二
package 备赛课;
import java.util.Scanner;
public class a字符是否能拼接b字符 {
//盛放第一个数组的大写字母, 小写字母
static int [] c1 = new int[26], c2 = new int[26];
//盛放第二个数组的大写字母, 小写字母
static int [] c3 = new int[26], c4= new int[26];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
//判断第第一个字符串内的相应的字符数量
for(int i= 0 ; i <str1.length();i++){
char c = str1.charAt(i);
// 如果大于Z 那么就是小写字母, 然后用 数组记录,否则小写
if(c>'Z') c1[c-'a']++;
else c2[c-'A']++;
}
//判断第第二个字符串内的相应的字符数量
for(int i= 0 ; i <str2.length();i++){
char c = str2.charAt(i);
if(c>'Z') c3[c-'a']++;
else c4[c-'A']++;
}
//如果第一个字符串的大写字母个数与第二个字符串大写字母个数不一样,或者小写不一样就返回No终止程序
for (int i = 0 ; i<26;i++){
if(c1[i]!=c3[i]||c2[i]!=c4[i]){
System.out.println("No");
return;
}
}
System.out.println("yes");
}
}
总结:
c3[c-'a']++;统计个数,能用这种就用这种
}
方法一:
package 备赛课;
import java.util.Arrays;
import java.util.Scanner;
public class a字符是否能拼接b字符001 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char [] s = sc.next().toCharArray();
char [] t = sc.next().toCharArray();
Arrays.sort(s);
Arrays.sort(t);
String s1 = new String(s);
String s2 = new String(t);
System.out.println(s1.equals(s2)?"yes":"no");
}
进制转换:
package 备赛课.基础2;
import 备赛课.基础1.分饼干;
import java.math.BigInteger;
public class 进制 {
public static void main(String[] args) {
//java自带的进制转换
//(1),Integer.toString(数字,几进制);10进制转0~35进制,记过为字符串
String ss = Integer.toString(255,2);
String ss1 = Integer.toString(255,8);
String ss2= Integer.toString(255,16);
System.out.println(ss);
System.out.println(ss1);
System.out.println(ss2);
System.out.println("____________________________");
//(2),Integer.parseInt(字符串,几进制);把及进制的字符串转换成十进制。
int a = Integer.parseInt("10001",2);
int a1= Integer.parseInt("ff",16);
int a2= Integer.parseInt("5656",10);
int a3= Integer.parseInt("77",8);
System.out.println(a);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
//(3),BigIngter m = new BigInteger(字符串,几进制);把几进制的数转换成十进制的大数
BigInteger m = new BigInteger("ff",16);
BigInteger m1 = new BigInteger("255",8);
BigInteger m2 = new BigInteger("0110110",2) ;
System.out.println("BIg Integer is "+m1);
System.out.println("BIg Integer is "+m);
System.out.println("BIg Integer is "+m2);
}
}