【水题】hdoj1335 Basically Speaking

来源:互联网 发布:百度网盘下载 知乎 编辑:程序博客网 时间:2024/06/06 17:22

代码我基本参照别人写的

因为不是很了解进制转换的itoa函数。所以差点很脑残的写一个各种进制转换。对自己也是蜜汁服气

但是一道水题也是收获很多,下面一一道来。

【题目】

Problem Description 

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features: 
It will have a 7-digit display.

Its buttons will include the capital letters A through F in addition to the digits 0 through 9.

It will support bases 2 through 16.

Input 
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.

Output 
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print “ERROR” (without the quotes) right justified in the display.

Sample Input

<code class="hljs autohotkey has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1111000</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1111000</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2102101</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2102101</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12312</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>     <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">A</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1234567</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>   ABCD <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

Sample Output

<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>     <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">78</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1765</span>    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>CA  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ERROR</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11001</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>D687   D071</code>
【code】

#include<iostream>#include<cmath>#include<algorithm>#include<ctype.h>using namespace std;int main() {int before, after, result;char c[50];while (cin >> c >> before >> after) {result =0;reverse(c, c +strlen(c));for (int i = 0; i < strlen(c); i++) {if (isalpha(c[i]))result = result + (c[i] - 'A'+10) * pow(before, i);else result = result + (c[i] - '0')*pow(before, i);}_itoa_s(result, c, after);for (int i = 0; i < strlen(c); i++)c[i] = toupper(c[i]);if (strlen(c)<= 7)printf("%7s\n",c);elseprintf("%7s\n", "ERROR");}return 0;}

一开始一直不AC后来才发现因为vs使用习惯我一直powf了,结果导致细微末节出了错误。//一失足成不AC啊

主要思路就是将某进制转成十进制然后再转成某进制(itoa函数)

1reverse给好评

2十进制转换那个判断那里我一直很愚蠢的记错了isalnum()他判断的是数字和字母,isalpha()判断的是字母,前面各种bug

3toupper( )忘了包含头文件#include<ctype.h>编译器也傲娇的没有提示结果出来一堆小写

4意外收获itoa(整数(十进制),字符数组,要转换进制)//进制转换神器,诚不欺我。起码在我眼里省事很多。//由于编译器有差异所以变成了_itoa_s

5适当时候穿插printf吧,虽说写惯了c++可是c有些东西又快又方便


0 0
原创粉丝点击