提交tes01

dev
huangguoxin 8 months ago
parent 89db9197e7
commit e46335e56d

@ -0,0 +1,531 @@
package com.msb.test01;
import org.junit.Test;
import java.util.*;
public class test01 {
@Test
public void test01(){
/**
* Exceljava)
*
* excel
* 仿Excel
*
* 377
* =
* 1.=B12
* 2.+-),=C1-C2,C3+B2
* 3.+-),B1+1,100-B2
*
*
* 1.=C+1,=C1-C2+B3,=5,=3+5
* 2.A1=B1+C1,C1=A1+B2
* 3,
*
*
* rows cols,1<=rows<=20,1<=cols<=26.
* rowscolsvalues
* A1:C2.
*
*
* -2147483648~2147483647
*
*
* 1-20A-Z,B3value[2][1].
*
* [0,100].
* B2:C10,B2:B5,B2Y2,C2:A1
*
*
*
*
*/
StringBuffer buffer = new StringBuffer();
String str = "abcdefgh";
if(str.length()<8){
buffer.append(str);
}
while(buffer.length()< 8 && str.length()<8){
buffer.append("0");
}
while(str.length()>8){
String strs =str.substring(0,8);
System.out.println(strs);
str = str.substring(8);
}
if(str.length()==8){
System.out.println(str);
}
if(str.length()<8 && buffer.length()<8){
buffer.append(str);
}
while(buffer.length()< 8 && str.length()<8 && str.length()>0){
buffer.append("0");
}
System.out.println(buffer.toString());
}
//递归方法实现
public void mergeSort1(int[] arr){
if(arr == null || arr.length <2){
return;
}
process(arr,0,arr.length -1);
}
public void process(int[] arr,int l,int r){
if(l==r){
return;
}
int mid = l + ((r -l) >>1);
process(arr,l,mid);
process(arr,mid+1,r);
merge(arr,l,mid,r);
}
public void merge(int[] arr,int l,int m,int r){
int[] help = new int[r-l+1];
int i=0;
int p1=l;
int p2= m+1;
while(p1<=m && p2<=r){
help[i++] = arr[p1]<= arr[p2]? arr[p1++]:arr[p2++];
}
//要么p1越界了要么p2越界了
while(p1<=m){
help[i++] = arr[p1++];
}
while(p2<=r){
help[i++]=arr[p2++];
}
for(i=0;i<help.length;i++){
arr[l+i] = help[i];
}
}
//当前考虑到了index号货物index...所有的货物可以自由选择
//做的选择不能超过背包容量
//返回最大价值
public static int process1(int[] w,int[] v,int index,int bag){
if(bag < 0){
return -1;
}
if(index == w.length){
return 0;
}
//有货index位置的货
//bag有空间
//不要当前的货
int p1 = process1(w,v,index+1,bag);
//要当前的货
int p2=0;
int next = process1(w,v,index+1,bag-w[index]);
if(next !=-1){
p2 = v[index] + next;
}
return Math.max(p1,p2);
}
@Test
public void test02(){
String str = "0101";
int anInt = Integer.parseInt(str, 2);
System.out.println(anInt);
TreeSet<Character> set = new TreeSet<Character>();
}
public void mergeSort2(int[] arr){
if(arr == null || arr.length < 2){
return;
}
int N = arr.length;
//步长
int mergeSize = 1;
while(mergeSize < N){
// 当前左组的,第一个位置
int L = 0;
while(L < N){
int M = L + mergeSize -1;
if (M >= N) {
break;
}
int R = Math.min(M + mergeSize,N-1);
merge(arr,L,M,R);
L = R +1;
}
//防止溢出
if(mergeSize > N/2){
break;
}
mergeSize <<= 1;
}
}
public int comparator(int[] arr){
if(arr == null || arr.length <2){
return 0;
}
int res = 0;
for(int i=1;i<arr.length;i++){
for(int j=0;j<i;j++){
res += arr[j] < arr[i] ? arr[j] :0;
}
}
return res;
}
public int smallSum(int[] arr){
if(arr == null || arr.length < 2){
return 0;
}
return process3(arr,0,arr.length - 1);
}
//arr[l,r)即要排好序,也要求小和返回
//所有merge时产生的小和累加
//左 排序 merge
//右 排序merge
//merge
public int process3(int[] arr,int l,int r){
if(l == r){
return 0;
}
// l < r
int mid = l + ((r - l) >> 1);
return process3(arr,l,mid) + process3(arr,mid + 1,r) + merge3(arr,l,mid,r);
}
public int merge3(int[] arr,int l,int m,int r){
int[] help = new int[r-l+1];
int i=0;
int p1 = l;
int p2= m+1;
int res =0;
while(p1<= m && p2 <= r){
res += arr[p1] < arr[p2] ? (r-p2+1) * arr[p1] :0;
help[i++] = arr[p1] < arr[p2] ? arr[p1++] :arr[p2++];
}
while(p1 <= m){
help[i++] = arr[p1++];
}
while(p2 <= r){
help[i++] = arr[p2++];
}
for(i=0;i<help.length;i++){
arr[l+i] = help[i];
}
return res;
}
public int biggerTwice(int[] arr){
if(arr == null || arr.length < 2){
return 0;
}
return process4(arr,0,arr.length -1);
}
public int process4(int[] arr,int l,int r){
if(l == r){
return 0;
}
int mid = l+((r-l) >> 1);
return process4(arr,l,mid) + process4(arr,mid+1,r) +
merge4(arr,l,mid,r);
}
public int merge4(int[] arr,int l,int m,int r){
return 0;
}
@Test
public void test03(){
/**
* 1java)
*
* m*n012.
* 11s,01.
* 2
*
*/
}
@Test
public void test04(){
/**
* Linuxjava)
*
* Linux distrowatch.com
* UbuntuDebianMintUbuntu
* MintDebian
*
*
* n*nisConnected,isConnected[i][j]=1
* ijisConnected[i][j]=0
*
*
* N,
*
*
*
* 1<=N<= 200
*
*
* 4
* 1 1 0 0
* 1 1 1 0
* 0 1 1 0
* 0 0 0 1
* 3
* Debian1Unbuntu2
* Mint(3)Ubuntu(2)
* EeulerOS(4)
* 313.
*
*/
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int[][] matrix = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j< n;j++){
matrix[i][j] = sc.nextInt();
}
}
System.out.println(getResult(matrix,n));
}
public int getResult(int[][] matrix,int n){
UnionFindSet ufs = new UnionFindSet(n);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
// j从i+1开始是因为矩阵是对称的
if(matrix[i][j] == 1){
ufs.union(i,j);
}
}
}
// conncted的key代表某个连通分量的顶级父节点value
//代表该连通分量下的节点个数
HashMap<Integer,Integer> connected = new HashMap<>();
for(int i=0;i<n;i++){
Integer fa = ufs.find(ufs.fa[i]);
connected.put(fa,connected.getOrDefault(fa,0)+1);
}
//返回最大节点数
return connected.values().stream().max((a,b)-> a-b).get();
}
@Test
public void test05(){
/**
* java)
*
*
* 使
*
*
* 1.N1<=n<10;
* 2.使
* 1 1 2 3 2 3.
* <=100
* 3.
*
*
*
*
*
*
*
* 1
* 1 2 1 2
* 0
* 1
* 12121
*
* 1
* 1 2 2 3 1 3
* 1
* 1
* 122
* 3131
*
*
*
*/
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String[] kids = sc.nextLine().split(" ");
System.out.println(getResult1(n,kids));
}
public int getResult1(int n,String[] kids){
//不开心的小朋友数量
int unHappy = 0;
//已在摇摇车上的小朋友编号
HashSet<String> playing = new HashSet<>();
//正在排队的小朋友编号
LinkedList<String> waiting = new LinkedList<>();
for(String kid : kids){
if(playing.contains(kid)){
//如果kid是摇摇车上的小朋友编号则代表kid玩好了要离开
playing.remove(kid);
//如果kid离开后摇摇车有空位了如果此时有人排队则让排队的人上车玩
if(waiting.size() > 0){
playing.add(waiting.removeFirst());
}
continue;
}
//如果kid不是摇摇车上的小朋友则检查id是不是排队的小朋友
int index = waiting.indexOf(kid);
if(index != -1){
// 如果是排队的小朋友。则说明kid没有玩到摇摇车因此会不开心的离开
unHappy++;
waiting.remove(index);
continue;
}
//如果kid既不是摇摇车上的小朋友也不是排队的小朋友则是
//新来的小朋友
if(playing.size() < n){
//如果摇摇车还有空位,则直接玩
playing.add(kid);
}else{
//如果摇摇车没有空位了,则需要排队
waiting.add(kid);
}
}
return unHappy;
}
@Test
public void test06(){
/**
* java)
*
* nesw,
*
*
* wenwes"跟"news
*
* "woood""wood"
* "wod"
* "not found"
*
* 1.","
* 2.","
*
* ","
* not found"
*
* N0<n<1000
* M0<M<1000
* P0<P<20
*
*
*
*
* conection
* connection ,today
* connection
*
*
* bdni,woood
* bind,wrong,wood
* bind,wood
*
*/
Scanner sc = new Scanner(System.in);
String[] issues = sc.nextLine().split(",");
String[] answers = sc.nextLine().split(",");
System.out.println(getResult6(issues,answers));
}
public String getResult6(String[] issues,String[] answers){
ArrayList<String> ans = new ArrayList<>();
for(String issue:issues){
String str1 = getSortAndDistinctStr(issue);
boolean find = false;
for(String answer: answers){
String str2 = getSortAndDistinctStr(answer);
if(str1.equals(str2)){
ans.add(answer);
find = true;
}
}
if(!find){
ans.add("not found");
}
}
StringJoiner sj = new StringJoiner(",","","");
for(String an:ans){
sj.add(an);
}
return sj.toString();
}
public String getSortAndDistinctStr(String str){
TreeSet<Character> set = new TreeSet<>();
for(char c:str.toCharArray()){
set.add(c);
}
return set.toString();
}
}
//并查集实现
class UnionFindSet{
int[] fa;
int count;
public UnionFindSet(int n){
this.count = n;
this.fa = new int[n];
for(int i=0;i<n;i++){
this.fa[i] =i;
}
}
public int find(int x){
if(x != this.fa[x]){
return (this.fa[x] = this.find(this.fa[x]));
}
return x;
}
public void union(int x,int y){
int x_fa = this.find(x);
int y_fa = this.find(y);
if(x_fa != y_fa){
this.fa[y_fa] = x_fa;
this.count--;
}
}
}
Loading…
Cancel
Save