本题要求编写一个解密藏尾诗的程序。
注:在 2023 年 1 月 17 日 15 点 14 分以后,该题数据修改为 UTF-8 编码。
输入格式:
输入为一首中文藏尾诗,一共四句。每句一行,但句子不一定是等长的,最短一个汉字,最长九个汉字。注意:一个汉字占三个字节。
输出格式:
取出每句的最后一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。
输入样例:
悠悠田园风
然而心难平
兰花轻涌浪
兰香愈幽静
输出样例:
风平浪静
code
#include <stdio.h>
#include <string.h>
int main() {
char poem[4][30];
char result[13];
int i, j;
// Input strings using fgets instead of gets
for (i = 0; i < 4; i++) {
fgets(poem[i], sizeof(poem[i]), stdin);
// Remove the newline character from fgets if present
poem[i][strcspn(poem[i], "\n")] = '\0';
}
j = 0;
for (i = 0; i < 4; i++) {
int len = strlen(poem[i]);
if (len >= 3) {
result[j] = poem[i][len - 3];
result[j + 1] = poem[i][len - 2];
result[j + 2] = poem[i][len - 1];
j += 3;
} else {
// Handle case where a string has fewer than 3 characters
// For example, you could fill in with a placeholder like 'X'
result[j] = result[j + 1] = result[j + 2] = 'X';
j += 3;
}
}
result[12] = '\0'; // Null-terminate the result string
printf("%s\n", result);
return 0;
}
383

被折叠的 条评论
为什么被折叠?



