程序读取输入数据

程序读取输入数据

c++

读取日期时间

#include <iostream>

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;
}
/*
2021-02-21 01:02:03
*/

以字符串形式读取所有输入

#include <iostream>

using namespace std;

int main() {
    
    string s;
    while (true) {
        string line;
        if (!getline(cin, line)) break;
        s += line + "\n";
    }
    
    cout << s << endl;
    
    return 0;
}
/*
#include <iostream>

using namespace std;

int main() {

    cout << "hello world" << endl;

    return 0;
}
*/

读取一行未知个数的数据

#include <iostream>
#include <vector>
#include <sstream>

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
*/

读取行数列数未知的数据

#include <iostream>
#include <sstream>

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
*/

版权声明:本文为weixin_42638946原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。