当前位置:编程学习 > JAVA >>

java 代码一个游戏两个数字测试对比通过

"" 0
12 21
13 31

22 220
123 321

222 2220
1234 1243
1243 1324
1342 1423

2222 22220
12345 12354

33333 333330
123456 123465


以此类推。。可以无限个对比都能通过

两个参数对比,用java 代码写怎么能让上面的对比通过 java 类 游戏 测试 --------------------编程问答--------------------
引用 楼主 u011891132 的回复:
"" 0
12 21
13 31

22 220
123 321

222 2220
1234 1243
1243 1324
1342 1423

2222 22220
12345 12354

33333 333330
123456 123465


以此类推。。可以无限个对比都能通过

两个参数对比,用java 代码写怎么能让上面的对比通过


比如
33333 333330
123456 123465

把33333 33333 通过
123456 123456 用java 方法去传换数字的位子 和去除0
--------------------编程问答-------------------- 看不懂需求的说 --------------------编程问答-------------------- //任意长度的数,保持所含数字不变,求比原数大一点的数。
//例如: 123 -> 132
//当不能变的更大时,则添加一个'0'
//例如: 1 -> 10 --------------------编程问答-------------------- public class Test4 {
public static void main(String[] args) {
Test4 t = new Test4();
Integer source = 1342;
Integer s = t.change(source);
System.out.println("处理前:"+source+"   处理后:"+s); 

}
public Integer change(Integer source){
if(source == null){
return 0;
}
String tempStr = source+"";
int length = tempStr.length();
char less;
char great;
char temp;
char [] c = new char[length];
for(int i=0;i<length;i++){
c[i] = tempStr.charAt(i);
}
char[] afterChar;
boolean isMax = true;
StringBuffer res = new StringBuffer();
for(int i=length-1;i>0;i--){
great = c[i];
for(int j=i-1;j>=0;j--){
temp = c[j];
if(great > temp){
isMax = false;
int frontCharLenght = j+1;
int afterCharLength = length-j-1;
afterChar = new char[afterCharLength];
for(int k=0;k<afterCharLength;k++){
afterChar[k] = c[length-afterCharLength+k]; 
}
Arrays.sort(afterChar);
for(int k=0;k<afterCharLength;k++){
less = afterChar[k];
if(less > temp){
c[j] = less;
afterChar[k] = temp;
break;
}
}
Arrays.sort(afterChar);
for(int k=0;k<afterCharLength;k++){
c[frontCharLenght+k] = afterChar[k];
}
for(int k=0;k<length;k++){
res.append(c[k]);
}
source = Integer.parseInt(res.toString());
return source;
}else{
great = temp;
i=j;
}
}
}
if(isMax){
source = source*10;
}
return source;
}
} --------------------编程问答-------------------- 让给楼下回答吧 --------------------编程问答--------------------
引用 4 楼 u011942744 的回复:
public class Test4 {
public static void main(String[] args) {
Test4 t = new Test4();
Integer source = 1342;
Integer s = t.change(source);
System.out.println("处理前:"+source+"   处理后:"+s); 

}
public Integer change(Integer source){
if(source == null){
return 0;
}
String tempStr = source+"";
int length = tempStr.length();
char less;
char great;
char temp;
char [] c = new char[length];
for(int i=0;i<length;i++){
c[i] = tempStr.charAt(i);
}
char[] afterChar;
boolean isMax = true;
StringBuffer res = new StringBuffer();
for(int i=length-1;i>0;i--){
great = c[i];
for(int j=i-1;j>=0;j--){
temp = c[j];
if(great > temp){
isMax = false;
int frontCharLenght = j+1;
int afterCharLength = length-j-1;
afterChar = new char[afterCharLength];
for(int k=0;k<afterCharLength;k++){
afterChar[k] = c[length-afterCharLength+k]; 
}
Arrays.sort(afterChar);
for(int k=0;k<afterCharLength;k++){
less = afterChar[k];
if(less > temp){
c[j] = less;
afterChar[k] = temp;
break;
}
}
Arrays.sort(afterChar);
for(int k=0;k<afterCharLength;k++){
c[frontCharLenght+k] = afterChar[k];
}
for(int k=0;k<length;k++){
res.append(c[k]);
}
source = Integer.parseInt(res.toString());
return source;
}else{
great = temp;
i=j;
}
}
}
if(isMax){
source = source*10;
}
return source;
}
}


哈哈 还是有一个数字错了 :
你的程序输入 处理前:321   处理后:3210

可是正确的是输入321 得出不能多0 只是别321大的数 --------------------编程问答-------------------- 在保持所含数字不变的情况下,没有比321更大一点的数字,所以得在后面加0,这是题目的要求,难道是我理解错了?如果不是3210,那应该是什么数? --------------------编程问答-------------------- 我感觉这个就是一个从后向前比较数的大小如果前边的小就交互位置结束。在递归;最后在统计没有一次交互的在后边补零;
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,