UVA10361 - Automatic Poetry
# 问题描述
第一行输入字符串形势如同s1<s2>s3<s4>s5,第二行输入的字符串中,将s2和s4互换,并输出("<>"不输出)。
# 思路
将字符串分隔存储,然后进行处理,看懂题意并不难,难得是如何让自己写的代码能够简洁明了。
# 代码
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
const int maxn=120;
using namespace std;
int main()
{
int n,i,j;
char s1[maxn],s2[maxn],s3[maxn],s4[maxn],s5[maxn],s[maxn],sn[maxn];
cin>>n;
getchar();
while(n--)
{
int a,b,c,d,e;
a=b=c=d=e=0;
memset(s,0,sizeof(s));
memset(sn,0,sizeof(sn));
gets(s);
gets(sn);
for(i=0;s[i]!='<';i++)
s1[a++]=s[i];
s1[a]='\0';
for(j=i+1;s[j]!='>';j++)
s2[b++]=s[j];
s2[b]='\0';
for(i=j+1;s[i]!='<';i++)
s3[c++]=s[i];
s3[c]='\0';
for(j=i+1;s[j]!='>';j++)
s4[d++]=s[j];
s4[d]='\0';
for(i=j+1;s[i]!='\0';i++)
s5[e++]=s[i];
s5[e]='\0';
cout<<s1<<s2<<s3<<s4<<s5<<endl;
sn[strlen(sn)-3]='\0';
cout<<sn<<s4<<s3<<s2<<s5<<endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
编辑 (opens new window)
上次更新: 2022/05/18, 19:03:08