题目链接:https://codeforc.es/contest/1352
A
题意:给定一个数字,输出其每个位置的值与其权值的乘积,并输出可输出多少个数。
题解:用字符串读入,扫描每个数字,记录其位置的权值,最后按整形输出即可。
#include <bits/stdc++.h>
using namespace std;
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
string s;
int a[6],b[6];
cin>>t;
while (t--)
{
cin>>s;
int l=s.size();
int sum=0;
for (int i=1;i<=l;i++)
{
if (s[l-i]!='0')
{
a[sum]=s[l-i]-'0';
b[sum]=i-1;
sum++;
}
}
cout<<sum<<endl;
for (int i=0;i<sum;i++)
{
cout<<a[i]*pow(10,b[i])<<' ';
}
cout<<endl;
}
return 0;
}
B
题意:给定数n和数k,判断能否由k奇偶性相同的正整数相加得到n。
题解:如果n为奇数,则只能由k个奇数相加,判断能否构成即可。
n为偶数,此时如果k为奇数,则只能由k个偶数相加,判断能否即可。
若此时k为偶数,则选择判断k个奇数相加能否即可。(证明:若选择偶数则最小数字为2,若此时相加可以构成结果,那么选择其中一个数字不变,将其他所有数字变为1,差值加给不变的数字,则可得到k个奇数,即k个偶数可以构成,那么k个奇数一定也可以构成,反之不成立)
#include <bits/stdc++.h>
using namespace std;
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
{
int n,k;
cin>>n>>k;
if (n%2)
{
if (k%2)
{
if (n-(k-1)>0)
{
cout<<"YES"<<endl;
for (int i=0;i<k;i++)
{
if (!i)
cout<<n-k+1<<' ';
else
cout<<'1'<<' ';
}
cout<<endl;
}
else
cout<<"NO"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
else
{
if (k%2)
{
if (n/2>=k)
{
cout<<"YES"<<endl;
for (int i=0;i<k;i++)
{
if (!i)
cout<<2*(1+n/2-k)<<' ';
else
cout<<'2'<<' ';
}
cout<<endl;
}
else
cout<<"NO"<<endl;
}
else
{
if (n-(k-1)>0)
{
cout<<"YES"<<endl;
for (int i=0;i<k;i++)
{
if (!i)
cout<<n-k+1<<' ';
else
cout<<'1'<<' ';
}
cout<<endl;
}
else
cout<<"NO"<<endl;
}
}
}
return 0;
}
C
题意:给定数n和数k,输出第k个不被n整除的数。
题解:余数以0~(n-1)周期性排列,判断一下周期和位于此周期的位置即可。
using namespace std;
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
{
int n,k;
cin>>n>>k;
int a,b;
a=k/(n-1);
b=k%(n-1);
if (b)
{
cout<<a*n+b<<endl;
}
else
{
cout<<(a-1)*n+n-1<<endl;
}
}
return 0;
}
D
题意:有n颗糖果,alice从左往右吃,bob从右往左吃,alice和bob交替吃,alice先吃,每个人吃的都要比上一个人吃的严格多,最后一次除外,
直至吃完所有糖果。输出俩个人一共吃了几次糖果,和每个人吃的糖果数量。
题解:简单的模拟一下就好了,记录一下上个人吃了多少,和吃到什么位置即可。
#include <bits/stdc++.h>
using namespace std;
const int s=1e3+5;
int alice(int &x,int a,int n,int f[])//alice吃糖的函数
{
int rec=0;
while (rec<=a)
{
if (x>n)
break;
rec+=f[x];
x++;
}
return rec;
};
int bob(int &x,int a,int n,int f[])//bob吃糖的函数
{
int rec=0;
while (rec<=a)
{
if (x<n)
break;
rec+=f[x];
x--;
}
return rec;
};
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
{
int n,sum=0;
int a[s];
cin>>n;
for (int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
}
int l,r;
l=1;
r=n;
bool flag=true;
int ans_sum=0,num=0,alice_candy=0,bob_candy=0;
while (true)
{
if (flag)
{
ans_sum=alice(l,ans_sum,r,a);
alice_candy+=ans_sum;
num++;
flag=false;
if (alice_candy+bob_candy==sum)
break;
}
else
{
ans_sum=bob(r,ans_sum,l,a);
bob_candy+=ans_sum;
num++;
flag=true;
if (alice_candy+bob_candy==sum)
break;
}
}
cout<<num<<' '<<alice_candy<<' '<<bob_candy<<endl;
}
return 0;
}
E
题意:给定一个长为n的数组,输出数组中的元素等于此数组中连续片段和的元素个数。
题解:记录前缀和,用俩个循环就可以,外循环控制左边界,内循环控制右边界。
#include <bits/stdc++.h>
using namespace std;
const int s=8e3+3;
int a[s],judge[s],sum[s];
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
{
int n;
cin>>n;
for (int i=1;i<=n;i++)
{
cin>>a[i];
judge[a[i]]++;
sum[i]=sum[i-1]+a[i];
}
int ans,answer=0;
for (int i=1;i<=(n-1);i++)
{
for (int j=i+1;j<=n;j++)
{
ans=sum[j]-sum[i-1];
if (ans<8001 && ans>-1)
{
if (judge[ans])
{
answer+=judge[ans];
judge[ans]=0;
}
}
}
}
memset(judge,0,sizeof(judge));
memset(sum,0,sizeof(sum));
cout<<answer<<endl;
}
return 0;
}
F
题意:给定"11"的个数n2,"10"和"01"的个数n1,“00"的个数n0,输出可能的字符串。(题意缩减,请自行参考原题。)
题解:首先输出所有的"11”,n1的个数为奇数则按0,1的顺序输出直至完成n1的输出,最后输出0。n1的个数为偶数,则先输出0完成n0的输出,最后按1,0顺序完成n1的输出,如果n0为0,则选择按0,1的顺序输出完成n1的输出即可。
若n2为0,n1为奇数,则按1,0的顺序输出,否则按0,1的顺序输出,最后输出0。
若n2,n1都为0,则直接输出0即可。
#include <bits/stdc++.h>
using namespace std;
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
{
int n0,n1,n2;
cin>>n0>>n1>>n2;
if (n2)
{
//cout<<"fdsa"<<endl;
for (int i=0;i<=n2;i++)
cout<<'1';
if (n1%2)
{
for (int i=0;i<n1;i++)
{
if (i%2)
cout<<'1';
else
cout<<'0';
}
for (int i=0;i<n0;i++)
cout<<'0';
}
else
{
if (n0)
{
for (int i=0;i<=n0;i++)
cout<<'0';
for (int i=1;i<n1;i++)
{
if (i%2)
cout<<'1';
else
cout<<'0';
}
}
else
{
for (int i=0;i<n1;i++)
{
if (i%2)
cout<<'1';
else
cout<<'0';
}
}
}
}
else
{
if (n1)
{
if (n1%2)
{
for (int i=0;i<=n1;i++)
{
if (i%2)
cout<<'0';
else
cout<<'1';
}
}
else
{
for (int i=0;i<=n1;i++)
{
if (i%2)
cout<<'1';
else
cout<<'0';
}
}
for (int i=0;i<n0;i++)
cout<<'0';
}
else
{
for (int i=0;i<=n0;i++)
cout<<'0';
}
}
cout<<endl;
}
return 0;
}
G
题意:给定数n,输出长度为n,元素为1~n的数组,其中任意两个相邻元素的差值的绝对值大于1,小于5。没有此数组输出-1。
题解:构造一下即可,容易发现n小于4的时候无法构造。
大于等于4时:以 3 1 4 2为基础,左右拓展即可。说明如下:
n=5, 数组为:3 1 4 2 5
n=6,数组为:6 3 1 4 2 5
n=7,数组为:6 3 1 4 2 5 7
#include <bits/stdc++.h>
using namespace std;
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
{
int n,ans;
cin>>n;
if (n<4)
cout<<"-1"<<endl;
else
{
if (n%2)
ans=n-1;
else
ans=n;
while (ans>=6)
{
cout<<ans<<' ';
ans-=2;
}
cout<<"3 1 4 2 ";
ans=5;
while (ans<=n)
{
cout<<ans<<' ';
ans+=2;
}
cout<<endl;
}
}
return 0;
}
写在最后的吐槽(查看题解者可自行忽略)
E题专门说区分效率高与效率低的方案,吓得我当时没敢交代码,看其他题去了,结果暴力枚举还是可以过的。。。。。(我太菜了。。。)
也是自己第一次依靠自己完成了一场div的全解 (虽然是补完的,打的时候也就过了4题。。。。。),不过还是很开心。