2012-03-02 8 views
1

私は http://www.spoj.pl/problems/HELLOKIT/C++プログラムを最適化

明らかにここに記載されているこの問題に近づいたために検討されるように、それが受け入れられていません。私は次のコードを最適化するために何をすべきか知りたい。ロジックはシンプルですが、なぜ私はいつもシンプルな問題のために長いプログラムを書くことに終わります。どうもありがとう。私はあなたが良いと堅牢なプログラムを書くのを手伝ってくれることを願っています。ここ はコード

#include<iostream> 
#include<stdio.h> 
#include<string.h> 
#include<conio.h> 


using namespace std; 

void rotate(string a , int b){ 

string newstring; 
bool result=true; 
char displaystr[50],displaystr2[50],sourcestring[50]; 

    for(int i=0;i<b;i++){ 
    newstring = newstring.append(a);//Append the string with itself b times 
    } 
    cout<<newstring<<endl; 
    int length = newstring.length() ; 
for(int l=0;l<length;l++){ 
     sourcestring[l]=newstring[l]; //copy the string to a char* 
    } 
    int index=0,counter=1;//counter keeps track of changing source string 
    while(result){ 
    index=0; 
    while(index<length-1){ 
    if(counter==1){ 
        displaystr[index] = sourcestring[index+1];//rotating the string 
        displaystr[length-1] = sourcestring[0]; 
        index++; 
    } 
    else{ 
        displaystr2[index] = displaystr[index+1];//rotatinsg when source change 
        displaystr2[length-1] = displaystr[0]; 
        index++; 
    } 
    } 
if(counter>1){ 
     for(int i=0;i<length;i++) 
     displaystr[i]=displaystr2[i]; 
} 
counter++; 
if(strncmp(displaystr,sourcestring,length)==0){ 
result=false; 
return; 
} 
for(int i=0;i<length;i++) 
cout<<displaystr[i]; 
cout<<endl; 
} 
int main(){ 

int testcases=0; 
string key; 
int num=0; 
string keyarr[10]; 
int numarr[10]; 
int i=0,j=0; 
while(testcases<10){ 
getline(cin,key); 
if(key==".") 
break; 
cin>>num; 
    cin.ignore(1024, '\n'); 

keyarr[i]=key; 
numarr[i]=num; 
i++; 
testcases++; 

} 

for(int j=0;j<i;j++){ 

    rotate(keyarr[j],numarr[j]); 
    } 
getch(); 
return 0; 

} 

の話コードサンプルIAMはこの

#include<iostream> 
#include<stdio.h> 
#include<string> 
#include<conio.h> 


using namespace std; 

void rotate(string a , int b){ 
string newstring,displaystr; 
for(int i=0;i<b;i++){ 
    newstring = newstring.append(a); 
    } 

    int length = newstring.length() ; 
    int index=0; 

while(index<length-1){ 

        displaystr[index] = newstring[index+1]; 
        index++; 

    } 
     displaystr[length-1]=newstring[0]; 
     cout<<displaystr; 


} 

int main(){ 
rotate("love",2); 
getch();  
return 0; 
} 
+0

私が気づくいくつかの事柄:私は、関連するコードが10行(max!)を超えず、入力ルーチンも多くと期待しています。なぜあなたのコードはずっと長くなっていますか?また、なぜあなたは固定サイズのバッファを使用していますか?私はそれ以上正直ではなかった。 –

+5

これはcodereviewsに属すると思います。 –

+0

コードをよりよくフォーマットしてください。 – m0skit0

答えて

1

優れた私は手で(関係なく、タスクの見ることができる問題とどのように多くの行それが必要か、いけないですあなたがコードを書くときにインデントを使う必要があるということです。混乱したコードを記述するのは簡単です。字下げは、整理され、重要かつ読みやすいようにします。これを行うだけで、コードがより効率的になる方法に驚くでしょう。

このように言えば、プログラミングは他のどのようなスキルでもあります。あなたは練習を続ける必要があり、勉強をしてスキルを最新に保つ必要があります。チュートリアル、書籍、他の仲間のプログラマーなどから新しい概念を学ぶ。課題を取り上げ、単一の問題に対する異なる(創造的な)アプローチを考えよう。これは、効率的な方法を見つけるのを助けることができる。仕事をすれば、ますます効率の良いコードを書くことになるでしょう。練習、練習、練習、そしてあなたの間違いから学ぶ。

+0

良いヒント。特にコードが実際に閉じ括弧を欠いているためです。 –

+0

アドバイスありがとうございました。また、Iamは文字列を扱う際にいくつかの問題があります。以前は、私は固定サイズのバッファを使用していないとき、私は文字列を使用していた。しかし、彼らは私がそれらを実行すると、悪いptrエラーを表示していた。それについて何か言いたいことがありますか? – YuNo

+0

こんにちは、私はそれを見て固定しました。 – YuNo