日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/4 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 英雄会在线编程题目(请大家不吝赐教) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<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的地方已經小于3continue;}}}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>

總結

以上是生活随笔為你收集整理的英雄会在线编程题目(请大家不吝赐教)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。