问题:键盘输入一个高精度的正整数N(N<=240位),去掉任意S个数字后剩下的数字按原来左右次序组成一个新的正整数。编程实现对给定的N和S,寻找一种解决方案,使得剩下的数最小。
例如,给定的N=178543,S=4,则得到的结果为13
答:实现代码如下:
/**
* @author: YuHuang
* @date: 2011-11-30
* @summary: Just for fun.
*/
public class DeleteNumberProblem {
public static String doDelete(String n,int s){
char[] numberArray = n.toCharArray();
int len = numberArray.length;
if(len<=s) {
System.out.println("invalid s!");
return null;
}else{
int deleteLength = 0;
int pos = 1;
while(pos<len){
if(numberArray[pos]<numberArray[pos-1]) {
int i=pos;
while(i<len){
numberArray[i-1] = numberArray[i];
++i;
}
pos = pos>1 ? --pos:pos;
--len;
++deleteLength;
}else{
pos++;
}
if(deleteLength==s) break;
}
len = len-(s-deleteLength);
}
StringBuilder sb = new StringBuilder();
for(int index=0;index<len;++index){
sb.append(numberArray[index]);
}
return sb.toString();
}
public static void main(String[] args) {
String n = "178543";
int s = 4;
String result = DeleteNumberProblem.doDelete(n,s);
System.out.println(result);
}
}
代码运行结果:
Lab-Computer-0db2f6:JavaExercises labuser$ javac DeleteNumberProblem.java
Lab-Computer-0db2f6:JavaExercises labuser$ java DeleteNumberProblem
13
Lab-Computer-0db2f6:JavaExercises labuser$
分享到:
相关推荐
1.河内之塔.. 2.Algorithm Gossip: 费式数列. 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官(一) 6.Algorithm Gossip: 老鼠走迷... 21.Algorithm Gossip: 最大访客数等等经典问题详解
论文研究-SGA(Simplex-Genetic Algorithm):一类求解Minimax问题的通用算法.pdf, 在指出一般的迭代法不能保证收敛性之后,将注意力投向基于Stackelberg-NashEquilibrium的...
赠送jar包:pentaho-aggdesigner-algorithm-5.1.5-jhyde.jar; 赠送原API文档:pentaho-aggdesigner-algorithm-5.1.5-jhyde-javadoc.jar; 赠送源代码:pentaho-aggdesigner-algorithm-5.1.5-jhyde-sources.jar; ...
洗扑克牌(乱数排列) 58 25.Algorithm Gossip: Craps赌博游戏 60 26.Algorithm Gossip: 约瑟夫问题(Josephus Problem) 62 27.Algorithm Gossip: 排列组合 64 28.Algorithm Gossip: 格雷码(Gray ...
java.lang.RuntimeException: Unsupported algorithm: HmacSHA1 解决方法,阿里云
Automated Algorithm Selection: Survey and Perspectives。今年的新survey,对算法自动调参领域作了详尽介绍。
河内之塔2.Algorithm Gossip: 费式数列3. 巴斯卡三角形4.Algorithm Gossip: 三色棋5... 生命游戏11.Algorithm Gossip: 字串核对12.Algorithm Gossip: 双色、三色河内塔13.Algorithm Gossip: 背包问题(Knapsack Problem
可以解决,maven引入hive jar包时,hive Could not find artifact org.pentaho:pentaho-aggdesigner-algorithm:jar:5.1.5-jhyde 问题
解决 Cannot resolve org.pentaho:pentaho-aggdesigner-algorithm:5.1.5-jhyde jar放入D根目录执行: 以下命令加入本地maven库 mvn install:install-file -DgroupId=org.pentaho -DartifactId=pentaho-aggdesigner-...
解决maven引入hive的jar包时依赖报错Could not find artifact org.pentaho:pentaho-aggdesigner-algorithm:pom:5.1.5-jhyde in xxx的问题,maven路径org/pentaho/pentaho-aggdesigner-algorithm/5.1.5-jhyde/pentaho...
DGA- An Efficient Genetic Algorithm DGA:一种高效的遗传算法.doc
2.Algorithm Gossip: 费式数列 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官 6.Algorithm Gossip: 老鼠走迷官(二) 7.Algorithm Gossip: 骑士走棋盘 8.Algorithm Gossip: 八皇 9....
algorithm design foundations analysis and internet examples 中文版
骑士走棋盘8.Algorithm Gossip: 八皇后9.Algorithm Gossip: 八枚银币10.Algorithm Gossip: 生命游戏11.Algorithm Gossip: 字串核对12.Algorithm Gossip: 双色、三色河内塔13.Algorithm Gossip: 背包问题(Knapsack ...
面向初学者,深入浅出,通俗易懂的算法介绍。Once Upon an Algorithm: How Stories Explain Computing
13.Algorithm Gossip: 背包问题(Knapsack Problem)....... 29 14.Algorithm Gossip: 蒙地卡罗法求PI.............. 34 15.Algorithm Gossip: Eratosthenes 筛选求质数......36 16.Algorithm Gossip: 超长整数运算...
maven 报错pentaho-aggdesigner-algorithm:jar:5.1.5-jhyde 解决下载不了问题,记录下