水题
1.搜索链表
http://oj.jxust.edu.cn/contest/problem?id=1644&pid=1
Problem Description
从键盘上输入n个学生的信息:学号,姓名, 数学, 英语, 语文的成绩,用这些信息构建一个动态链表。再从键盘上输入一个学号, 用顺序查找的方法在链表中查找该学生的信息。若找到,输出该学生的信息,没有找到,输出No such person
Input
输入学生的信息:学号,姓名, 数学, 英语, 语文的成绩, 输入学号为0时终止输入;
再输入学号进行查询.
Output
若找到,输出该学生的信息,成绩保留一位小数,没有找到,输出No such person
Sample Input
20130610 Guojin 90.5 89.5 70
20140612 Huangrong 99.5 100 90.5
20150548 HuangYaoshi 89 78 56
0 0 0 0 0
20140612
Sample Output
20140612 Huangrong 99.5 100.0 90.5
开始用scanf和printf输入输出老错,我日,后来改成cin,cout输入输出就过了;
补充一个知识点,用cout输出浮点数(要求浮点数保留小数点后一位)方法:
1 #include <iomanip> 2 cout<<setiosflags(ios::fixed)<<setprecision(1);
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#define N 30
#include <iostream>
#include <iomanip>
using namespace std;
struct node{
string sno;
string sname;
double math;
double english;
double chinese;
}student[N];
int main()
{
string mysno,mysname;
double mymath,myenglish,mychinese;
int i=0,j;
while(cin>>mysno>>mysname>>mymath>>myenglish>>mychinese,(mysno!="0"||mysname!="0"||mymath!=0||myenglish!=0||mychinese!=0))
{
student[i].sno=mysno;
student[i].sname=mysname;
student[i].math=mymath;
student[i].english=myenglish;
student[i].chinese=mychinese;
i++;
}
int num=i;
cin>>student[i].sno;
for(j=0;j<num;j++)
{
if(student[j].sno==student[i].sno)
{
cout<<setiosflags(ios::fixed)<<setprecision(1);
cout<<student[j].sno<<" "<<student[j].sname<<" "<<student[j].math<<" "<<student[j].english<<" "<<student[j].chinese;
break;
}
}
if(j>=num) printf("No such person\n");
return 0;
}
天晴了,起飞吧

浙公网安备 33010602011771号