首页 > 试题广场 >

单组_二维字符数组

[编程题]单组_二维字符数组
  • 热度指数:9152 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个 nm 列的二维字符数组 a ,第 i 行第 j 列元素的值为 a_{i,j}
请你对行和列都倒置,然后输出之。

输入描述:
第一行有两个整数 n\ (\ 1 \leq n \leq 10^3\ )m\ (\ 1 \leq m \leq 10^3\ )
随后 n 行,每行有 m 个字符,仅包含小写英文字符 。


输出描述:
输出一个二维字符数组。
示例1

输入

3 4
abcd
efgh
ijkl

输出

lkji
hgfe
dcba
#include <stdio.h>
#include <stdlib.h>

char * filpOnce(int count) {
    if (count > 0) {
        char* str = malloc(sizeof(char) * (count + 1));
        if (str != NULL) {
            str[count] = 0;
            scanf("%s", str);
            char *pStart = str, *pEnd = str + (count - 1);
            while (pEnd > pStart) {
                char tmp = *pStart;
                *pStart = *pEnd;
                *pEnd = tmp;
                ++pStart;
                --pEnd;
            }
            return str;
        }
    }
    return NULL;
}

int main() {
    int countX = 0, countY = 0;
    scanf("%d %d", &countX, &countY);
    char * strList[countX];
    int count = countX;
    while (count > 0) {
        strList[count - 1] = filpOnce(countY);
        count--;
    }
    int i = 0;
    while (i < countX) {
        printf("%s\n", strList[i]);
        free(strList[i]);
        i++;
    }
    return 0;
}
发表于 2025-03-04 09:56:37 回复(0)
C语言基础做法,依然需要注意换行符也属于字符,需要用到getchar(),整体思路比较好懂,结尾不要忘记换行。
#include <stdio.h>
int main() {
    int  a,b;
    scanf("%d%d",&a,&b);
    getchar();
    char  n[a][b];
    for (int  i=0;i<a;i++)
    {
        for (int  j=0;j<b;j++)
        {
            scanf("%c",&n[i][j]);
        }
        getchar();
    }
    for (int i=a-1;i>=0;i--)
    {
        for(int  j=b-1;j>=0;j--)
        {
            printf("%c",n[i][j]);
        }
        printf("\n");
    }
    return 0;
}

发表于 2025-02-07 20:41:34 回复(0)