7-11 藏头诗

7-11 藏头诗

作者 张泳        单位 浙江大学

本题要求编写一个解密藏头诗的程序。

注:在 2022 年 7 月 14 日 16 点 50 分以后,该题数据修改为 UTF-8 编码。

输入格式:

输入为一首中文藏头诗,一共四句,每句一行。注意:一个汉字占三个字节。

输出格式:

取出每句的第一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

样例:">样例:">:">样例:">样例:">输入样例

一叶轻舟向东流
帆稍轻握杨柳手
风纤碧波微起舞
顺水任从雅客流

输出样例:

一帆风顺
#include<stdio.h>
int main(){
    int i,j;
    char ch[4][50];
    for(i=0;i<4;i++)
        gets(ch[i]);
    for(i=0;i<4;i++)
        printf("%c%c%c",ch[i][0],ch[i][1],ch[i][2]);
    return 0;
}

第一个for循环是输入

第二个for循环时输出,题目说了一个汉字占三个字节。因此打印每一行的前三个字节就行

# include <stdio.h>
char *change(char s[][20]);
int main (void)
{   int i;
    char *poem[4] = { "一叶轻舟向东流,", "帆梢轻握杨柳手,", "风纤碧波微起舞,", "顺水任从雅客悠。"};    /* 指针数组初始化 */
    char mean[10];
    for (i = 0; i < 4; i++){  /* 每行取第1个汉字存入mean */
      mean[2 * i] = *(poem[i]);
      mean[2 * i + 1] = *(poem[i] + 1);
    }
    mean[2 * i] = ’\0’;
    printf ("%s\n", mean);    /* 输出结果 */
    return 0;
}

 

 这也能过测试点

在 UTF - 8 编码下,中文字符通常占用 3 个字节。以下是一个 C 语言编写解密藏头诗程序,用于取出四句中文诗每句首字并连成字符串换行输出: ```c #include <stdio.h> #include <string.h> // 获取 UTF-8 字符长度 int get_utf8_char_length(unsigned char c) { if (c < 0x80) return 1; else if ((c & 0xE0) == 0xC0) return 2; else if ((c & 0xF0) == 0xE0) return 3; else if ((c & 0xF8) == 0xF0) return 4; return 1; // 默认情况 } // 解密藏头诗 void decrypt_acrostic(const char *poem) { int i = 0; char first_chars[13] = {0}; // 假设每句首字最长 3 字节,4 句最多 12 字节加结尾 &#39;\0&#39; int char_index = 0; int line_count = 0; while (poem[i] != &#39;\0&#39; && line_count < 4) { // 取每句首字 int len = get_utf8_char_length(poem[i]); for (int j = 0; j < len; j++) { first_chars[char_index++] = poem[i++]; } first_chars[char_index] = &#39;\0&#39;; // 跳过当前句子剩余部分 while (poem[i] != &#39;\n&#39; && poem[i] != &#39;\0&#39;) { int skip_len = get_utf8_char_length(poem[i]); i += skip_len; } if (poem[i] == &#39;\n&#39;) i++; // 跳过换行符 line_count++; } printf("%s\n", first_chars); } int main() { const char *poem = "春眠不觉晓\n处处闻啼鸟\n夜来风雨声\n花落知多少"; decrypt_acrostic(poem); return 0; } ``` 上述代码定义了 `get_utf8_char_length` 函数,用于获取 UTF - 8 字符的长度。`decrypt_acrostic` 函数遍历输入的诗,取出每句的首字并存储在 `first_chars` 数组中,最后将这些首字组成的字符串换行输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值