看板 C_Sharp 關於我們 聯絡資訊
public class Solution { public string ToGoatLatin(string S) { String [] words = S.Split(' '); List<char> vowel = new List<char>() { 'a','e','i','o','u','A','E','I','O','U' }; StringBuilder ans = new StringBuilder(); StringBuilder a = new StringBuilder(); foreach(string s in words) //s 正常 { List<char> change_word = s.ToList(); if (vowel.IndexOf(change_word[0])==-1) { change_word.Insert(change_word.Count,change_word[0]); change_word.RemoveAt(0); } a.Append("a"); ans.Append(" " + change_word.ToString() + "ma" + a); } return ans.Remove(ans.Length," " ).ToString() ; } } 目前COMPLIER 是過的,但怎麼修改答案都是 " System.Collections.Generic.List`1[System.Char]maa System.Collections.Generic.List`1[System.Char]maaa System.Collections.Generic.List`1[System.Char]maaaa System.Collections.Generic.List`1[System.Char]maaaaa" 但若以input "I speak Goat Latin" 為例,應該是要為 「 Imaa peaksmaaa oatGmaaaa atinLmaaaa」 第一次在板上發文,請各位大大解惑 感謝<(__ )> --   有一個香錦囊,是從一個神話般的守軍的血屍頂上剝下的。那一次我們部隊遭受從未 有過的頑強抵抗,我們犧牲了三個艦隊,一個裝甲師和無以數計小組推進的敢死排,才摧 毀了那處隘口的碉堡。但是竟然發現,使我們遭受如此慘烈傷亡的守軍,總數只有一人。   士兵們起鬨地在他胸前發現這枚香袋,大家都相信這是一枚具有神奇力量的護身符。 我們把他的頭顱砍斷,取下香袋,剝開,   裡面一張被血浸紅的宣紙竟用漢字娟娟秀秀四個整齊的楷書寫著-「盼君早歸。」 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.13.112.92 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1525139205.A.F85.html
Litfal: 沒寫題目是要擲盃喔... 05/01 12:08
原本只是認為我單純詢問為何這段CODE會產生這個問題,所以沒打算貼題目出來 只不過依您要求 題目: #824 Goat Latin 難度:easy A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows: If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word. For example, the word 'apple' becomes 'applema'. If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma". For example, the word "goat" becomes "oatgma". Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1. For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on. Return the final sentence representing the conversion from S to Goat Latin. 簡單來說: 1. Input string , return string 2. If words' first char is a,e,i,o,u ,then append "ma" after the word 3. If words' first char isnt a,e,i,o,u , then the first char put the words end. 4. increase 'a' per word and put it to the end word .
t64141: System.Collections.Generic.List`1[System.Char]這是你 05/01 13:13
t64141: 把整個list丟進Console.WriteLine()印出來的吧? 05/01 13:14
t64141: ex: Console.WriteLine(list), list是一個List<Char>物件 05/01 13:15
先感謝您的回覆,這題目前已經用別的寫法解出來了 只不過還是好奇錯在哪 我跟你的想法一樣,他直接把我 list這個 object 給印出來 但我用 List.ToString()、 Array.ToString() 應該是直接把List內的value 給變成ToString() 才對呀@@ 為何會變成單純show list 而已?? ※ 編輯: ken52011219 (101.13.112.92), 05/01/2018 13:31:21
t64141: 如果是的話,可能就是Console.Write()的參數放錯 05/01 13:17
※ 編輯: ken52011219 (101.13.112.92), 05/01/2018 13:37:21
t64141: List<>好像沒有實作ToString(),所以你的情境他做的是 05/01 13:40
t64141: Object.ToString() 05/01 13:40
t64141: 根據https://referencesource.microsoft.com/ 05/01 13:41
t64141: object.ToString()是回傳GetType().ToString(); 做的是 05/01 13:42
t64141: Type.ToString() 05/01 13:43
t64141: Type.ToString()找一下MSDN可以發現就是印出型別 05/01 13:45
t64141: 平時也沒記這麼細節的地方,剛剛臨時去查的,看看就好 05/01 13:46
t64141: 123.ToString()能正確轉成字串是因為Int32.ToString()覆寫 05/01 13:49
t64141: 了Object.ToString(),所以才能直覺的數字轉字串...吧 05/01 13:50
剛剛稍微在網路搜尋印證了t大的說法 List.ToString() 是 Object.ToString() 沒錯 ,非常感謝解惑<(_ _)> ※ 編輯: ken52011219 (101.13.112.92), 05/01/2018 13:59:06