http://ac.jobdu.com/problem.php?pid=1361
将一行英文句子按单词反转。
二维字符串存储,反转外围即可。
#include <stdio.h>
#include <string.h>
#define M 50000
int main(void)
{
int n, i, j, m, k;
char s[M+1];
while (gets(s))
{
n = strlen(s);
i = n;
while (i>0)
{
m = i;
while (i >= 0 && s[--i] == ' ');
if (i<0)
break;
k = i;
if (m != n)
{
for (j=i+1; j<=m; j++)
printf(" ");
}
while (i >= 0 && s[--i] != ' ' );
//if (m != n)
// printf(" ");
for (j=i+1; j<=k; j++)
printf("%c", s[j]);
}
printf("\n");
}
return 0;
}
/************************************************************** Problem: 1361 User: liangrx06 Language: C Result: Accepted Time:210 ms Memory:912 kb ****************************************************************/
http://ac.jobdu.com/problem.php?pid=1362
实现字符串的循环左移。
用另外一个字符串存储要左移会被覆盖的那部分,然后放到后面即可。
#include <stdio.h>
#include <string.h>
#define N 1000
int main(void)
{
int k, i, n;
char s[N+1], s2[N+1];
while (scanf("%s%d", s, &k) != EOF)
{
strcpy(s2, s);
n = strlen(s);
k = k%n;
for (i=0; i<n-k; i++)
s[i] = s2[i+k];
for (; i<n; i++)
s[i] = s2[i+k-n];
printf("%s\n", s);
}
return 0;
}
/**************************************************************
Problem: 1362
User: liangrx06
Language: C
Result: Accepted
Time:70 ms
Memory:912 kb
****************************************************************/
http://ac.jobdu.com/problem.php?pid=1363
找出在上家出的牌是“三带一对”的情况下你手中的牌是否比上家的牌大。
主要是注意特殊情况的考虑,比较简单。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int main(void)
{
int n, i, j, x;
int a[N], b[N], rank[N];
while (scanf("%d", &n) != EOF)
{
memset(rank, 0, sizeof(rank));
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
if (a[i] <= 2)
a[i] += 13;
rank[a[i]]++;
}
for(i=0; i<5; i++)
scanf("%d", &b[i]);
x = b[0];
if (x <= 2)
x += 13;
for (i=x+1; i<=15; i++)
{
if (rank[i] >= 3)
{
for (j=3; j<=15; j++)
{
if (j == i)
continue;
if (rank[j] >= 2)
break;
}
if (j <= 15)
break;
}
}
if (i <= 15)
{
if (i > 13)
i -= 13;
if (j > 13)
j -= 13;
printf("%d %d %d %d %d\n", i, i, i, j, j);
}
else
printf("My God\n");
}
return 0;
}
/************************************************************** Problem: 1363 User: liangrx06 Language: C Result: Accepted Time:70 ms Memory:912 kb ****************************************************************/
http://ac.jobdu.com/problem.php?pid=1364
在地铁有限的空间中装载挑选出来的炸药使得地铁的破坏力最大。
其实就是最基本的01背包。
#include <stdio.h>
#include <string.h>
#define N 100
#define M 1000
int main(void)
{
int s, n, i, j;
int room[N], power[N];
int dp[M+1];
while (scanf("%d%d", &s, &n) != EOF)
{
for(i=0; i<n; i++)
scanf("%d%d", &room[i], &power[i]);
memset(dp, 0, sizeof(dp));
for(i=0; i<n; i++)
{
for(j=s; j>=room[i]; j--)
{
int tmp = dp[j-room[i]] + power[i];
dp[j] = (tmp > dp[j]) ? tmp : dp[j];
}
}
printf("%d\n", dp[s]);
}
return 0;
}
/**************************************************************
Problem: 1364
User: liangrx06
Language: C
Result: Accepted
Time:170 ms
Memory:912 kb
****************************************************************/
http://ac.jobdu.com/problem.php?pid=1365
在一张二维棋盘上,按照中国象棋马的走法,从起点到终点的步数。
基本的广度优先搜索,注意起点终点相同这种特殊情况的判断。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int N = 100;
const int INF = 0x3f3f3f3f;
int n;
bool legal(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < n;
}
int BFS()
{
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
sx--, sy--, ex--, ey--;
int i;
int d[N][N];
typedef pair<int, int> P;
queue <P> q;
for (i = 0; i < n; i ++)
fill(d[i], d[i]+n, INF);
q.push(P(sx, sy));
d[sx][sy] = 0;
int pos[8][2] = {{1, 2}, {2, 1}, {1, -2}, {-2, 1},
{-1, 2}, {2, -1}, {-1, -2}, {-2, -1}};
while( q.size() ) {
P p = q.front();
q.pop();
int x = p.first;
int y = p.second;
if (x == ex && y == ey)
return d[x][y];
for (i = 0; i < 8; i ++) {
int nx = x + pos[i][0];
int ny = y + pos[i][1];
if (legal(nx, ny) && d[nx][ny] == INF) {
q.push(P(nx, ny));
d[nx][ny] = d[x][y]+1;
}
}
}
return -1;
}
int main(void)
{
while (cin >> n) {
printf("%d\n", BFS());
}
return 0;
}
/************************************************************** Problem: 1365 User: liangrx06 Language: C++ Result: Accepted Time:730 ms Memory:1524 kb ****************************************************************/