zoukankan      html  css  js  c++  java
  • 洛谷P1143 进制转换

    题目描述

    请你编一程序实现两种不同进制之间的数据转换。

    输入输出格式

    输入格式:

    输入数据共有三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用大写字母A~F表示数码10~15,并且该n进制数对应的十进制的值不超过1000000000,第三行也是一个正整数,表示转换之后的数的进制m(2≤m≤16)。

    输出格式:

    输出仅一行,包含一个正整数,表示转换之后的m进制数。

    输入输出样例

    输入样例#1: 复制
    16
    FF
    2
    
    输出样例#1: 复制
    11111111



    先把一个数转换成十进制
    转换的时候用乘权累加法
    然后再转换成m进制
    转换的时候用不断取模法(xjb扯的) :joy:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define LL long long 
    using namespace std;
    const int MAXN=1e6+10;
    const int mod=1e9+7;
    inline int read()
    {
        char c=getchar();int flag=1,x=0;
        while(c<'0'||c>'9')    {if(c=='-')    flag=-1;c=getchar();}
        while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*flag;
    }
    int n,m;
    char s[MAXN];
    int a[MAXN],b[MAXN],tot=-1,ans=0,now=1;
    int out[MAXN],cnt=0;
    int main()
    {
        for(int i='0';i<='9';i++)    a[i]=++tot,b[tot]=i;
        for(int i='A';i<='Z';i++)    a[i]=++tot,b[tot]=i;
        n=read();
        scanf("%s",s+1);m=read();
        int ls=strlen(s+1);
        for(int i=ls;i>=1;i--)    ans+=a[ s[i] ]*now,now=now*n;
        now=0;
        while(ans)        out[++cnt]=ans%m,ans/=m;
        for(int i=cnt;i>=1;i--)    printf("%c",b[ out[i] ]);
        return 0;
    }

      

  • 相关阅读:
    线程池中的scheduleAtFixedRate scheduleWithFixedDelay区别
    几道MySQL问题
    【SQL server 2012】复制数据库到另一台机器上
    LeetCode 98. 验证二叉搜索树
    深度学习知识点
    Graph Network Notes
    剑指 Offer 33. 二叉搜索树的后序遍历序列
    剑指 Offer 29. 顺时针打印矩阵
    LeetCode 54. 螺旋矩阵
    LeetCode 50. Pow(x, n)
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7805942.html
Copyright © 2011-2022 走看看