蓝桥杯每日一题2023.11.29

题目描述

cpp 复制代码
#include <stdio.h>
#include <string.h>

void StringInGrid(int width, int height, const char* s)
{
    int i,k;
    char buf[1000];
    strcpy(buf, s);
    if(strlen(s)>width-2) buf[width-2]=0;
    
    printf("+");
    for(i=0;i<width-2;i++) printf("-");
    printf("+\n");
    
    for(k=1; k<(height-1)/2;k++){
        printf("|");
        for(i=0;i<width-2;i++) printf(".");
        printf("|\n");
    }
    
    printf("|");
    
    printf("%*s%s%*s",__________________);
              
    printf("|\n");
    
    for(k=(height-1)/2+1; k<height-1; k++){
        printf("|");
        for(i=0;i<width-2;i++) printf(".");
        printf("|\n");
    }    
    
    printf("+");
    for(i=0;i<width-2;i++) printf("-");
    printf("+\n");    
}

int main()
{
    StringInGrid(10,4,"abcd123");
    return 0;
}

题目分析

题目知识点:

*%s的含义:printf("%s",10,s):意思是输出字符串s,但至少占10个位置,不足的在字符串s左边补空格,这里等同于printf("%10s",s);

填空部分是填出缺少字母的那一行,进行尝试填补即可,将两边都取一半左右空格,左边少取,右边多取即可以符合题意

cpp 复制代码
#include <stdio.h>
#include <string.h>

void StringInGrid(int width, int height, const char* s)
{
    int i,k;
    char buf[1000];
    strcpy(buf, s);
    if(strlen(s)>width-2) buf[width-2]=0;
    //printf("%d:",(int)strlen(buf));

    printf("+");
    for(i=0;i<width-2;i++) printf("-");
    printf("+\n");

    for(k=1; k<(height-1)/2;k++){
        printf("|");
        for(i=0;i<width-2;i++) printf(".");
        printf("|\n");
    }

    printf("|");
    //printf("%d:\n",sz);
    printf("%*s%s%*s",(width - 2 - strlen(buf)) / 2, "", buf, (width - 1 - strlen(buf)) / 2, "");  //填空

    printf("|\n");

    for(k=(height-1)/2+1; k<height-1; k++){
        printf("|");
        for(i=0;i<width-2;i++) printf(".");
        printf("|\n");
    }

    printf("+");
    for(i=0;i<width-2;i++) printf("-");
    printf("+\n");
}

int main()
{
    StringInGrid(10,4,"abcd123");
    return 0;
}
相关推荐
AvocadoZeroOneOI1 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz38993 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
橘子137 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘18 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴21 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David21 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Pniubi1 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
AIgorithmGEEK1 天前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
上海潭龙东海科技有限公司1 天前
智慧堆场动态模型三维实时孪生重构智能管控
大数据·网络·数据库·算法·重构
阿庆_AI研发工程师1 天前
Stanford CS336 Lecture 10:大语言模型推理与性能优化
算法