#22317: c++WA 找不到問題點


fdhs107cyprinoid (unknown)

學校 : 桃園市私立復旦高級中學
編號 : 80314
來源 : [39.9.193.199]
最後登入時間 :
2024-12-24 15:33:31

#include <iostream>
using namespace std;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, x;
    string str, s;
    while (cin >> n >> m && n != 0)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> s;
            str += s;
        }
        for (int i = 0; i < m; i++)
        {
            cin >> x;
            cout << str[x - 1];
        }
        cout << '\n';
    }
}
#22338: Re:c++WA 找不到問題點


cstandy (小鹿)

學校 : 國立成功大學
編號 : 124252
來源 : [220.133.37.215]
最後登入時間 :
2020-12-23 15:49:38

#include 
using namespace std;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, x;
    string str, s;
    while (cin >> n >> m && n != 0)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> s;
            str += s;
        }
        for (int i = 0; i < m; i++)
        {
            cin >> x;
            cout << str[x - 1];
        }
        cout << '\n';
    }
}
 
====
 
你的 str 沒有清空,第二行就會錯了。要加 str.clear();。