算法笔记之进制转换

1.5、进制转换

1022 D进制的A+B (20)
输入两个非负 10 进制整数 A 和 B (230
​​ −1),输出 A+B 的 D (1<D≤10)进制数。

输入格式:
输入在一行中依次给出 3 个整数 A、B 和 D。

输出格式:
输出 A+B 的 D 进制数。

输入样例:
123 456 8

      
    
输出样例:
1103

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,d;
    int c;
    cin>>a>>b>>d;
    c=a+b;
    stack<int> res;
    do
    {
        res.push(c%d);
        c=c/d;
    }
    while(c!=0);
    while(!res.empty())
    {
        printf("%d",res.top());
        res.pop();
    }
    return 0;
}

1037 在霍格沃茨找零钱 (20)
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。

输入格式:
输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut,其间用 1 个空格分隔。这里 Galleon 是 [0, 107
​​ ] 区间内的整数,Sickle 是 [0, 17) 区间内的整数,Knut 是 [0, 29) 区间内的整数。

输出格式:
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。

输入样例 110.16.27 14.1.28

      
    
输出样例 13.2.1

      
    
输入样例 214.1.28 10.16.27

      
    
输出样例 2-3.2.1

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int g,s,k,g1,s1,k1;
    scanf("%d.%d.%d %d.%d.%d",&g,&s,&k,&g1,&s1,&k1);
    int sum1=0,sum2=0;
    sum1=k+s*29+g*17*29;
    sum2=k1+s1*29+g1*17*29;
    int res_g=0,res_s=0,res_k=0;
    int sum=sum2-sum1;
    if(sum<0)
    {
        sum=-sum;
        printf("-");
    }
    while(sum>0)
    {
        res_k++;
        if(res_k==29)
        {
            res_s++;
            res_k=0;
        }
        if(res_s==17)
        {
            res_g++;
            res_s=0;
        }
        sum--;
    }
    printf("%d.%d.%d",res_g,res_s,res_k);
    return 0;
}

1019 General Palindromic Number (20)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N>0 in base b≥2, where it is written in standard notation with k+1 digits a
​i as ∑​i=0k(aib
​i
​​ ). Here, as usual, 0≤a
​i
​​ <b for all i and a
​k
​​ is non-zero. Then N is palindromic if and only if a
​i
​​ =a
​k−i
​​  for all i. Zero is written 0 in any base and is also palindromic by definition.

Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.

Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and b, where 0<N≤109
​​  is the decimal number and 2≤b≤109
​​  is the base. The numbers are separated by a space.

Output Specification:
For each test case, first print in one line Yes if N is a palindromic number in base b, or No if not. Then in the next line, print N as the number in base b in the form "a
​k
​​ a
​k−1
​​  ... a
​0
​​ ". Notice that there must be no extra space at the end of output.

Sample Input 1:
27 2

      
    
Sample Output 1:
Yes
1 1 0 1 1

      
    
Sample Input 2:
121 5

      
    
Sample Output 2:
No
4 4 1

#include<bits/stdc++.h>
using namespace std;
bool Judge(int a[],int num)
{
    for(int i=0;i<=num/2;i++)
    {
        if(a[i]!=a[num-1-i])
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int n,b;
    int a[40],num=0;
    cin>>n>>b;
    do
    {
        a[num++]=n%b;
        n/=b;
    }
    while(n!=0);
    if(Judge(a,num))
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
    for(int i=num-1;i>=0;i--)
    {
        printf("%d",a[i]);
        if(i!=0)
            printf(" ");
    }
    return 0;
}

1027 Colors in Mars (20)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input Specification:
Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:
For each test case you should output the Mars RGB value in the following format: first output #, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a 0 to its left.

Sample Input:
15 43 71

      
    
Sample Output:
#123456

题意:给定三个十进制数,转为十三进制输出。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int r,g,b;
    char radix[13]=
    {
        '0','1','2','3','4','5','6','7','8','9','A','B','C'
    };
    char res[10010];
    cin>>r>>g>>b;
    cout<<'#';
    printf("%c%c",radix[r/13],radix[r%13]);
    printf("%c%c",radix[g/13],radix[g%13]);
    printf("%c%c",radix[b/13],radix[b%13]);
    return 0;
}

问题 A: 又一版 A+B
时间限制: 1 Sec  内存限制: 32 MB
提交: 3294  解决: 894
[提交][状态][讨论版][命题人:外部导入]
题目描述
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。

输入
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。

输出
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。

样例输入
2 4 5
8 123 456
0
样例输出
1001
1103
提示
注意输入的两个数相加后的结果可能会超过intlong的范围。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    while(1)
    {
        int m;
        long long a,b;
        scanf("%d",&m);
        if(m==0)break;
        scanf("%lld %lld",&a,&b);
        long long c;
        c=a+b;
        stack<long long>res;
        do
        {
            res.push(c%m);
            c/=m;
        }
        while(c!=0);
        while(!res.empty())
        {
            printf("%lld",res.top());
            res.pop();
        }
        printf("\n");
    }
    return 0;
}

问题 B: 数制转换
时间限制: 1 Sec  内存限制: 32 MB
提交: 2758  解决: 694
[提交][状态][讨论版][命题人:外部导入]
题目描述
求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。
不同进制的表示符号为(01...9,a,b,...,f)或者(01...9,A,B,...,F)。

输入
输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。a,b是十进制整数,2 =< a,b <= 16。

输出
可能有多组测试数据,对于每组数据,输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(01...9,A,B,...,F)。

样例输入
4 123 10
样例输出
27

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    char n[40];
    char res[18]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    while(scanf("%d %s %d",&a,n,&b)!=EOF)
    {
        long c;
        sscanf(n,"%ld",&c);
        int y=0,product=1;
        while(c!=0)
        {
            y=y+(c%10)*product;
            c/=10;
            product=product*a;
        }
        c=y;
        int a[10010];
        int num=0;
        do
        {
            a[num++]=c%b;
            c=c/b;
        }
        while(c!=0);
        for(int i=num-1;i>=0;i--)
        {
            printf("%c",res[a[i]]);
        }
        printf("\n");
    }
    return 0;
}

不知道为啥AC不掉。先放着
问题 D: 八进制
时间限制: 1 Sec  内存限制: 32 MB
提交: 731  解决: 537
[提交][状态][讨论版][命题人:外部导入]
题目描述
输入一个整数,将其转换成八进制数输出。

输入
输入包括一个整数N(0<=N<=100000)。

输出
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。

样例输入
9
8
5
样例输出
11
10
5

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        printf("%o\n",n);
    }
    return 0;
}
%% 印出百分比符号,不转换。
%c 整数转成对应的 ASCII 字元。
%d 整数转成十进位。
%f 倍精确度数字转成浮点数。
%o 整数转成八进位。
%s 整数转成字符串。
%x 整数转成小写十六进位。
%X 整数转成大写十六进位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值