1 7-1 打字
题目
题目解析
直接判断就好。
代码
#include <bits/stdc++.h>
using namespace std;
char a[]={'1','Q','A','Z',
'2','W','S','X',
'3','E','D','C',
'4','R','F','V',
'5','T','G','B',
'6','Y','H','N',
'7','U','J','M',
'8','I','K',',',
'9','O','L','.',
'0','P',';','/','-','[', '\'' ,'=',']' };
int ans[8];
int main()
{
string s;
cin>>s;
for(int i=0;i<s.length();++i)
{
for(int j=0;j<45;++j)
{
if(s[i]==a[j])
{
if(j<=3)
ans[0]++;
else if(j<=7)
ans[1]++;
else if(j<=11)
ans[2]++;
else if(j<=19)
ans[3]++;
else if(j<=27)
ans[4]++;
else if(j<=31)
ans[5]++;
else if(j<=35)
ans[6]++;
else if(j<=44)
ans[7]++;
break;
}
}
}
for(int i=0;i<8;++i)
cout<<ans[i]<<endl;
return 0;
}
2 7-2 分香肠
题目
题目解析
可以视作把每个香肠均分给所有客人。
题目是把2个香肠分给6个客人,可以把香肠看成12份,每个人分2份,但要记住每6份为一个香肠,分到6份时无需再切割。
代码
#include <bits/stdc++.h>
using namespace std;
int N,M;
int main()
{
cin>>N>>M;
int tot=N*M;
int tmp=0,ans=0;
while(tmp<tot)
{
tmp+=N;
if(tmp%M==0)
continue;
ans++;
}
cout<<ans;
return 0;
}
7-3 h0145. 会议安排
题目
题目解析
题目为贪心问题。先将会议按开始先后排序,那么前后的两个会议有三种情况。
一、前一个会议的结束时间小于等于后一个会议的开始时间。可以直接参加后一个会议。
二、前一个会议的结束时间大于后一个会议结束时间。选择不参加前一个会议,参加后一个会议。
三、后一个会议的开始时间小于前一个会议结束时间,并且前一个会议结束时间小于后一个会议的结束时间。选择参加前一个会议,因为前一个会议早结束有更多时间参加其他会议。
代码
#include <bits/stdc++.h>
using namespace std;
struct act{
int b;
int e;
};
int M,N;
act a[10005];
int main()
{
cin>>M;
while(M--)
{
cin>>N;
for(int i=1;i<=N;++i)
cin>>a[i].b>>a[i].e;
sort(a+1,a+N+1,[](act x,act y){
return x.b<y.b;
});
int now=0;
int cnt=0;
for(int i=1;i<=N;++i)
{
if(a[i].b>=now)
{
now=a[i].e;
cnt++;
}
else
{
if(a[i].e<now)
now=a[i].e;
}
}
cout<<cnt<<endl;
}
return 0;
}
4 7-4 神秘密码
题目
题目解析
压缩有多重压缩的可能,如AC[3FUN[3X]],所以使用递归方法解决。
代码
#include <bits/stdc++.h>
using namespace std;
string func()
{
int num;
string s1, s2;
char c;
while (cin >> c)
{
if (c == '[')
{
cin >> num;
s2 = func();
while (num--)
s1 += s2;
}
else if (c == ']')
return s1;
else
s1 += c;
}
return s1;
}
int main()
{
cout << func();
return 0;
}
5 7-5 h0114.国王游戏
题目
题目解析
按照a ∗ b a*ba∗b从小到大排序然后枚举即可。
从小到大排序的就能得到最优解。排好序后只需要算一下每个大臣有多少金币,找到拿的金币最多的那个即可(需要高精度)。
代码
#include <bits/stdc++.h>
using namespace std;
struct people
{
int l,r;
};
int n;
people mp[1005];
int tot[4001],tmp[4001],ans[4001];
int main()
{
cin>>n;
for(int i=0;i<=n;++i)
cin>>mp[i].l>>mp[i].r;
sort(mp+1,mp+n+1,[](people a,people b){
return a.l*a.r<b.l*b.r;
});
while(mp[0].l)
{
tot[++tot[0]]=mp[0].l%10;
mp[0].l/=10;
}
ans[0]=1;
for(int i=1;i<=n;++i)
{
int num=mp[i].r;
memset(tmp,0,sizeof(tmp));
int yu=0;
for(int j=tot[0];j>=1;--j)
{
tmp[j]=(yu*10+tot[j])/num;
yu=(yu*10+tot[j])%num;
}
tmp[0]=tot[0];
while(tmp[tmp[0]]==0&&tmp[0]>1)
tmp[0]--;
if(tmp[0]>ans[0]||(tmp[0]==ans[0]&&tmp[tmp[0]]>ans[ans[0]]))
{
for(int j=0;j<=tmp[0];++j)
ans[j]=tmp[j];
}
memset(tmp,0,sizeof(tmp));
int a[5];
a[0]=0;
while(mp[i].l)
{
a[++a[0]]=mp[i].l%10;
mp[i].l/=10;
}
for(int x=1;x<=a[0];++x)
{
yu=0;
for(int y=1;y<=tot[0];++y)
{
tmp[x+y-1]+=a[x]*tot[y]+yu;
yu=tmp[x+y-1]/10;
tmp[x+y-1]%=10;
}
tmp[x+tot[0]]+=yu;
}
tmp[0]=tot[0]+a[0];
while(tmp[tmp[0]]==0&&tmp[0]>1)
tmp[0]--;
for(int j=0;j<=tmp[0];++j)
tot[j]=tmp[j];
}
for(int i=ans[0];i>=1;--i)
cout<<ans[i];
return 0;
}