一、剔除数字字符
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin,s);
for(char x : s)
{
if(x<'0'||x>'9')
cout<<x;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin,s);
int j=0;
for(int i=0;i<s.size();i++)
{
if(s[i]<'0'||s[i]>'9')
s[j++]=s[i];
}
s[j]='\0';
cout<<s;
return 0;
}
二、统计各位上等于b的数字数目
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
int a,b;
cin>>a>>b;
if(b<0||b>9)
cout<<"第二个参数越界"<<endl;
else
{
int res=0;
while(a)
{
if(a%10==b)
res++;
a/=10;
}
cout<<res<<endl;
}
}
return 0;
}
三、建立一个学生在某一个课程到课情况统计程序
#include<bits/stdc++.h>
using namespace std;
struct Student
{
string id;
int normal;
int late;
int absence;
int total;
double AttendRate;
};
void printStudentRate(Student student) {
printf("学号:%s, 出勤率:%.2lf\n", student.id.c_str(), student.AttendRate);
}
int main()
{
int n,totalcourse;
cin>>n>>totalcourse;
Student student[n];
for(int i=0;i<n;i++)
{
string id;
int normal,late,absence;
cin>>id>>normal>>late>>absence;
int total=totalcourse;
double attendrate=(double)(normal+late)/totalcourse;
student[i]={id,normal,late,absence,total,attendrate};
}
printStudentRate(student[0]);
for (int i = 0; i < n; i++) {
cout << "学号:" << student[i].id << ":" << endl;
cout << "总课时:" << student[i].total << endl;
cout << "正常:" << student[i].normal << endl;
cout << "迟到:" << student[i].late << endl;
cout << "旷课:" << student[i].absence << endl;
cout << "出勤率:" << student[i].AttendRate << endl;
double absentRate = (double) student[i].absence / totalcourse;
cout << "旷课率:" << absentRate << endl;
puts("---------------------------");
}
int normalTotal = 0, lateTotal = 0, absentTotal = 0;
double attendRateTotal = 0;
for (int i = 0; i < n; i++) {
normalTotal += student[i].normal;
lateTotal += student[i].late;
absentTotal += student[i].absence;
attendRateTotal += student[i].AttendRate;
}
cout << "总正常次数: " << normalTotal << endl;
cout << "总迟到次数: " << lateTotal << endl;
cout << "总旷课次数: " << absentTotal << endl;
cout << "平均出勤率: " << attendRateTotal / n << endl;
}