英雄会在线编程题目(请大家不吝赐教)

本文探讨了一个在线编程题目,涉及数列操作的优化策略。通过限制操作步骤和数列元素调整,目标是最大化操作次数。文章详细解释了输入输出格式,并分享了一种算法实现方式,尽管当前解决方案在某些情况下未能达到预期效果。
<span style="font-size:18px;">最近看了一道英雄会在线编程题目,题目的介绍如下:</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">题目详情:</span>
<span style="font-size:18px;"></span><p style="font-family: Simsun;font-size:14px;">有一个数列,所有的数都是非负整数,你可以进行如下方式进行一次操作(注意一次完整的操作必须先后完成如下两个步骤):</p><p style="font-family: Simsun;font-size:14px;">(1) 任选一个不小于3的数,把它减少3。</p><p style="font-family: Simsun;font-size:14px;">(2) 任选一个数把它增加1。</p><p style="font-family: Simsun;font-size:14px;">请问,最多能够操作多少次?</p><p style="font-family: Simsun;font-size:14px;">输入格式:</p><p style="font-family: Simsun;font-size:14px;">多组数据,每组数据第一行是一个正整数n,表示数列中数的个数。(1<=n<=20000)</p><p style="font-family: Simsun;font-size:14px;">第二行包含n个空格分隔的非负整数,每个整数不超过1000000。</p><p style="font-family: Simsun;font-size:14px;">输出格式:</p><p style="font-family: Simsun;font-size:14px;">对每组数据输出一行,表示最多可以进行的操作次数。</p><div class="l3" style="font-family: Simsun;font-size:14px;">                答题说明</div><div class="l_c3" style="font-family: Simsun;font-size:14px;"><p>输入样例</p><p>1</p><p>10</p><p>2</p><p>10 11</p><p>输出样例:</p><p>4</p><p>10</p></div>    我想的是直接按照题目提供的算法来,这种方法很笨,但是我喜欢善始善终,尽然开始了,就尽量完成。现在程序能够正常运行,只是只能达到部分预期。只能计算出随机情况下的最大操作次数,无法计算出所有情况下最大的操作次数。此时又不能进行穷举法,走到这儿就觉得走不下去了,请大家不吝赐教。
<span style="font-size:18px;">package com.obob.forFun;

import java.util.ArrayList;
import java.util.Random;

public class Test {
	private static void calculate(ArrayList arr){
		int length=0;  //数组中数的个数
		ArrayList array=new ArrayList();  //相应数组
		int count=0;  //最多能进行的次数
		for(int i=0;i<arr.size()-1;i+=2){
			length=(int)arr.get(i);
			String val=arr.get(i+1).toString();
			String []valOneByOne=val.split(" ");
			for(int j=0;j<valOneByOne.length;j++){
				array.add(Integer.valueOf(valOneByOne[j]));
			}
				while(true){
					if(minusThree(array)){
						plusOne(array);
						count++;
					}
					else{
						System.out.println("第"+(i/2+1)+"条数组最多能操作的次数:    "+count);
						break;
					}
						
				}
		}
		return ;
	}
	private static boolean minusThree(ArrayList arr){
		Random random=new Random(System.currentTimeMillis());
		StringBuffer count=new StringBuffer(arr.size()); //计数器,记录在当前数组里查找的记录数
//		System.out.println("count capacity:"+count.capacity());
//		System.out.println("count length:"+count.length());
		for(int i=0;i<arr.size();i++){  
//			count.setCharAt(i, '0');
			count.append('0');
		}
			while(true){
				int ranInt=random.nextInt(arr.size());
				String s=count.toString();
				
				if(!s.contains("0")){
					return false;   //此时这个数组已经全部都小于3了,无法-3
				}
				if((int)arr.get(ranInt)>4){
					arr.set(ranInt, (int)arr.get(ranInt)-3);
					return true;
				}
				else{
					count.setCharAt(ranInt, '1');  //数组下标为ranInt的地方已经小于3
					continue;
				}
				
			}
	}
	private static void plusOne(ArrayList arr){
		Random random=new Random(System.currentTimeMillis());
		while(true){
			int ranInt=random.nextInt(arr.size());
			if((int)arr.get(ranInt)<1000000){
				arr.set(ranInt, (int)arr.get(ranInt)+1);
				break;
			}
			
		}
		
	}
	public static void main(String []args){
		ArrayList inputArray=new ArrayList();
		for(int i=0;i<10;i++){
			int j=0;
			inputArray.add(j++, 10); //数组第一位存储第二位的整数个数
			StringBuffer sbArray=new StringBuffer();
			String s="";
			Random random=new Random();
			for(int k=1;k<11;k++){
				sbArray.append(String.valueOf(random.nextInt(1000000)+1)+" ");
				/*if(k%10==0){
					System.out.println("\n");
				}*/
			}
			inputArray.add(j, sbArray);   //输入数组的初始化完成
		}
		calculate(inputArray);
	}
}
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值