Sicily 1007. To and Fro

本文介绍了一种数组下标操作的方法,通过输入一串字符和指定的列数,程序能够根据列数将字符串重新排列,并按列输出结果。具体实现包括了正向和反向填充数组的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
简单的数组下标操作
做的时候最好在草稿纸上列举实例走流程以免思维错误 
*/
/*
Run Time: 0secs
Run Memory: 312KB
*/

#include <iostream>
#include <string>

using namespace std;

int col;      //2-20
int row;
string input;   //1-200

int main()
{
    while (cin>>col && col){
        cin >> input;
        row = input.length() / col;
        
        char result[row][col];
        for(int i=0; i<row; i++){
            int base = col * i;
            if(i%2 == 0){    //从左到右 
                for(int j=0; j<col; j++){
                     char buf = input[base + j];
                     result[i][j] = buf;
                } 
            }else{
                for(int j=0; j<col; j++){
                    char buf = input[base + (col-j-1)];
                    result[i][j] = buf;
                }
            }
        }
        
        for(int j=0; j<col; j++){
            for(int i=0; i<row; i++){
                cout << result[i][j];
            }
        }
        cout << endl;
    } 
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值