hdu 2031

来源:互联网 发布:csgo游戏优化 编辑:程序博客网 时间:2024/06/04 18:13

进制转换

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24871    Accepted Submission(s): 13902


Problem Description
输入一个十进制数N,将它转换成R进制数输出。
 

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
 

Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
 

Sample Input
7 223 12-4 3
 

Sample Output
1111B-11
 

寻人启事:2014级新生看过来!

进制转换

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24871    Accepted Submission(s): 13902


Problem Description
输入一个十进制数N,将它转换成R进制数输出。
 

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
 

Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
 

Sample Input
7 223 12-4 3
 

Sample Output
1111B-11
 
//@auther Yang Zongjun#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace std;#define PI acos(-1.0)#define EPS 1e-8const int MAXN = 105;const int INF = 2100000000;int n, r;char c[MAXN];int main(){    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);    while(cin >> n >> r)    {        if(n < 0) printf("-");        int t = abs(n) % r;        int temp = abs(n);        int cnt = 0;        memset(c, 0, sizeof(c));        while(temp != 0) //开始wa了一次,因为写为t != 0;应该是最后的商!= 0,而不是余数!= 0;        {            if(t >= 10)            {                t = (t - 10) + 65;            }            else t = t + '0';            c[cnt] = (char)(t);            cnt++;            temp = (int )(temp / r);            t = temp % r;        }        int len = strlen(c);        for(int i = len-1; i >= 0; i--)//注意要反向输出        {            printf("%c", c[i]);        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击