Re: [問題] java程式碼轉c++

看板C_and_CPP (C/C++)作者 (null)時間14年前 (2011/10/02 23:37), 編輯推噓1(107)
留言8則, 4人參與, 最新討論串2/4 (看更多)
※ 引述《jolinsens (cherry花)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : borland c++ : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 問題(Question): : java中的程式碼想轉成c++ : 但部份語法不熟,請求協助 : 餵入的資料(Input): : 預期的正確結果(Expected Output): : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : import java.util.*; : public class strsorting{ : String converMachNo(String sMachs) : { String [] temp = sMachs.split(","); 把 sMachs 以『,』為分隔字元切開 : ArrayList<String> forsorting = new ArrayList<String>(); 準備一個 list 來放運算結果 : for(int i = 0; i < temp.length;i++) : forsorting.add(temp[i]); 把第 i 個 token 放到 list : Collections.sort(forsorting); 排序 list : temp = forsorting.toArray(new String[0]); 重新指定 temp 的內容為 list 轉成陣列 : int num1, num2, count =1; : sMachs = temp[0]; 重新指定 sMachs 內容為 temp[0] 的字串 : num1 = Integer.parseInt(temp[0].substring(3)); 取 temp[0] index 3 之後的字串,並轉成 Integer : for(int i = 1; i <temp.length; i++) : { : num2 = Integer.parseInt(temp[i].substring(3)); 取 temp[i] index 3 之後的字串,並轉成 Integer : if((num2 - num1) != 1) : { if(count!= 1) : sMachs +=("-"+temp[i-1]+","+temp[i]); else sMachs += (","+temp[i]); : count = 0; : } : num1 = num2; : count++; : } : if(count!= 1) : sMachs += ("-"+temp[temp.length-1]); : return sMachs; } : 補充說明(Supplement): 這什麼鬼東西qq 作者的思路好亂呦。 不值得『直譯』,建議還是問 input/output spec 重寫吧。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.231.54.232

10/02 23:41, , 1F
我也是看到if((num2 - num1) != 1)之後,就在想它在做什
10/02 23:41, 1F

10/02 23:41, , 2F
麼?
10/02 23:41, 2F

10/02 23:42, , 3F
假如可以看到輸入的string是什麼,那邊應該比較好解決一
10/02 23:42, 3F

10/02 23:43, , 4F
點.
10/02 23:43, 4F

10/03 00:18, , 5F
嗯,我是覺得排版亂無妨。寫作思路不能亂xd
10/03 00:18, 5F

10/03 01:10, , 6F
我猜的..應該是抓時間.日期差1的,抓出來統計,所以看起
10/03 01:10, 6F

10/03 01:11, , 7F
來很沒邏輯性
10/03 01:11, 7F

10/03 11:56, , 8F
只能說qrtt1真是佛心來了
10/03 11:56, 8F
文章代碼(AID): #1EY8L0xl (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1EY8L0xl (C_and_CPP)