题解 | #学生基本信息输入输出#
学生基本信息输入输出
https://www.nowcoder.com/practice/58b6a69b4bf943b49d2cd3c15770b9fd
#include <iostream> #include<cmath> #include <sstream> #include<iomanip> #include <vector> using namespace std; int main() { string str; // 学生ID和成绩输入字符串 getline(cin, str); // 从标准输入获取学生ID和成绩输入字符串 stringstream ss(str); // 将输入字符串转换为流 int stu_id; // 学生ID vector<double> scores(3); // 学生各科成绩,长度为3 ss >> stu_id; // 从流中获取学生ID for (int i = 0; i < 3; i++) { // 循环三次,获取各科成绩 ss.ignore(); // 忽略流中的换行符 ss >> scores[i]; // 从流中获取各科成绩,并保存到成绩数组中 scores[i] = round(scores[i] * 100) / 100.0; // 四舍五入保留2位小数 } cout << fixed << setprecision(2) << "The each subject score of No. " // 输出学生ID和各科成绩 << stu_id << " is " << scores[0] << ", " << scores[1] << ", " << scores[2] << "." << endl; return 0; }