• 【rlz03】十六进制转十进制


    Time Limit: 3 second
    Memory Limit: 2 MB

    问题描述

    输入一个十六进制数,编程转换为十进制数。
    整数部分不会超过65535,十六进制的小数部分不会超过2位。

    Sample Input

    FFFF
    Sample Output

    65535
    Sample Input

    200.AB
    Sample Output

    512.66796875

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=rlz03

    【题解】

    小数部分的权值为x1*1/16+x2*1/sqr(16)+x3*….
    还是用setprecision来控制输出;
    iomanip是它的头文件
    字母A..F和数字10..15的转换可以用一个函数来写;

    【完整代码】

    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <string>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    string s;
    
    int zs(char key)
    {
        if (isdigit(key))
            return key-'0';
        else
            return key-'A'+10;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        cin >> s;
        int pos = s.find('.',0);
        if (pos!=-1)
        {
            int num = 1;
            int zhengshu = 0;
            for (int i = pos-1;i >= 0;i--)
            {
                if (s[i]!='0')
                    zhengshu+=num*zs(s[i]);
                num*=16;
            }
            double num1 = (1.0/16.0),xiaoshu = 0;
            int len = s.size();
            for (int i = pos+1;i <= len-1;i++)
            {
                if (s[i]!='0')
                    xiaoshu+=num1*zs(s[i]);
                num1*=(1.0/16.0);
            }
            int lo;
            double temp = 0;
            temp = zhengshu;
            temp+=xiaoshu;
            cout <<setprecision(18)<<temp<<endl;
        }
        else
        {
            int num = 1;
            int zhengshu = 0,len = s.size();
            for (int i = len-1;i >= 0;i--)
            {
                if (s[i]!='0')
                    zhengshu+=num*zs(s[i]);
                num*=16;
            }
            printf("%d
    ",zhengshu);
        }
        return 0;
    }
    
  • 相关阅读:
    密码控件安全技术浅析及攻击实例
    一个QQ木马的逆向分析浅谈(附带源码)
    菜鸟开始学习SSDT HOOK((附带源码)
    leetcode229
    leetcode1401
    leetcode1400
    leetcode1399
    leetcode228
    leetcode223
    leetcode222
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626956.html
Copyright © 2020-2023  润新知