您的位置:首页 > 其它

HYNU 第四次周赛题解

2017-12-08 21:53 246 查看
大自然的搬运工

大水题

题目描述

给你n个整数,再给出整数k,请你求出有几个能被k整除?(所以整数都小于2^31-1)

输入

多组输入

每组第一行2个整数n,k(1<=n,k<=100)

第二行n个整数

输出

每组一个整数,占一行,代表能被整除的数的个数

样例输入

3 2

1 2 3

4 3

1 2 3 4

样例输出

1

1

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
int res=0;
for(int i=0;i<n;i++){
int x;
scanf("%d",&x);
if(x%k==0) res++;
}
printf("%d\n",res);
}
return 0;
}


B: Overs

英文题,英语太菜导致某些同学在题意理解上有问题,我们的锅

样例输入

5

2 1 4 3 5

5

1 4 3 5 2

样例输出

2 1 4

1 5 3

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
int n,k,rk[110];
while(scanf("%d",&n)!=EOF)
{
int res=0;
for(int i=1;i<=n;i++){
scanf("%d",&rk[i]);
}
for(int i=1;i<=3;i++){
for(int j=1;j<=n;j++){
if(rk[j]==i){
if(i==1) printf("%d",j);
else printf(" %d",j);
}
}
}
printf("\n");
}
return 0;
}


问题 C: 矩形问题

题目描述

鲜花可以在一块面积大小固定的区域围一个矩形栅栏,这样他就可以把猪放进去养猪了,苦于资金不够,他想在面积为S的矩形区域中,围成栅栏的周长尽量小,且边长是整数。这样他就能够尽量节省了。

输入

多组实例输入,每个实例一个整数S(1 <= S <= 1000)

输出

最小周长

样例输入

24

样例输出

20

枚举S的因子即可

#include<cstdio>
#include<queue>
#include<iostream>
#include<vector>
#include<map>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#define cle(a) memset(a,0,sizeof(a))
#define inf(a) memset(a,ox3f,sizeof(a))
#define ll long long
#define Rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
const int INF = ( 2e9 ) + 2;
//const int maxn =
int main()
{
int s;
while(~scanf("%d",&s))
{
int len=(int)sqrt(s+1);
int ans=INF;
for(int i=1; i<=len; i++)
{
if(s%i==0)
{
if(ans>2*(s/i+i))
{
ans=2*(s/i+i);
}
}
}
printf("%d\n",ans);
}

}


问题 D: TXN的新房

题目描述

TXN买了一套新房,她想要粉刷她卧室的一面墙,她现在有三种颜色的颜料,白,蓝,黄。假设她的墙一开始为全白,问她经过一系列操作之后墙壁的颜色。

输入

输入有多组。

第一行为两个正整数n,m,代表了墙的长度与操作数。(n<=100,m<=100)

接下来m行,每行包含两个数字和一个字符,l,r,w/b/y。(1<=l<=r<=n),[l,r]代表了粉刷的区间,w,b,y分别代表了三种颜色白,蓝,黄。(注意字符的输入)

输出

输出包括一行

Case+组数:最后的墙壁

样例输入

5 2

1 2 b

2 3 y

样例输出

根据题意模拟

Case 1: byyww

#include<iostream>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int INF = (2e9) + 2;
int main()
{
int n,m,cas=1;
char w[110];
while(~scanf("%d%d",&n,&m))
{
memset(w,'w',sizeof(w));
while(m--)
{
int l,r;
char c;
scanf("%d%d",&l,&r);
cin>>c;
for(int i=l;i<=r;i++)
w[i]=c;
}
printf("Case %d: ",cas++);
for(int i=1;i<=n;i++)printf("%c",w[i]);
puts("");
}
}


问题 E: 回。。。回文串

题目描述

给出一个字符串,让你求出有多少子串是回文串,且字串长度为奇数。

回文串为前后对称的字符串,例如:a,aa,aba;

子串为母串的一个连续子集,例如abc的字串:a,b,c,ab,bc,abc;

输入

多组输入,每组一个字符串,占一行。(所有字符串长度不超过100)

