zoukankan      html  css  js  c++  java
  • 题目1208:10进制 VS 2进制(进制转换以及大数保存问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1208

    详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus

    参考代码:

    //
    //  1208 10进制 VS 2进制.cpp
    //  Jobdu
    //
    //  Created by PengFei_Zheng on 17/04/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cmath>
    #define MAX_SIZE 10010
    #define FROM 10
    #define TO 2
     
    using namespace std;
     
     
     
     
    int main(){
        char from[MAX_SIZE];
        while(scanf("%s",from)!=EOF){
            int len = (int)strlen(from);
            int size = 0;
     
            char to[MAX_SIZE];//逆序保存十进制数对应的二进制,正好是题目需要的逆置二进制
            int ans[MAX_SIZE];//保存最终计算结果
             
            while(true){//FROM 进制转为 TO 进制
                int i = 0;
                while(from[i]=='0' && i < len) i++;
                if(i == len) break;
                int remain = 0;
                for(; i < len ; i++){
                    int tmp = from[i] - '0' + remain * FROM;
                    from[i] = tmp/TO+'0';
                    remain = tmp%TO;
                }
                to[size++] = remain+'0';
            }
    //        cout<<to<<endl;
            int length = 1;
            ans[0] = 0;
            for(int i = 0; i < size ; i++){//1011 --> (((0*2+1)*2+0)*2+1)*2+1
                int carry = to[i] - '0';
                for(int j = 0 ; j < length ; j++){
                    if(j==0)
                        ans[j] = ans[j] * TO + carry;
                    else
                        ans[j] = ans[j] * TO;
                }
                for(int j = 0 ; j < length ; j++){
                    if(j == length - 1 && ans[j] >=10){
                        ans[j] = ans[j]%10;
                        ans[++j] = 1;
                        length++;
                    }
                    else if (ans[j]>=10){
                        ans[j] = ans[j]%10;
                        ans[j+1]++;
                    }
                }
            }
            for(int i = length - 1 ; i >= 0 ; i--){
                printf("%d",ans[i]);
            }
            printf("
    ");
        }
        return 0;
    }
     
     
    /**************************************************************
        Problem: 1208
        User: zpfbuaa
        Language: C++
        Result: Accepted
        Time:40 ms
        Memory:1520 kb
    ****************************************************************/
  • 相关阅读:
    链接数据库
    Ajax 密码验证
    for循环 打印菱形 空 和 实
    for 循环 正方形
    面向对象
    用正则表达式 匹配手机号码
    正则表达式
    js 中 == 和=== 有什么区别?
    js 删除
    封装函数增删改查
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6721459.html
Copyright © 2011-2022 走看看