本次训练内容为字符串,有一定难度,但是也都是可以做的。
1.5473 学习之正确率
本题细节比较多,Waiting、Judging、System Error均不考虑在内。
我们可以设置一个总题数的袋子,还有一个Accepted的袋子,正确率=正确的/总数
注意输入包含多组,而且n后面的换行要吃掉。
还有总题数可能是0,可能会除0错误,需要特判。
当然这段时间只要有正确的,那么正确率最低为1%,这点也需要特别判断出来。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int num1=0,num2=0;
cin.get();
for(int i=0;i<n;i++)
{
string s;
getline(cin,s);
if(s=="Accepted")
num1++;
if(s!="Waiting"&&s!="Judging"&&s!="System Error")
num2++;
}
//总题数是0
if(num2==0)
{
cout<<"0%"<<endl;
}
else
{
//求出比率
int k=num1*100/num2;
if(k<1&&num1)k=1;
cout<<k<<"%"<<endl;
}
}
return 0;
}
2.4032 删除相同字符
我们可以用一个栈去维护当前的字符,如果相等两个均出栈即可。
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin>>s,s!="###")
{
int top=0;
char st[1005];
for(int i=0;s[i];i++)
{
//进栈
st[top++]=s[i];
//栈上两个相等弹出
if(top>=2&&st[top-1]==st[top-2])
{
top-=2;
}
}
for(int i=0;i<top;i++)
{
cout<<st[i];
}
if(top==0)cout<<"NO char !";
cout<<"
";
}
return 0;
}
3.4675 字符串
直接桶排标记下,map也可以。注意题目是字符是否出现过
#include<bits/stdc++.h>
using namespace std;
int main()
{
int flag[26]={0};
char c;
while(cin>>c,c!='#')
{
flag[c-'a']=1;
}
bool flag2=true;
while(cin>>c,c!='#')
{
if(flag[c-'a']==0)
{
flag2=false;
break;
}
}
if(flag2)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
4.4379 这个真不会
还是栈,遇到-出栈一个元素,遇到@栈清空即top=0
#include<bits/stdc++.h>
using namespace std;
char a[1005];
int main()
{
string s;
while(getline(cin,s))
{
int top=0;
for(int i=0;s[i];i++)
{
if(s[i]=='#')
{
if(top)top--;
}
else if(s[i]=='@')top=0;
else a[top++]=s[i];
}
if(!top)cout<<"it is a pity!";
else
{
for(int i=0;i<top;i++)cout<<a[i];
}
cout<<"
";
}
return 0;
}
5.5051 火星数字
复杂模拟,分情况讨论
6.4644 展开字符串
复杂模拟,分情况讨论