在一行上输入一个整数
与三个小数点后位数不超过
位浮点数
,表示学生学号、三科成绩。其中,学号和成绩之间用英文分号隔开,成绩之间用英文逗号隔开。
在一行上输出格式化后的学号与三科成绩,您可以查看样例得到更具体的说明。
17140216;80.845,90.55,100.00
The each subject score of No. 17140216 is 80.85, 90.55, 100.00.
123456;93.33,99.99,81.20
The each subject score of No. 123456 is 93.33, 99.99, 81.20.
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//使用;或,分隔输入数据
String[] data = scanner.nextLine().split("[;,]");
int id=Integer.parseInt(data[0]);
double score1=Double.parseDouble(data[1]);
double score2=Double.parseDouble(data[2]);
double score3=Double.parseDouble(data[3]);
System.out.println("The each subject score of No. "+id+" is "+String.format("%.2f",score1)+", "+String.format("%.2f",score2)+", "+String.format("%.2f",score3)+".");
}
}
#include
(849)#include
#include
using namespace std;
int main(){
double a,b,c;
int no;
scanf("%d;%lf,%lf,%lf",&no,&a,&b,&c);
a = (int)(a*100+0.5)/100.0;
b = (int)(b*100+0.5)/100.0;
c = (int)(c*100+0.5)/100.0;
printf("The each subject score of No. %d is %.2lf, %.2f, %.2f.",no,a,b,c);
return 0;
}
#include <math.h>
#include <stdio.h>
int main()
{
int SuID = 0;
float C = 0;
float math = 0;
float Eg = 0;
scanf("%d;%f,%f,%f",&SuID,&C,&math,&Eg);
printf("The each subject score of No. %d is %.2f, %.2f, %.2f.",SuID,round(C * 100) / 100,math,Eg);
return 0;
}
//C语言成绩这里因为浮点数存储有不准确性,所以有概率出现四舍五入不对的情况,这个时候要用round(变量 * 100) / 100 来让浮点数变得具有准确性 #include<stdio.h>
struct Student
{
char num[9];
float arr[3];
};
int main()
{
struct Student st1;
int j=0;
char ch=0;
while((ch=getchar())!=';')
{
st1.num[j]=ch;
j++;
}
int i=0;
while(i<3)
{
scanf("%f",&st1.arr[i]);
getchar();
i++;
}
printf("The each subject score of No. %s is %.2f, %.2f, %.2f.",st1.num,st1.arr[0],st1.arr[1],st1.arr[2]);
return 0;
} 为啥输入 16;0,100,0#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int num;
float score1, score2, score3;
cin >> num;
getchar();
cin >> score1;
getchar();
cin >> score2;
getchar();
cin >> score3;
cout << "The each subject score of No. " << num << " is " << setiosflags(ios::fixed) << setprecision(2) << score1 << ", " << setiosflags(ios::fixed) << setprecision(2) << score2 << ", " << setiosflags(ios::fixed) << setprecision(2) << score3 << "." << endl;
return 0;
} #include<stdio.h>
struct xingxi
{
int xuehao;
float xueke1;
float xueke2;
float xueke3;
};
int main()
{
int a=0;
float b=0;
float c=0;
float d=0;
scanf("%d;%f,%f ,%f",&a,&b,&c,&d);
struct xingxi p={.xuehao=a,.xueke1=b,.xueke2=c,.xueke3=d};
printf("The each subject score of No. %d is %.2f, %.2f, %.2f.",p.xuehao,p.xueke1,p.xueke2,p.xueke3);
return 0;
} 简单问题复杂化,结构体其实也可以尝试用一下结构体来写。
#include <stdio.h>
int main()
{
struct student
{
int num;
float c_score;
float m_score;
float e_score;
}stu;
scanf("%d;%f,%f,%f",&stu.num,&stu.c_score,&stu.m_score,&stu.e_score);
printf("The each subject score of No. %d is %.2f, %.2f, %.2f.",stu.num,stu.c_score,stu.m_score,stu.e_score);
return 0;
}