HDU

来源:互联网 发布:网站备案域名查询 编辑:程序博客网 时间:2024/05/29 19:59

题目链接:点击打开链接


/*  这题,倒是没有技巧上,需要特别提及的,但是要注意细节,细节就是负数和零的处理,如果遗漏,就很容易WA多次    此外,用一个 string 来将字母压栈,会比写 +'0',或者写 -10+'A' 这样的写法好一些,因为很容易忽视ASCII码和数字其实是不同的,一粗心就容易犯错,而,如果直接用一个 string 来处理,一定程度上可以避免出错的可能*/


#include <iostream>#include <string>#include <stack>#define rep(i, k, n) for (int i = k; i < (n); i++)using namespace std;const string str = "0123456789ABCDEF";int main(){int n, r, tp;while (cin >> n >> r){if (!n){cout << "0" << endl;continue;}if (n * r < 0)cout << "-"; //注意:计算机内负数以补码表示,所以必须对负数的情况,做一些特殊的处理 stack<char> s;while (n){tp = n % r;tp = tp > 0 ? tp : -tp;s.push(str[tp]);n /= r;}while (!s.empty()){cout << s.top();s.pop();}cout << endl;}return 0;}



原创粉丝点击