Encoding The Diary
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1789 Accepted Submission(s): 1052
Problem Description
You know many girls likes writing diaries,of course they have some secrets don’t want others to know.So this time, they asked you to encoding the diary.
The rule is :
Give you a string. Such as “ARE YOU AC?”
Firstly , delete all spaces in this string.
You will get “AREYOUAC?”
String AREYOUAC?
Index 123456789
Secondly,print the characters who’s index are the multiple of 3.
Thirdly, print the characters who’s index are the multiple of 2.If it has been printed,just ignore it .
At last,print the characters that have not been printed.
The rule is :
Give you a string. Such as “ARE YOU AC?”
Firstly , delete all spaces in this string.
You will get “AREYOUAC?”
String AREYOUAC?
Index 123456789
Secondly,print the characters who’s index are the multiple of 3.
Thirdly, print the characters who’s index are the multiple of 2.If it has been printed,just ignore it .
At last,print the characters that have not been printed.
Input
Each case will contain a string in one line.You may suppose the length of the string will not exceed 200.
Output
For each case, output the encoded string in one line.
Sample Input
ARE YOU AC?
Sample Output
EU?RYCAOA
这题是字符串的基础题,主要是需要先处理空格然后进行按索引输出,对于这题的话可以选择对于该字符串进行删空格处理,但是这样子的话会让时间复杂度过高,所以我利用一个flag来储存除去空格之后的字符索引位置,然后开两个辅助数组进行储存索引为2的倍数且不为3的倍数以及其他索引的字符,这样子利用空间换取时间减少时间复杂度。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str[210];
char a[210];
char b[210];
int temp1, temp2;
while (gets(str))
{
int len = strlen(str);
int flag = 0;
temp1 = temp2 = 0;
for (int i = 0; i < len; i++)
{
if (str[i] != ' ')
flag++;
else
continue;
if (flag % 3 == 0)
printf("%c", str[i]);
else if (flag % 2 == 0 && flag % 3 != 0)
a[temp1++] = str[i];
else if (flag % 3 != 0 && flag % 2 != 0)
b[temp2++] = str[i];
}
a[temp1] = '\0';
b[temp2] = '\0';
printf("%s", a);
printf("%s\n", b);
}
}