1、输入学生信息,姓名 成绩(成绩的数目不一定)
输出每个学生的学号和平均成绩,以及不及格课程数超过2的学生,按不及格课程数从大到小排好序输出。

#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct node
{
string name;
int grade[100];
int unsix;
double avg;
int g; //有g科
}student;
student stu[100];
bool cmp(student a,student b)
{
return a.unsix>b.unsix;
}
int main()
{
int i=0;
int N=0;
string ss[200];
while(getline(cin,ss[N])&&ss[N]!="")//读取字符串
N++;
for(int i=0;i<N;i++)
{
string str=ss[i];
int len=str.length();
int have_name=0; //是否读取过名字
string temp;
int g=0,k=0;
int start=0;
for(int j=0;j<len;j++)
{
if(have_name==0&&str[j]==' ') //读取名字
{
stu[i].name=str.substr(start,k);
k=0;
temp="";
start=j+1;
have_name=1;
}
else if(have_name==1&&str[j]==' ') //读取分数
{
int gra=0;
for(int l=0;l<k;l++)
gra=gra*10+temp[l]-'0';
stu[i].grade[g]=gra;
k=0;
temp="";
g++;
}
if(str[j]!=' ')
{
temp[k]=str[j];
k++;
}
stu[i].g=g;
}
int gra=0; //最后一科
for(int l=0;l<k;l++)
gra=gra*10+temp[l]-'0';
stu[i].grade[g]=gra;
stu[i].g++;
for(int p=0;p<stu[i].g;p++) //计算平均成绩,不及格成绩数
{
stu[i].avg+=stu[i].grade[p];
if(stu[i].grade[p]<60)
stu[i].unsix++;
}
stu[i].avg/=double(stu[i].g);
}
for(int i=0;i<N;i++) //输出姓名+学生均绩
cout<<stu[i].name<<" "<<stu[i].avg<<endl;
sort(stu,stu+N,cmp); //按照不及格数目排序
cout<<"不及格课程数超过2的学生:"<<endl;
for(int i=0;i<N;i++)
{
if(stu[i].unsix>2)
{
cout<<stu[i].name;
cout<<" "<<stu[i].unsix<<endl; //输出不及格科数
}
}
return 0;
}
2、输入字符串,输出字符串中包含的数字,比如 2.3ABC0-2.3 输出 2.3 0 -2.3。
注意一些特殊的情况如+004.500值为+4.5。
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str;
cin>>str;
string temp;
int temp_len=0;
int dot_flag=0;
int ok=0;
int len=str.length();
for(int i=0;i<=len;i++)
{
if(i==len)
{
if(dot_flag==1)
while(temp[temp_len-1]=='0')
temp_len--;
for(int j=0;j<temp_len;j++)
cout<<temp[j];
if(temp_len==0)
cout<<"0";
cout<<endl;
}
else if(str[i]=='+'||str[i]=='-')
{
if(ok)
{
if(dot_flag==1) //去掉小数点后面的不必要的0
while(temp[temp_len-1]=='0')
temp_len--;
for(int j=0;j<temp_len;j++)
cout<<temp[j];
cout<<" ";
ok=0;
dot_flag=0;
}
else
cout<<"0 ";
temp_len=1;
temp[0]=str[i];
}
else if(str[i]>'0'&&str[i]<='9')
{
ok=1;
temp[temp_len]=str[i];
temp_len++;
}
else if(str[i]=='.')
{
if(temp_len==0||(temp_len!=0&&(temp[temp_len-1]=='+'||temp[temp_len-1]=='-')))
{
temp[temp_len]='0';
temp_len++;
}
temp[temp_len]=str[i];
temp_len++;
dot_flag=1;
}
else if(str[i]=='0')
{
if(ok||str[i+1]=='.')
{
ok=1;
temp[temp_len]=str[i];
temp_len++;
}
}
else //非数字类
{
if(ok)
{
if(dot_flag==1)
while(temp[temp_len-1]=='0')
temp_len--;
for(int j=0;j<temp_len;j++)
cout<<temp[j];
cout<<" ";
ok=0;
dot_flag=0;
}
temp_len=0;
}
}
return 0;
}
本文介绍了一种算法,用于处理学生信息,包括姓名、成绩,并计算平均成绩及不及格科目数量,同时对输入的字符串进行处理,提取并标准化其中的数字。此算法能够有效处理大量数据,提供学生表现的综合分析。
1万+

被折叠的 条评论
为什么被折叠?



