590
社区成员
发帖
与我相关
我的任务
分享
maxValue(2,9890);这种数据就会报错
function maxValue($n,$m)
{
$m = (string)$m;
for($i=$n;$i>0;$i--)
{
$t = min($i*2,strlen($m));
for($j=1,$k=0;$j<$t;$j++)
{
$t1 = $m{$k};
$t2 = $m{$j};
if($t1 > $t2)
{
$k = $j;
}
}
$tmp = substr($m,0,$k).substr($m,$k+1);
$m = $tmp;
}
return $m;
}
echo maxValue(1,51342);
echo "<br>";
echo maxValue(2,51342);
echo "<br>";
echo maxValue(2,731518);
echo "<br>";
echo maxValue(3,731518);
echo "<br>";
echo maxValue(4,731518);
最后结果为:
5342
542
7518
758
78
public static int maxValue(int n, int m) {
String temp = m + "";
// 屏蔽异常问题
if (n >= temp.length()) {
return 0;
}
// 只剩一个,查询到最大值就可以了
char[] charArr = temp.toCharArray();
Arrays.sort(charArr);
if (n == temp.length() - 1) {
return charArr[temp.length() - 1] - 48;
}
// 开始正常排序,List 方便删除
List<String> list = new ArrayList<String>();
for (int i = 0; i < charArr.length; i++) {
list.add("" + charArr[i]);
}
int deleteNums = 0;
StringBuilder lessStr = new StringBuilder();
while (deleteNums < n) {
for (int i = list.size() - 1; i >= 0; i--) {
// 发现最大的数据是在那个位置,判断这个数据前面的数据是不是可以删除
int maxIndex = temp.indexOf(list.get(i));
if (maxIndex <= n - deleteNums) {
lessStr.append(temp.charAt(maxIndex));
temp = temp.substring(maxIndex + 1);
deleteNums += maxIndex;
list.remove(i);
break;
}
}
}
return Integer.valueOf(new String(lessStr.append(temp)));
}
private static String maxValue(int n, String m) {
if(n==0||n>=m.length()){
return "";
}
char c = m.charAt(0);
int index = 0;
for (int j = 1; j < m.length(); j++) {
if ((c - '0') < (m.charAt(j) - '0')) {
c = m.charAt(j);
index = j;
}
}
if(index>n){
m = maxValue(n,m.substring(0,index))+m.substring(index,m.length());
}else if(index<n){
m = m.substring(index,index+1)+maxValue(n-index,m.substring(index+1, m.length()));
}else{
m = m.substring(index,m.length());
}
return m;
}
function maxValue($n,$m)
{
echo "n=$n,m=$m : ";
$m = (string)$m;
$len = strlen($m) - $n;//结果字符串长度
$str = "";
for($i=$n;$i>0;)
{
for($j=1,$k=0;$j<=$i;$j++)
{
$t1 = $m{$k};
$t2 = $m{$j};
if($t1 < $t2)
{//找出前N个中最大数字位置
$k = $j;
}
}
$str .= $m{$k};
if(strlen($str) == $len)return $str;
$i = $i - $k;
$m = substr($m,$k+1);
}
$str .= $m;
return $str;
}
echo maxValue(1,51342);
echo "<br>";
echo maxValue(2,51342);
echo "<br>";
echo maxValue(1,731518);
echo "<br>";
echo maxValue(3,731518);
echo "<br>";
echo maxValue(4,731518);
echo "<br>";
echo maxValue(1,654321);
echo "<br>";
echo maxValue(2,123456);
echo "<br>";
echo maxValue(1,425161);
echo "<br>";
echo maxValue(2,425161);
echo "<br>";
echo maxValue(3,425161);
echo "<br>";
echo maxValue(4,425161);
echo "<br>";
echo maxValue(5,425161);
n=1,m=51342 : 5342
n=2,m=51342 : 542
n=1,m=731518 : 73518
n=3,m=731518 : 758
n=4,m=731518 : 78
n=1,m=654321 : 65432
n=2,m=123456 : 3456
n=1,m=425161 : 45161
n=2,m=425161 : 5161
n=3,m=425161 : 561
n=4,m=425161 : 61
n=5,m=425161 : 6
private static void maxValue(int n, int m) {
String s = m + "";
char c;
int index;
if (n >= s.length()) {
System.out.println("error");
return;
}
for (int i = 0; i < n; i++) {
index = 0;
c = s.charAt(index);
for (int j = 1; j < s.length(); j++) {
if(i==n-1){
if ((s.charAt(j-1) - '0') < (s.charAt(j) - '0')) {
c = s.charAt(j);
index = j-1;
break;
}
}else{
if ((c - '0') > (s.charAt(j) - '0')) {
c = s.charAt(j);
index = j;
}
}
}
s = s.substring(0, index) + s.substring(index + 1, s.length());
}
System.out.println(s);
}
最简单的思路,只为求分!!!!!
public static void getMaxNum(int m,int n){
StringBuffer sb = new StringBuffer();
sb.append(m);
for(int j=0;j<n;j++){
for(int i=0;i<sb.length();i++){
if(i==sb.length()-1){
sb.deleteCharAt(i);
break;
}
if(sb.charAt(i)<sb.charAt(i+1)){
sb.deleteCharAt(i);
break;
}
}
}
System.out.println(sb.toString());
}