1
4 2
1 2
1 3
1
方案,
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int Q;
cin >> Q; //Q=2
while(Q--)
{
int N, E;
cin >> N >> E; // N=4, E=2
while(1)
{
string edge;
getline(cin,edge); //edge should store "1 2" for the first iteration and "1 3" for the second iteration.
cout << edge << " " << edge.size() << endl;
vector<int> adjList[N];
if(edge.size()>1)
{
int u = stoi(edge.substr(0,1));
int v = stoi(edge.substr(2,1));
for(int i=0; i<E; ++i)
{
adjList[u].push_back(v);
adjList[v].push_back(u);
}
}
else
break;
}
}
return 0;
}
使輸出
0
程序應該打印出來,
1 2 3
1 3 3
1 1
爲什麼我不是S顛倒字符串中的任何值邊緣?我認爲這是導致問題的功能getline()
。是否還有其他函數可以將整行作爲一個字符串(包括空格),直到達到換行符爲止?
如果您打算使用慣用的C++,您應該避免使用C標準庫頭文件(即'cstdio')。由於您已經在使用C +的'',對'cstdio'沒有太大的需求。 –
Dai