依次输入一个学生的学号,以及3科(C语言,数学,英语)成绩,在屏幕上输出该学生的学号,3科成绩(注:输出成绩时需进行四舍五入且保留2位小数)。
数据范围:学号满足 ,各科成绩使用百分制,且不可能出现负数
学号以及3科成绩,学号和成绩之间用英文分号隔开,成绩之间用英文逗号隔开。
学号,3科成绩,输出格式详见输出样例。
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<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; }
#include<stdio.h> int main(void){ int student_number; // 用来存储学号 float score1, score2, score3; // 分别用来存储输入的每科的成绩 // 按照题目的实例中的输入来编写双引号里面的内容 scanf("%d;%f,%f,%f", &student_number, &score1, &score2, &score3); // 复制题目中的实例,然后修改数字为对应的格式输出字符,printf里填写必要的参数即可 printf("The each subject score of No. %d is %.2f, %.2f, %.2f.", student_number, score1, score2, score3); return 0; }1.变量定义,整型变量定义,浮点型变量定义,2.scanf输入语句的熟悉,3.%.2f中.2的含义是保留2位有效数字
from decimal import Decimal s = input() numb = s.split(';')[0] a,b,c = map(float,s.split(';')[1].split(',')) print('The each subject score of'\ ' No. %s is %.2f, %.2f, %.2f.' % (numb,Decimal(a+0.000000001).quantize(Decimal("0.01"), rounding = "ROUND_HALF_UP"), Decimal(b+0.000000001).quantize(Decimal("0.01"), rounding = "ROUND_HALF_UP"), Decimal(c+0.000000001).quantize(Decimal("0.01"), rounding = "ROUND_HALF_UP")))