欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Which Base is it Anyway (进制) ?

程序员文章站 2022-03-13 17:50:06
...

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

 

一、问题:

Description

Programming languages such as C++ and Java can prefix characters to denote the base of constant integer values. For example, hexadecimal (base 16) constants are preceded by the string “0x”. Octal (base 8) values are preceded by the character “0” (zero). Decimal (base 10) values do not have a prefix. For example, all the following represent the same integer constant, albeit in different bases. 0x1234 011064 4660 The prefix makes it clear to the compiler what base the value is in. Without the “0x” prefix, for example, it would be impossible for the compiler to determine if 1234 was hexadecimal. It could be octal or decimal. For this problem, you will write a program that interprets a string of decimal digits as if it were an octal value, a decimal value or a hexadecimal value.

Input

The first line of input contains a single decimal integer P, (1 ≤ P ≤ 10000), which is the number of data sets that follow. Each data set should be processed identically and independently. Each data set consists of a single line of input. It contains the data set number, K, followed by a single space, followed by a string of at most 7 decimal digits.

Output

For each data set there is one line of output. The single output line consists of the data set number, K, followed by a space followed by 3 space separated decimal integers which are the value of the input as if it were interpreted to as octal, decimal and hexadecimal respectively. If the input value cannot be interpreted as an octal value, use the value ‘0’.

Sample Input

4

1 1234

2 9

3 1777

4 129

Sample Output

1 668 1234 4660

2 0 9 9

3 1023 1777 6007

4 0 129 297

 

二、题意:

第一行数字 T 代表测试样例个数。

接下来的 T 行每行第一个数 cnt 表示序号,后面输入一个串。

求:把这个串分别当成八进制、十进制、十六进制,并求这三种情况下对应的十进制的值,若其不可能为该进制状态,则输出0。

 

三、思路:

先用 atof 转成十进制,再分别求对应的八进制和十六进制的值,计算该数在八进制和十六进制下的十进制值很简单,比如1234在八进制状态下的十进制值即为1*128+2*64+3*8+4*1=668 。同时,我们也知道,十进制的值输出和输入本身一致,只不过输入的是串,我们还知道,只有八进制条件下是有可能不存在的,因为每一位不会超过10。

 

四、代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-10
#define mod 1e9+7
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define mem(a,b) memset(a,0,sizeof(a))
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define fork(a,b) for(int k=a;k<=b;k++)
#define ifor(a,b) for(int i=a;i>=b;i--)
#define jfor(a,b) for(int j=a;j>=b;j--)
#define kfor(a,b) for(int k=a;k>=b;k--)
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)

using namespace std;
typedef long long LL;

int main()
{
    int T,cnt;
    char a[10];
    read(T);
    while(T--)
    {
        scanf("%d %s",&cnt,a);
        int num=atof(a);
        int x=0,y=0;
        int z=num,p=1;
        while(z)
        {
            if(z%10>=8)
            {
                x=0;
                break;
            }
            x+=(z%10)*p;
            z/=10;
            p*=8;
        }
        z=num,p=1;
        while(z)
        {
            y+=(z%10)*p;
            z/=10;
            p*=16;
        }
        printf("%d %d %d %d\n",cnt,x,num,y);
    }
    return 0;
}

 

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

 

相关标签: 进制