hdu 3344 Kakuro Extension Extension

很恶心的一道字符串模拟题

对于输入中的下划线,如果相邻右侧或下侧是数字,记录从数字起到下一个下划线出的所有数字的和

在这里用sprintf()着实很方便啊

sprintf(st, "%s", t)把数字t转化为字符串st

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 110
#define ll long long
using namespace std;

char ch[MAXN][MAXN];
string str;
int row, col;

void down(int a, int b) {
    int t = 0;
    int count = 0;
    for(int i=a+1; i<=row; ++i) {
        if(isdigit(ch[i][b])) {
            t += (ch[i][b]-'0');
            count++;
        }
        else break;
    }
    char st[20];
    sprintf(st, "%d", t);
    if(t<100 && t>9) {
        str[0] = '0';
        str.replace(1,2,st);
    }
    else if(t < 10) {
        str[0] = '0';
        str[1] = '0';
        str.replace(2, 1, st);
    } 
    else str.replace(0, 3, st);
    str[3] = '\\';
   // cout << "st = " << st << endl;
    return ;
}

void right(int a, int b) {
    int t = 0;
    for(int i=b+1; i<=col; ++i) {
        if(isdigit(ch[a][i]))
            t += (ch[a][i]-'0');
        else break;
    }
    char st[20];
    sprintf(st, "%d", t);
    if(t<100 && t>9) {
        str[4] = '0';
        str.replace(5,2,st);
    }
    else if(t < 10) {
        str[4] = '0';
        str[5] = '0';
        str.replace(6, 1, st);
    }
    else str.replace(4,3,st);
    str[3] = '\\';
    return ;
}

int main(void) {
    int T;
    cin >> T;
    while(T--) {
        cin >> row >> col;
        memset(ch, 0, sizeof(ch));
        str = "";

        for(int i=1; i<=row; ++i) {
            for(int j=1; j<=col; ++j) {
                cin >> ch[i][j];
                getchar();
            }
        }
        for(int i=1; i<=row; ++i) {
            for(int j=1; j<=col; ++j) {
                str = "XXXXXXX";
                if(isdigit(ch[i][j]))
                    str = ".......";
                else {
                    if(i<row && isdigit(ch[i+1][j])) {
                        down(i, j);
                    }
                    if(j<col && isdigit(ch[i][j+1])) {
                        right(i, j);
                    }
                }
                cout << str;
                if(j != col)
                    cout << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值