1.特殊的正方形
代码
#include <bits/stdc++.h>
using namespace std;
int n;
char c[2]={'+','.'};
char ans[101][101];
int main()
{
scanf("%d", &n);
int op=0;
for(int i=1; i<=n; ++i)
{
for(int j=i; j<=n+1-i; ++j)
{
ans[i][j]=c[op];
ans[j][i]=c[op];
ans[n+1-i][j]=c[op];
ans[j][n+1-i]=c[op];
}
op^=1;
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
cout<<ans[i][j];
cout<<endl;
}
return 0;
}
2.走楼梯2
题目解析
动态规划,state[i][0]走到第i阶时最近一步只走一阶的方法数,state[i][1]走到第i阶时最后只连走一次两阶的方法数,state[i][2]走到第i阶时最后只连走两次两阶的方法数。
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int state[51][3];
int main()
{
scanf("%d", &n);
state[1][0]=1;
state[1][1]=0;
state[1][2]=0;
state[2][0]=1;
state[2][1]=1;
state[2][2]=0;
for(int i=3;i<=n;++i)
{
state[i][0]=state[i-1][0]+state[i-1][1]+state[i-1][2];
state[i][1]=state[i-2][0];
state[i][2]=state[i-2][1];
}
cout<<state[n][0]+state[n][1]+state[n][2];
return 0;
}
3.走路
代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
int action[101][2];
bool mp[100001];
unordered_set<int> s1,s2;
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d %d", &action[i][0], &action[i][1]);
s1.insert(0);
int op=1;
for(int i=1;i<=n;++i)
{
if(op==1)
{
for(int j: s1)
{
if(j+action[i][0]<=m)
s2.insert(j+action[i][0]);
if(j+action[i][1]<=m)
s2.insert(j+action[i][1]);
}
s1.clear();
op=2;
}
else
{
for(int j: s2)
{
if(j+action[i][0]<=m)
s1.insert(j+action[i][0]);
if(j+action[i][1]<=m)
s1.insert(j+action[i][1]);
}
s2.clear();
op=1;
}
}
if(op==1)
{
for(int i: s1)
mp[i]=true;
}
if(op==2)
{
for(int i: s2)
mp[i]=true;
}
for(int i=0;i<=m;++i)
{
if(mp[i]==true)
cout<<1;
else
cout<<0;
}
return 0;
}
4.简单分数统计
代码
#include <bits/stdc++.h>
using namespace std;
int N, M, K, score;
string pname[201],sname[201];
int pscore[201],sscore[201];
string s1, s2, s3;
int main()
{
scanf("%d %d %d", &N, &M, &K);
for(int i=1;i<=N;++i)
cin>>pname[i];
for(int i=1;i<=M;++i)
cin>>sname[i]>>sscore[i];
for(int i=1;i<=K;++i)
{
cin>>s1>>s2>>s3;
if(s3=="AC")
{
for(int j=1;j<=N;++j)
{
if(pname[j]==s1)
{
for(int k=1;k<=M;++k)
{
if(sname[k]==s2)
{
pscore[j]+=sscore[k];
break;
}
}
break;
}
}
}
}
for(int i=1;i<=N;++i)
cout<<pname[i]<<" "<<pscore[i]<<endl;
return 0;
}
5.Alice的德州扑克
代码
#include <bits/stdc++.h>
using namespace std;
int a[10];
int main()
{
for (int i = 0; i < 10; ++i)
scanf("%d", &a[i]);
if (a[4] == 14 && a[1] == a[0] + 1 && a[2] == a[1] + 1 && a[3] == a[2] + 1 && a[4] == a[3] + 1 &&
a[5] == a[6] && a[6] == a[7] && a[7] == a[8] && a[8] == a[9])
cout << "ROYAL FLUSH";
else if (a[1] == a[0] + 1 && a[2] == a[1] + 1 && a[3] == a[2] + 1 && a[4] == a[3] + 1 &&
a[5] == a[6] && a[6] == a[7] && a[7] == a[8] && a[8] == a[9])
cout << "STRAIGHT FLUSH";
else if (a[1] == a[2] && a[2] == a[3] && (a[0] == a[1] || a[3] == a[4]))
cout << "FOUR OF A KIND";
else if ((a[0] == a[1] && a[1] == a[2] && a[3] == a[4]) || (a[0] == a[1] && a[2] == a[3] && a[3] == a[4]))
cout << "FULL HOUSE";
else if (a[5] == a[6] && a[6] == a[7] && a[7] == a[8] && a[8] == a[9])
cout << "FLUSH";
else if (a[1] == a[0] + 1 && a[2] == a[1] + 1 && a[3] == a[2] + 1 && a[4] == a[3] + 1)
cout << "STRAIGHT";
else
cout << "FOLD";
return 0;
}
6.订单编号
题目解析
将整个订单编号设为一个范围,使用一个订单编号i,则在未使用的订单范围内二分查找,再将此范围(l,r)分为(l,i-1)和(i+1,r)。
代码
#include <bits/stdc++.h>
using namespace std;
int n;
set<pair<int, int>> s;
int main()
{
s.insert(make_pair(2e9, 1)); // r,l pair比较大小默认比较第一个元素
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
auto itr = s.lower_bound(make_pair(x, 0));
if (itr->second <= x)
{
printf("%d ", x);
if (itr->first >= x + 1)
s.insert(make_pair(itr->first, x + 1));
if (x - 1 >= itr->second)
s.insert(make_pair(x - 1, itr->second));
s.erase(itr);
}
else
{
printf("%d ", itr->second);
if (itr->first >= itr->second + 1)
s.insert(make_pair(itr->first, itr->second + 1));
s.erase(itr);
}
}
return 0;
}
7.饿饿 饭饭
题目解析
二分打饭的轮数
代码
#include <bits/stdc++.h>
using namespace std;
int n;
long long k;
int a[100001], b[100001], tot;
long long func(int x)
{
long long res = 0;
for (int i = 1; i <= n; ++i)
{
if (a[i] <= x)
res += a[i];
else
res += x;
}
return res;
}
int main()
{
scanf("%d %lld", &n, &k);
long long s = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
s += a[i];
}
if (s < k)
{
cout << "-1" << endl;
return 0;
}
int l = 0, r = 1e9;
while (l + 1 < r)
{
int m = (l + r) / 2;
if (func(m) <= k)
l = m;
else
r = m;
}
k -= func(l);
for (int i = 1; i <= n; ++i)
{
if (a[i] > l)
b[++tot] = i;
}
for (int i = k + 1; i <= tot; ++i)
printf("%d ", b[i]);
for (int i = 1; i <= k; ++i)
{
if (a[b[i]] > l + 1)
printf("%d ", b[i]);
}
return 0;
}
8.任务分配
题目解析
动态规划,dp[i]为到时间i为止进行活动获得最大收益。
代码
#include <bits/stdc++.h>
using namespace std;
int n, dp[1001], ans, maxtime;
struct act
{
int s, e, w;
} a[1001];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d %d %d", &a[i].s, &a[i].e, &a[i].w);
maxtime = max(maxtime, a[i].e);
}
sort(a+1,a+n+1,[](act x, act y){
return x.s<y.s||(x.s==y.s&&x.e<y.e);
});
for (int i = 1; i <= n; ++i)
{
//printf("begin=%d end=%d w=%d\n", a[i].s, a[i].e, a[i].w);
dp[a[i].e] = max(dp[a[i].e], dp[a[i].s] + a[i].w);
for (int j = a[i].e; j <= maxtime; ++j)
{
dp[j] = max(dp[j], dp[j - 1]);
//printf("dp[%d]=%d\n", j, dp[j]);
}
}
cout << dp[maxtime] << endl;
return 0;
}