题解 | #学生基本信息输入输出#-输入输出分号和逗号的处理
学生基本信息输入输出
https://www.nowcoder.com/practice/58b6a69b4bf943b49d2cd3c15770b9fd
C++的写法
分号和逗号使用了一个char类型的字符串来输入。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long int number;
float a, b, c;
char ch;
cin >> number >> ch >> a >> ch >> b >> ch >> c;
cout << fixed;
cout << "The each subject score of No. " << number << " is " << setprecision(2) << a << ", " << b << ", " << c << ".";
return 0;
}
C的写法
C的写法要简洁很多。对于引号和逗号的处理不需要额外定义。
#include <cstdio>
using namespace std;
int main() {
long int number;
float a, b, c;
scanf("%ld;%f,%f,%f",&number,&a,&b,&c);
printf("The each subject score of No. %ld is %.2f, %.2f, %.2f.", number, a, b, c);
return 0;
}
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路
小天才公司福利 1166人发布
查看19道真题和解析

