一个看似简单算法 给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含),指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。 * 比如,A=[1,0] K=21 那么输出结构应该为100 有了思路,但写了一晚上没写出来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 还真是有点难度,搞了一个多小时.这个题目玩了一个数字游戏,我们只需要找出最大的一位,后面补上最小的数字,就得到答案了.public class TTT { /** * @param args */ public static void main(String[] args) { int[] list = { 0, 1, 3, 4, 7, 8 }; // int[] list = { 0, 1,8 }; // 先对list排序,这里默认排好序,并且去掉重复项 String value = "138"; System.out.println("value is 1000 result is " + haha1("931", list)); System.out.println("value is 1000 result is " + haha1("999", list)); System.out.println("value is 1000 result is " + haha1("888", list)); System.out.println("value is 1000 result is " + haha1("891", list)); System.out.println("value is 300 result is " + haha1("234", list)); System.out.println("value is 330 result is " + haha1("324", list)); System.out.println("value is 3 result is " + haha1("1", list)); System.out.println("value is 1 result is " + haha1("0", list)); System.out.println("value is 10 result is " + haha1("9", list)); System.out.println("value is 100 result is " + haha1("90", list)); System.out.println("value is 137 result is " + haha1("135", list)); System.out.println("value is 140 result is " + haha1("138", list)); System.out.println("value is 138 result is " + haha1("137", list)); } public static String haha1(String value, int[] list) { String result = ""; String preMax = ""; for (int i = 0; i < value.length(); i++) { int tempValue = Integer.parseInt(value.substring(i, i + 1)); boolean hasBreak = false; for (int j = 0; j < list.length; j++) { if (list[j] == tempValue) { hasBreak = true; if (j < list.length - 1) { preMax = value.substring(0, i) + list[j + 1]; } else { if (preMax.equals("")) { if (list[0] == 0) { for (int aa = i; aa >= 0; aa--) { preMax = preMax + list[0]; } preMax = list[1] + preMax; } else { for (int aa = i; aa >= 0; aa--) { preMax = preMax + list[0]; } } } else { preMax = preMax + list[0]; } } break; } else if (list[j] > tempValue) { if (i == 0) { result = String.valueOf(list[j]); } else { result = value.substring(0, i) + list[j]; } return replace(result, String.valueOf(list[0]), value.length() - result.length()); } } if (preMax.equals("")) { if (list[0] == 0) { result = list[1] + "" + list[0]; return replace(result, String.valueOf(list[0]), value.length() - 1); } else { result = list[0] + "" + list[0]; return replace(result, String.valueOf(list[0]), value.length() - 1); } } if (!hasBreak) { for (int k = i; k < value.length(); k++) { result = result + list[0]; } return preMax + result; } } return preMax; } // 将除第一位之外的全都置为最小值 public static String replace(String value, String key, int num) { for (int i = 0; i < num; i++) { value = value + key; } return value; }} public static int isContain(int[] A,int K) { int returnValue=-1; int temp=1; while(K++>0) { int tempCondition=0; temp=K; while(temp>0) { for(int i=0;i<A.length;i++) { if(A[i]==temp%10) { tempCondition++; break; } } temp/=10; } if(tempCondition==String.valueOf(K).length()) { returnValue=K; break; } } return returnValue; } JAVA WEB数据库连接 关于hibernate保存的问题 sql 数据库里ntext类型中文读取出来全是乱码!寻求解决方法!! 用struts2怎么写的分页 java oa源码谁有? 有人知道如何使用ibatis实现复合条件查询吗? 能给出一个框架吗? 实现下面的一个网页: JSP中数据库问题! 高手进来帮帮忙!!!!!特急!!!!! The requested resource () is not available 关于lucene中文查询问题,分词用的是庖丁解牛,在线等。。。 spring配置问题
* @param args
*/
public static void main(String[] args) {
int[] list = { 0, 1, 3, 4, 7, 8 };
// int[] list = { 0, 1,8 };
// 先对list排序,这里默认排好序,并且去掉重复项 String value = "138"; System.out.println("value is 1000 result is " + haha1("931", list));
System.out.println("value is 1000 result is " + haha1("999", list));
System.out.println("value is 1000 result is " + haha1("888", list));
System.out.println("value is 1000 result is " + haha1("891", list));
System.out.println("value is 300 result is " + haha1("234", list));
System.out.println("value is 330 result is " + haha1("324", list));
System.out.println("value is 3 result is " + haha1("1", list));
System.out.println("value is 1 result is " + haha1("0", list));
System.out.println("value is 10 result is " + haha1("9", list));
System.out.println("value is 100 result is " + haha1("90", list));
System.out.println("value is 137 result is " + haha1("135", list));
System.out.println("value is 140 result is " + haha1("138", list));
System.out.println("value is 138 result is " + haha1("137", list));
} public static String haha1(String value, int[] list) {
String result = "";
String preMax = ""; for (int i = 0; i < value.length(); i++) {
int tempValue = Integer.parseInt(value.substring(i, i + 1));
boolean hasBreak = false;
for (int j = 0; j < list.length; j++) {
if (list[j] == tempValue) {
hasBreak = true;
if (j < list.length - 1) {
preMax = value.substring(0, i) + list[j + 1];
} else {
if (preMax.equals("")) {
if (list[0] == 0) {
for (int aa = i; aa >= 0; aa--) {
preMax = preMax + list[0];
}
preMax = list[1] + preMax;
} else {
for (int aa = i; aa >= 0; aa--) {
preMax = preMax + list[0];
}
}
} else {
preMax = preMax + list[0];
}
}
break;
} else if (list[j] > tempValue) {
if (i == 0) {
result = String.valueOf(list[j]);
} else {
result = value.substring(0, i) + list[j];
}
return replace(result, String.valueOf(list[0]), value.length()
- result.length());
}
}
if (preMax.equals("")) {
if (list[0] == 0) {
result = list[1] + "" + list[0];
return replace(result, String.valueOf(list[0]), value.length() - 1);
} else {
result = list[0] + "" + list[0];
return replace(result, String.valueOf(list[0]), value.length() - 1);
}
}
if (!hasBreak) {
for (int k = i; k < value.length(); k++) {
result = result + list[0];
}
return preMax + result;
}
} return preMax;
} // 将除第一位之外的全都置为最小值
public static String replace(String value, String key, int num) {
for (int i = 0; i < num; i++) {
value = value + key;
}
return value;
}
}
{
int returnValue=-1;
int temp=1;
while(K++>0)
{
int tempCondition=0;
temp=K;
while(temp>0)
{
for(int i=0;i<A.length;i++)
{
if(A[i]==temp%10)
{
tempCondition++;
break;
}
}
temp/=10;
}
if(tempCondition==String.valueOf(K).length())
{
returnValue=K;
break;
}
}
return returnValue;
}