输出

每组一个整数,占一行,代表回文字子串个数

样例输入

abba

ababa

样例输出

4

9

暴力

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n,m,cas=0;
char s[101];
while(scanf("%s",s)!=EOF)
{
int len=strlen(s);
int res=0;
for(int i=0;i<len;i++)
{
for(int j=i;j<len;j++){
if((j-i+1)%2==0) continue;
bool flag=0;
for(int a=i,b=j;a<b;a++,b--)
{
if(s[a]!=s[b]){
flag=1;
break;
}
}
if(!flag) res++;
}
}
printf("%d\n",res);
}
return 0;
}


问题 F: IP地址

题目描述

IP是英文Internet Protocol的缩写,意思是“网络之间互连的协议”,也就是为计算机网络相互连接进行通信而设计的协议。在因特网中,它是能使连接到网上的所有计算机网络实现相互通信的一套规则,规定了计算机在因特网上进行通信时应当遵守的规则。输入IP地址被用来给Internet上的电脑一个编号。大家日常见到的情况是每台联网的PC上都需要有IP地址,才能正常通信。我们可以把“个人电脑”比作“一台电话”,那么“IP地址”就相当于“电话号码”,而Internet中的路由器,就相当于电信局的“程控式交换机”。IP地址是一个32位的二进制数,通常被分割为4个“8位二进制数”(也就是4个字节)。IP地址通常用“点分十进制”表示成(a.b.c.d)的形式,其中,a,b,c,d都是0~255之间的十进制整数。例:点分十进IP地址(100.4.5.6),实际上是32位二进制数(01100100.00000100.00000101.00000110)。—摘自百度百科

现在给你一些32位二进制数,将这些数全部用点分十进制表示

注意:如果要读入字符串,不要用gets(),用scanf() 或cin读取!!!

输入

输入数据有多组,每个实例一个32位二进制数

输出

每个实例用“点分十进制”表示这个数,输出之

样例输入

11001000001111111010100100100110

10101110110110111010011111100100

样例输出

200.63.169.38

174.219.167.228

根据题意模拟

#include<iostream>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int INF = (2e9) + 2;
int main()
{
char bits[33];
int ans[5];
while(~scanf("%s",bits))
{
int index=31;
int cur=1,sum=0,cnt=0;
while(index>=0)
{
if(bits[index]=='1')sum+=cur;
cur<<=1;
if(index%8==0)
{
ans[cnt++]=sum;
cur=1;
sum=0;
}
index--;
}
for(int i=cnt-1;i>=0;i--)
if(i==0)printf("%d\n",ans[i]);
else printf("%d.",ans[i]);

}
}


问题 G: XY数

题目描述

HXY发明了一种数(那就叫XY数吧),XY数定义为:数的每一位只能为1或者0,例如1010,10,1,0,都是XY数。

而102,2,12,则不是。现在给你一个正整数N,问最少能分解成多少个XY数。并从大到小输出这些数。

输入

输入多组数据,每组数据包含一个N。(0 < N<=10^6)

输出

输出包含两行,第一行为最少分解的XY数的个数,第二行为从大到小排序过的被分解后的XY数。

最后一个数后不跟空格。

样例输入

9

32

样例输出

9

1 1 1 1 1 1 1 1 1

3

11 11 10

思维+模拟

可以发现数目最少就是:这个数上所有位中的最大值。对于其他小于最大值的位可以用0来填。只有最大值需要全部填1,具体操作看代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int INF = (2e9) + 2;
int ans[7][10];
int bits[10];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(bits,0,sizeof(bits));
memset(ans,0,sizeof(ans));
int col=0,row=0;
int temp=n;
while(temp)
{
col=max(col,temp%10);
bits[++row] = temp%10;
temp/=10;
}
printf("%d\n",col);
int fir=1;
while(n)
{
int tmp = n,cnt=0,cur=1;
int res=0;
while(tmp)
{
if(tmp%10)
res+=cur;
cur*=10;
tmp/=10;
}
n-=res;
if(!fir)printf(" ");
printf("%d",res);
fir=0;
}
puts("");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: