• 进制转换和大数除法


    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <cstring>
    using namespace std;
    
    /*************************************************
    trip0函数在程序中比较关键的作用是在除二运算后,去除 
    高位的0,方便进行比较。 
    
    *************************************************/ 
    void trip0(string &s){
        while(!s.empty() && s[0] == '0')
            s.erase(s.begin());
    }
    string div_two(string s){
        string ans;
        int plus = 0;
        /*******************************************
        大数除法的部分 
        
        通过小时候所学习的基本的运算法则:通过除数除
        被除数的每一位 ,由高到低进行运算。余数到下
        一位。plus是余数。now是当前位的被除数。在编写
        程序的时候出现了一个问题,程序一直不运算完成,
        通过调试发现是由于plus未初始化,导致其中有一个
        很大的数。 
        
        ********************************************/
        for(int i = 0;i < s.length();i ++){
            int now = plus * 10 + (s[i] - '0');
            if(now >= 2){
                ans.push_back(char(now / 2 + '0'));
                plus = now % 2;
            }else{
                ans.push_back('0');
                plus = now;
            }
        }
        trip0(ans);
        return ans;
    }
    int main(){
        string tmp;
        while(cin >> tmp){
            string ans;
            trip0(tmp);
            while(!tmp.empty()){
                //此处的写法值得借鉴。通过指针的访问,实现起来十分的灵活。 
                int rail = (*(tmp.end() - 1) - '0') % 2;
                //string类型字符串的使用方式push_back 
                ans.push_back(char(rail + '0'));
                tmp = div_two(tmp);
            }
            reverse(ans.begin(),ans.end());
            if(!ans.empty()) cout << ans <<endl;
            else cout<< 0 << endl;
        }
        return 0;
    }

     进制转换 

  • 相关阅读:
    搜索栏+collectionView实现
    iOS密码框实现(二)取消确定按钮
    MotionManager 陀螺仪实现方式
    ubuntu常用命令
    Ubuntu 启用root账户
    How to solve “sudo: /etc/sudoers.d is world writable”
    ubuntu進入dos界面的方法
    炫耀一下hadoop學習成果
    fpdf使用標楷體
    win8磁盘占用100%的12种解决办法
  • 原文地址:https://www.cnblogs.com/shubin/p/10574495.html
Copyright © 2020-2023  润新知