腾讯笔试题:输入16的倍数个字符串,按格式排版输出

    xiaoxiao2021-04-15  70

    题目描述:

    从屏幕上接收16的倍数个字符串,回车后按照如下格式输出:

    上图有箭头的地方就是要求有两个空,其它间隔一个空格,以16进制输出

    /* 测试字符串 * abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl * */ #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string str; cout<<"请输入字符串:"<<endl; cin>>str; cout<<endl; if((str.size()%16) != 0){ /*注意鲁棒性*/ cout<<"输入的字符串不是16的整数倍"<<endl; return 1; } cout<<hex;/*以16进制显示,一次设置永久有效*/ for(int i=0,j=0;i<str.size();i++){ if(i%16 == 0){ if(i != 0) cout<<endl; cout<<setw(8)<<setfill('0')<<i<<' '; } if(i%16 <= 15){ if(i%8 == 0) cout<<' '; cout<<(int)str[i]<<' '; } if(i%16 == 15){ cout<<' '; for(int j=i-15;j<=i;j++) cout<<str[j]; cout<<endl; } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-671123.html

    最新回复(0)