Java贪心算法 删数字问题

问题描述:对给定的n位数字 指定要删除的数字个数 k ,要求删除这k个数之后 ,按照数字原左右顺序 新形成的数字最大

如输入: 5689   1    表示对于5689 删掉一个数字后    得到最大值       

应输出 : 689


贪心算法核心思想就是 :总是做出当前最好的选择    

那么要删除每一个数时    都从左侧第一个数(最高位)开始比较每两个数字的大小  如果左侧小于右侧  删除左侧数字    这样就保证了最高位上的数字总是一直在变大的   


比如  21965 这个数  删两位     第一次判断删掉1    第二次判断删除 2    得到结果最优 


贴出代码 :

public class 删数字问题 {


<span style="white-space:pre">	</span>private static int      k;
<span style="white-space:pre">	</span>private static int[]<span style="white-space:pre">	</span>data;
<span style="white-space:pre">	</span>private static int<span style="white-space:pre">	</span>length;


<span style="white-space:pre">	</span>public static void test() {
<span style="white-space:pre">		</span>for (int i = 0; i < k; i++) {
<span style="white-space:pre">			</span>boolean isDelete = false; // 判断是否可以覆盖当前数字的标志量
<span style="white-space:pre">			</span>for (int m = 0; m < length - 1; m++) {
<span style="white-space:pre">				</span>if ((data[m] < data[m + 1]) && !isDelete) {
<span style="white-space:pre">					</span>isDelete = true;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">				</span>if (isDelete) {
<span style="white-space:pre">					</span>data[m] = data[m + 1];
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>length--; // 不论这个数是不是在遍历的时候是否将isDelete变为true,
<span style="white-space:pre">				</span>      // 循环结束都得将这个长度减1   表示删去了最后的那个数     
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>for (int n = 0; n < length; n++) {
<span style="white-space:pre">			</span>System.out.print("" + data[n]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">		</span>data = new int[50];
<span style="white-space:pre">		</span>Scanner in = new Scanner(System.in);
<span style="white-space:pre">		</span>String data1 = in.nextLine();
<span style="white-space:pre">		</span>length = data1.length();
<span style="white-space:pre">		</span>System.err.println("length = " + length);
<span style="white-space:pre">		</span>k = in.nextInt();
<span style="white-space:pre">		</span>String[] data2 = data1.split("");
<span style="white-space:pre">		</span>for (int j = 0; j < length; j++) {
<span style="white-space:pre">			</span>data[j] = Integer.parseInt(data2[j]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>test();
<span style="white-space:pre">		</span>in.close();


<span style="white-space:pre">	</span>}
}


输入的是一个String    然后split  分割成String数组      然后 转为int[] 数组     用全局保存

删除k 个数  就建立一个 for循环,次数为k

内层循环为了遍历这个数    需要注意的是每次循环结束都需要  删去最后的数字   就是length--  ;

length是我设置的全局int  表示当前数组的长度   




  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值