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