我在Java中創建了這個程序,但我在C++中是相當新的,並且使用字符串是不可行的。我想知道是否有人能指出我的方向,以便將該程序「翻譯」爲C++。只是如何使用字符串,也許你在我的代碼中看到的值得一提的東西。注意:我並不是要求你們中的任何一位進行直接翻譯,只需要我提供信息,代碼僅供參考。在C++中使用字符串
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class DragonCurve{
public static void main(String[] args){
try{
Scanner sc = new Scanner(System.in);
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int n;
while((n = Integer.parseInt(sc.nextLine()))!=-1){
log.write(dragon(n)+"\n");
}
log.flush();
}catch(Exception e){
e.printStackTrace();
}
}
public static String dragon(int n){
if(n>2){
String aux = dragon(n-1);
return aux+"L"+reverse(aux);
}else if(n==2)
return "LLR";
else if(n==1)
return "L";
else if(n==0)
return "";
return "";
}
public static String reverse(String aux){
String ans = "";
for(int i = aux.length();i>0;i--){
if(aux.charAt(i-1)=='L')
ans+='R';
else
ans+='L';
}
return ans;
}
}
C++是與java不同的語言。代碼轉換不是微不足道的!你不能要求我們提供這種情況下的代碼plz_。試試自己的拳頭,並在遇到困難時詢問特定問題。 – 2014-09-23 04:49:44
我相信[此頁上的示例](http://en.cppreference.com/w/cpp/string/basic_string/operator%2B)和[本頁](http://en.cppreference.com/w/) cpp/string/basic_string/operator_ltltgtgt)可以幫助你。如果您仍然不明白某些事情,請隨時提出具體問題。編譯示例程序,並增加你的東西應該幫助。 – 2014-09-23 04:50:02
我其實並沒有要求你翻譯,而是告訴我去哪裏看,因爲我一直在試圖自己做,只是感到困惑。我發佈了代碼僅供參考。對不起,如果它看起來像我只是要求你爲我做。 @MohitJain謝謝,我會看看併發布我的代碼,如果我有任何問題。 – Darwin57721 2014-09-23 04:58:29