数制转换

数制转换

Time Limit:1s Memory Limit:1024k
Total Submit:3888 Accepted:1808
下载样例程序(PE)


Problem

设字符串A$的结构为: A$='m p'

其中m为数字串(长度<=20),而n,p均为1或2位的数字串(其中所表达的内容在2-10之间)

程序要求:从键盘上读入A$后(不用正确性检查),将A$中的数字串m(n进制)以p进制的形式输出.

例如:A$='48<10>8'

其意义为:将10进制数48,转换为8进制数输出.

Input

第一行为一整数N,表示该程序行有N组测试数据,接下来为N行,每行为一字符串,如题目描述。

Output

输出格式参照题目描述。

Sample Input

2
48<10>8
43<5>10

Sample Output

48<10>=60<8>
43<5>=23<10>

My Solution

#include <stdio.h>
int main(int argc, char* argv[])
{
    char m[40], *pm;
    char output[100];
    int n, p, N, i, temp;

    scanf("%d", &N);
    gets(m);
    for(i = 0; i < N; i++){
        gets(m);
        pm = m;
        while(*pm != '<') pm++;
        *pm++ = 0;
        pm += sscanf(pm, "%d>%d", &n, &p);

        pm = m;
        if(n != 10){
            temp = 0;
            while(*pm){
                temp = temp * n + (*pm - '0');
                pm++;
            }
        }else sscanf(pm, "%d", &temp);

        pm = output;
        if(p != 10){
            char *ptemp = output;
            while(temp > 0){
                *pm = temp % p + '0';
                temp /= p;
                pm++;
            }
            *pm = 0;
            pm --;
            while(ptemp < pm){
                temp = *pm; *pm = *ptemp;   *ptemp = temp;
                pm--;   ptemp++;
            }
        }else sprintf(pm, "%d", temp);

        printf("%s<%d>=%s<%d>/n", m, n, output, p);
    }

    return 0;
}

Memory: 28K
Time: 0ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值