百度建立企业网站建设的目的,门户网站建设的建议,域名注册服务器,网站建设方案.doc试题 基础练习 十六进制转八进制
资源限制
内存限制#xff1a;512.0MB C/C时间限制#xff1a;1.0s Java时间限制#xff1a;3.0s Python时间限制#xff1a;5.0s
问题描述 给定n个十六进制正整数#xff0c;输出它们对应的八进制数。输入格式 输入的第…试题 基础练习 十六进制转八进制
资源限制
内存限制512.0MB C/C时间限制1.0s Java时间限制3.0s Python时间限制5.0s
问题描述 给定n个十六进制正整数输出它们对应的八进制数。输入格式 输入的第一行为一个正整数n 1n10。 接下来n行每行一个由0~9、大写字母A~F组成的字符串表示要转换的十六进制正整数每个十六进制数长度不超过100000。输出格式 输出n行每行为输入对应的八进制正整数。 【注意】 输入的十六进制数不会有前导0比如012A。 输出的八进制数也不能有前导0。样例输入 2 39 123ABC样例输出 71 4435274 【提示】 先将十六进制数转换成某进制数再由某进制数转换成八进制。 具体思路给出一个转换函数先将16进制数字转化为整数再将整数转化为八进制字符串
看change函数即可
版本一使用数字来作为中间过程过多的数据long long完全无法接收。
#includeiostream
#includevector
#includestring
#includecmath
#includestring
#includealgorithm
using namespace std;string change(string s)
{string res;long long num 0;int step 0;for (int i s.size() - 1; i 0; i--){switch (s[i]){case 1:num 1 * pow(16, step);break;case 2:num 2 * pow(16, step);break;case 3:num 3 * pow(16, step);break;case 4:num 4 * pow(16, step);break;case 5:num 5 * pow(16, step);break;case 6:num 6 * pow(16, step);break;case 7:num 7 * pow(16, step);break;case 8:num 8 * pow(16, step);break;case 9:num 9 * pow(16, step);break;case A:num 10 * pow(16, step);break;case B:num 11 * pow(16, step);break;case C:num 12 * pow(16, step);break;case D:num 13 * pow(16, step);break;case E:num 14 * pow(16, step);break;case F:num 15 * pow(16, step);break;}}int m;while (num){m num % 8;num / 8;res 0m;// res to_string(m);}reverse(res.begin(), res.end());return res;
}int main()
{int n;cin n;vectorstring v(n);vectorstring res(n);for (int i 0; i n; i){cin v[i];string temp change(v[i]);res[i] temp;}for (int i 0; i n; i){cout res[i];cout endl;}return 0;
} 【解法二】使用16-》2-》8进制
#include iostream
using namespace std;int main()
{int n;cinn;for(int i0; in; i){ string tow;string sixteen;string eight;cinsixteen;for(int j0; jsixteen.length(); j){switch(sixteen[j]){case 0:tow0000;break;case 1:tow0001;break;case 2:tow0010;break;case 3:tow0011;break;case 4:tow0100;break;case 5:tow0101;break;case 6:tow0110;break;case 7:tow0111;break;case 8:tow1000;break;case 9:tow1001;break;case A:case a:tow1010;break;case B:case b:tow1011;break;case C:case c:tow1100;break;case D:case d:tow1101;break;case E:case e:tow1110;break;case F:case f:tow1111;break;}}if(tow.length()%31)tow00tow;if(tow.length()%32)tow0tow;if(!(tow[0]0tow[1]0tow[2]0)){char temp;temp (tow[0]-0)*4(tow[1]-0)*2tow[2];eight temp;}for(int j3; jtow.length(); j3){eight(tow[j]-0)*4(tow[j1]-0)*2tow[j2];}couteightendl;}return 0;
} 文章转载自: http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.ndyrb.com.gov.cn.ndyrb.com http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.gmplp.cn.gov.cn.gmplp.cn http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.llmhq.cn.gov.cn.llmhq.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.qhydkj.com.gov.cn.qhydkj.com http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn