A.回文串
代码如下:
#include <cstdio>
#include <cstring>
bool is_palin( char *s){
int x = strlen( s);
for ( int i = 0; i < ( x - 1) / 2; i++){
if ( s[i] != s[x - i - 1])
return 0;
}
return 1;
}
char a[10010], b[10010];
int main()
{
int t;
scanf( "%d", &t);
while ( t--){
scanf( "%s", a);
int x = strlen( a), i;
for ( i = 0; i < x; i++){
strncpy( b, a, i);
strcpy( b + i, a + i + 1);
if ( is_palin( b)){
break;
}
}
if ( i == x && !is_palin( a))
printf( "No\n");
else
printf( "Yes\n");
}
return 0;
}
水题一个,直接判断删除对应各位(将删除后的串保留到另外数组中)后是否是回文串即可
B: 路上的颜色
代码如下:#include <cstdio>
#define maxn 105
int pa[maxn][maxn];
int findpa( int x, int y){
return pa[y][x] == x ? x : pa[y][x] = findpa( pa[y][x], y);
}
void unionpa( int x, int y, int z){
int fx = findpa( x, z), fy = findpa( y, z);
if ( fx != fy) pa[z][fy] = fx;
}
int main()
{
int t, n, m, a, b, c, q;
scanf( "%d", &t);
while ( t--){
scanf( "%d%d", &n, &m);
int x = m;
for ( int i = 1; i <= n; i++){
for ( int j = 1; j <= m; j++)
pa[j][i] = i;
}
while ( x--){
scanf( "%d%d%d", &a, &b, &c);
unionpa( a, b, c);
}
scanf( "%d", &q);
while ( q--){
scanf( "%d%d", &a, &b);
int s = 0;
for ( int i = 1; i <= m; i++){
if ( findpa( a, i) == findpa( b, i)){
s++;
}
}
printf( "%d\n", s);
}
}
return 0;
}
对于每种颜色维护一个并查集,读入边时合并,查询时一次遍历判断对应集合代表元素是否相同即可
C: Flower
代码如下#include <cstdio>
int main()
{
int t, r, g, b, x, y, z;
scanf( "%d", &t);
while ( t--){
scanf( "%d%d%d", &r, &g, &b);
int s = ( x = r / 3) + ( y = g / 3) + ( z = b / 3);
r = r % 3;
g = g % 3;
b = b % 3;
if ( r == 2 && g == 2 && b == 2){
s += 2;
}
else if ( r >= 1 && g >= 1 && b >= 1)
s++;
if ( r == 0 && g == 2 && b == 2 && x > 0 || r == 2 && g == 0 && b == 2
&& y > 0 || r == 2 && g == 2 && b == 0 && z > 0)
s++;
printf( "%d\n", s);
}
return 0;
}
水题,注意一下特殊情况即可( 余数0 2 2 , 2 2 2。。。)。注:OJ上的数据貌似有点弱,有组数据当时我自己测没过然后提交过了。。。
D: Ringo和机器人
#include <cstdio>
#include <cstring>
char a[110];
int main()
{
int t;
long long x, y;
scanf( "%d", &t);
while ( t--){
scanf( "%lld%lld", &x, &y);
scanf( "%s", a);
int w = strlen( a), s = 0, d = 0;
for ( int i = 0; i < w; i++){
if ( a[i] == 'L')
s--;
else if ( a[i] == 'R')
s++;
else if ( a[i] == 'U')
d++;
else if ( a[i] == 'D')
d--;
}
int l = 0, r = 0, i;
if ( x == 0 && y == 0)
printf( "Yes\n");
else{
for ( i = 0; i < w; i++){
if ( a[i] == 'L')
l--;
else if ( a[i] == 'R')
l++;
else if ( a[i] == 'U')
r++;
else if ( a[i] == 'D')
r--;
if ( s != 0 && d != 0 && ( x - l) / s == ( y - r) / d && ( x - l) / s >= 0 && ( x - l) % s == 0 && ( y - r) % d == 0)
break;
else if ( s != 0 && d == 0 && ( x - l) % s == 0 && ( y - r) == 0 && ( x - l) / s >= 0)
break;
else if ( s == 0 && d != 0 && ( x - l) == 0 && ( y - r) % d == 0 && ( y - r) / d >= 0)
break;
else if ( s == 0 && d == 0 && ( x - l) == 0 && ( y - r) == 0)
break;
}
if ( i == w)
printf( "No\n");
else
printf( "Yes\n");
}
}
return 0;
}
首先算出执行完整一遍指令的x,y的变化,然后一次遍历,注意对各种情况的分类即可
E: 纸牌
#include <cstdio>
#include <cmath>
int main()
{
int t, n, x, y;
scanf( "%d", &t);
while ( t--){
scanf( "%d%d", &n, &x);
int s = 0;
while ( n--){
scanf( "%d", &y);
s += y;
}
s = abs( s);
y = s / x;
if ( s % x)
y++;
printf( "%d\n", y);
}
return 0;
}
最水的题,没啥说的,五分钟应该能切掉