[英]my c++ code does not let me input all input values, and i don't know why
using namespace std;
int main() {
int t;
cin >> t;
for (int j = 0; j < t; j++) {
int n;
cin >> n;
vector<string> cities;
for (int i = 0; i < n; i++) {
cin >> cities[i];
}
int size = cities.size();
for (int i = 0; i < n; i++) {
for (int k = 1; k < n; k++) {
if (cities[i] == cities[k]) size--;
}
}
cout << size << '\n';
}
}
So, this task is from kattis website(link: https://open.kattis.com/problems/everywhere ) and the goal is to get the number of the cities someone has visited, the problem is that some of these cities were visited twice or even more, so the only thing we need is to get the number of cities.因此,此任务来自 kattis 网站(链接: https://open.kattis.com/problems/everywhere ),目标是获取某人访问过的城市数量,问题是访问过其中一些城市两倍甚至更多,所以我们唯一需要的是获得城市的数量。
That's how input looks like:这就是输入的样子:
7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton
And my code doesn't let me input the whole input, I mean, I input 3 values and then program stops working.而且我的代码不允许我输入整个输入,我的意思是,我输入了 3 个值,然后程序停止工作。 Can you help me?
你能帮助我吗?
You forgot to initialize your vector with a size.您忘记使用大小初始化向量。 You can do it by passing
n
to the constructor:您可以通过将
n
传递给构造函数来做到这一点:
vector<string> cities(n);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.