题解 | #字符串分隔#

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7?tpId=37&tqId=21227&rp=1&ru=/exam/company&qru=/exam/company&sourceUrl=%2Fexam%2Fcompany%3FjumpFrom%3D%25E6%259C%25AA%25E7%2599%25BB%25E5%25BD%2595%25E9%25A6%2596%25E9%25A1%25B5&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <stdio.h>
#include<string.h>
#include<malloc.h>
int main() {
    char s[100];
    gets(s);
    int n = strlen(s);
    int t = n / 8;
    if (n % 8 != 0)
        t++;
    char** ns = malloc(sizeof(char*) * t);
    for (int i = 0; i < t; i++)
    {
        ns[i] = malloc(sizeof(char)*8);
    }
    int j = 0, p = 0;
    for (int i = 0; i < n; i++)
    {
        if (p == 8)
        {
            j++;
            p = 0;
        }
        ns[j][p++] = s[i];
    }
    if (p < 8)
        for (int i = p; i < 8; i++)
        {
            ns[j][i] = '0';
        }
    for (int i = 0; i < t; i++)
        printf("%s\n", ns[i]);
    return 0;
}

全部评论

相关推荐

05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务