hdu 4054 模拟 练习十六进制输出

本文分享了解决HDU 4054题目中输出格式问题的经验,详细介绍了如何正确使用C++的输出格式控制,特别是%02x用于确保输出为两位并填充前导0的方法。

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

http://acm.hdu.edu.cn/showproblem.php?pid=4054

貌似一般区域赛都会有一道水题

这道题PE了一次  因为输出每个数其实是两个位 如果用空格补齐的话  应该用两个空格 我用了一个空格,,,

学到:
1、%x  十六进制输出  可以输出整型,字符型等等

2、%02x  保证两位 而且会输出先导0的两位


//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000;

const int MAXN = 4096 +200;
char s[MAXN];
int len;

char ch(char x)
{
    if(x>='a' && x<='z')return x-32;
    if(x>='A' && x<='Z')return x+32;
    return x;
}

void solve()
{
    int cnt;
    for(int i=0;i<len;i+=16)
    {
        if(i%16 == 0)
            printf("%04x:",i);
        cnt=0;
        for(int j=i;j<len && j<16+i;j+=2)
        {
            printf(" %02x",s[j]);
            if(j+1>=len)printf("  ");
            else printf("%02x",s[j+1]);
            cnt+=2;
        }
        //putchar(' ');
        int cc=0;
        while(cnt<16)
        {
            //printf("%d%d%d%d%d",cc,cc,cc,cc,cc);
            printf("     ");
            cnt+=2;
        }
        putchar(' ');//putchar('|');
        for(int j=i;j<len && j<16+i;j++)
            putchar(ch(s[j]));
        putchar('\n');
    }
}

int main()
{
    //IN("hdu4054.txt");
    while(gets(s))
    {
        len=strlen(s);
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值