程序读取输入数据
c++
读取日期时间
#include
using namespace std;
int main() {
int year, month, day, hour, minute, second;
scanf("%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
printf("%d %d %d %d %d %d", year, month, day, hour, minute, second);
return 0;
}
以字符串形式读取所有输入
#include
using namespace std;
int main() {
string s;
while (true) {
string line;
if (!getline(cin, line)) break;
s += line + "\n";
}
cout << s << endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
读取一行未知个数的数据
#include
#include
#include
using namespace std;
int main() {
string line;
getline(cin, line);
stringstream ssin(line);
vector<int> res;
int x;
while (ssin >> x) res.push_back(x);
for (auto x : res) cout << x << ' ';
cout << endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
读取行数列数未知的数据
#include
#include
using namespace std;
const int N = 1010;
int n, m;
int g[N][N];
int main() {
string line;
while (getline(cin, line)) {
n++, m = 0;
stringstream ssin(line);
int x;
while (ssin >> x) g[n][++m] = x;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cout << g[i][j] << ' ';
cout << endl;
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32