首页 > 试题广场 >

现有一个支付宝口令红包需要被领取。口令由m(m的范围是1~4

[问答题]

现有一个支付宝口令红包需要被领取。口令由m(m的范围是1~4) 个数字组成,每一位是0, 1, 2, ..., n-1(n的范围是1~10)中的一个。

假设你可以不停地输入数字,支付宝会自动记住最后的m位数字,如果m位数字组成地字符串与口令一致,就可以领取到红包。

例如,口令为"123",你可以输入"00123"来领取红包,这样你就是输入了5个字符。

请返回一个能打开该口令红包的最短字符串。

示例:

输入:m = 2, n = 2

输出:"01100"("00110"或"10011"或“10011”也同样可以)

public class Main {

	public static void main(String[] args) {
		int m=3;//由m个数字组成
		int n=2;//范围是0-(n-1)
		int s[]= new int[m];
		int all=(int)Math.pow(n,m);
		Set<String> set = new HashSet<String>();
		while(set.size()<all){
			String str="";
			for(int i1=0;i1<m;i1++){
				str=str+(int)(Math.random()*n);
			}
			set.add(str);
		}
		String str[] = new String[all];
		int k=0;
		for(String e:set){
			str[k]=e	;
			k++;
		}
		Arrays.sort(str);
		String word=str[str.length-1];
		for(int i=0;i<all;i++){
			String other=str[i];
			String q1w=word.substring(0,1);
			String h1w=word.substring(word.length()-1,word.length());
			String q1o=other.substring(0,1);
			String h1o=other.substring(other.length()-1,other.length());
			if(m==1){
				if(!word.contains(other)){
					word=word+other;
				}
			}
			if(m==2){
				if(!word.contains(other)){
					if(h1w.equals(q1o)){
						word=word+h1o;
					}else if(q1w.equals(h1o)){
						word=q1o+word;
					}else{
						word=word+other;
					}
				}
				
			}
			if(m==3){
				String q2w=word.substring(0,2);
				String h2w=word.substring(word.length()-2,word.length());
				String q2o=other.substring(0,2);
				String h2o=other.substring(other.length()-2,other.length());
				if(!word.contains(other)){
					if(h2w.equals(q2o)){
						word=word+h1o;
					}else if(q2w.equals(h2o)){
						word=q1o+word;
					}else if(h1w.equals(q1o)){
						word=word+h2o;
					}else if(q1w.equals(h1o)){
						word=q2o+word;
					}else{
						word=word+other;
					}
				}
			}
			if(m==4){
				String q3w=word.substring(0,3);
				String q2w=word.substring(0,2);
				String h3w=word.substring(word.length()-3,word.length());
				String h2w=word.substring(word.length()-2,word.length());
				String q3o=other.substring(0,3);
				String q2o=other.substring(0,2);
				String h3o=other.substring(other.length()-3,other.length());
				String h2o=other.substring(other.length()-2,other.length());
				if(!word.contains(other)){
					if(h3w.equals(q3o)){
						word=word+h1o;
					}else if(q3w.equals(h3o)){
						word=q1o+word;
					}else if(h2w.equals(q2o)){
						word=word+h2o;
					}else if(q2w.equals(h2o)){
						word=q2o+word;
					}else if(h1w.equals(q1o)){
						word=word+h3o;
					}else if(q1w.equals(h1o)){
						word=q3o+word;
					}else{
						word=word+other;
					}
				}
			}
			System.out.println(word);
			}
			
			
		
	}

}

发表于 2019-12-12 10:41:24 回复(0)