0

私は問題解決に不慣れです。そして、私はエクスプレッションと呼ばれるUVAの問題を解決していました。私のコードはすべての可能なテストケースに対して正しい出力を提供するので、私は問題を解決したと思います。しかし、それでも私はWAを取得しています。どこかで正しく行っていない改行を印刷したようです。この問題は、「出力ファイルには、各接尾辞式がすべて1行に含まれます。異なる式の間に空白行を出力します」というものです。誰かが私にこれを少しはっきりと説明してもらえますか?グループで質問しましたが、回答がありません。そして、以前の議論も役に立たなかった。前もって感謝します。

#include<iostream>
#include<map>
#include<stack>
#include<vector>
#include<cstdio>

using namespace std;

void push_into_stack(char c, vector< char > &ans, stack< char > &st);
void work_with_stack(vector< char > &ans, stack< char > &st);

int main(void)
{
    freopen("input.txt", "r", stdin);
int t;
char dummy;
cin >> t;

for(int i=1; i<=t; i++)
{
    vector< char > exp, ans;
    stack< char > st;
    char c;

    while(cin >> c)
        exp.push_back(c);

    for(int i=0; i<exp.size(); i++)
        if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/') push_into_stack(exp[i], ans, st);
        else if(exp[i]=='(') st.push(exp[i]);
        else if(exp[i]==')') work_with_stack(ans, st);
        else ans.push_back(exp[i]);

    while(!st.empty())
    {
        ans.push_back(st.top());
        st.pop();
    }

    for(int i=0; i<ans.size(); i++)
        cout << ans[i];
    cout << endl;
}
return 0;

}

void push_into_stack(char c, vector< char > &ans, stack< char > &st)
{
    map< char, int > mp;
    mp['/']=2;
    mp['*']=2;
    mp['+']=1;
    mp['-']=1;

while(true)
{
    if(!st.empty() && mp[c]<=mp[st.top()])
    {
        ans.push_back(st.top());
        st.pop();
    }
    else
    {
        st.push(c);
        break;
    }
}
return;

}

void work_with_stack(vector< char > &ans, stack< char > &st)
{
    while(true)
    {
        if(st.top()=='(') break;
        ans.push_back(st.top());
        st.pop();
    }
    st.pop();
    return;
}
4

1 に答える 1

1

ええと...答えの質は質問の質を反映することしかできないと思います...しかしどうですか:

int main(void) {
   char postfixone[] = "4 5 7 2 + - *            -16";
   char postfixtwo[] = "3 4 + 2  * 7 /             2";
   char postfixthree[] = "5 7 + 6 2 -  *            48";
   printf("%s\n\n",postfixone);
   printf("%s\n\n",postfixtwo);
   printf("%s\n\n",postfixthree);
}

mike@linux-4puc:~> ./a.out 
4 5 7 2 + - *            -16

3 4 + 2  * 7 /             2

5 7 + 6 2 -  *            48

それぞれが新しい行を挟んで一行に並んでいます...

編集: C++を使用していて、ここに行を印刷していると思います:

for(int i=0; i<ans.size(); i++)
     cout << ans[i];
 cout << endl; 

endl を使用して、接尾辞ごとに 1 つの新しい行を印刷しています。試してください:

 cout << endl << endl;

代わりに、余分な空白を行間に挿入します。

于 2012-09-24T19:56:40.933 に答える