关于这道题很多人50分的分析

如果你觉得你写的很正确却50分,先不要看下面的题解,这里提供一个自测点:

输入:
00000001001001011101110110111
000010101010101010101000000000011111111111

输出:
29ETN
5ALA01VV

你输出的结果是不是很奇怪?(不是的可以走了)

那么请往下看

首先,很多人都想到可以先把二进制转成十进制,再转成三十二进制(包括我)

于是就写出了类似于这个的代码:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ll long long
#define ull unsigned long long
#define de double
#define fs(n) fixed << setprecision(n)
#define Pq priority_queue
#define PII pair<int, int>
#define endl "\n"
#define f(i, a, b) for (int i = a; i <= b; i++)
using namespace std;
const int maxn = INT_MAX;
inline void solve()
{
    string s;
    while (cin >> s)
    {
        ll ans = 0, k = 0;
        for (int i = s.length() - 1; i >= 0; i--)
            ans += (s[i] - '0') * pow(2, k++);
        // cout << "\nans:" << ans << endl;
        vector<int> a(1000, 0);
        int o = 1;
        a[1] = ans;
        while (a[o] >= 32)
        {
            a[o + 1] = a[o] / 32;
            a[o] %= 32;
            o++;
        }
        for (int i = o; i >= 1; i--)
        {
            if (a[i] >= 10)
                cout << char(a[i] - 10 + 'A');
            else
                cout << a[i];
        }
        cout << endl;
    }
}
int main()
{
    IOS;
    int test = 1;
    // cin >> test;
    while (test--)
        solve();
    return 0;
}

当然了,只有50分。

为什么???明明很正确啊!!!

是不是?!

alt

当然不是

alt

我再认真地一思考,发现了不对之处:

二进制转三十二进制,是一对五(例如11111(2)=V(32),11101(2)=T(32)),也就是说,我们每输入五个数(00000除外),他一定给我们整出一个三十二进制的数!

那么问题来了:

用上面个方法最多能输出几位三十二进制数呢?

假设我们用范围最大的 unsigned long long (0~18446744073709551615)

你会崩溃地发现,撑死也就转出四五位三十二进制数!

(最大就是11111111111111111111(2)=VVVVV(32))

也就是说,当二进制数异常的长(给你输入个20位的!)的时候,我们上面的方法完全无能为力了。

因此,这道题的正解还是要用字符串:

AC代码如下:(我知道你们只看这个)

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ll long long
#define ull unsigned long long
#define de double
#define fs(n) fixed << setprecision(n)
#define Pq priority_queue
#define PII pair<int, int>
#define endl "\n"
#define f(i, a, b) for (int i = a; i <= b; i++)
using namespace std;
const int maxn = INT_MAX;
inline void solve()
{
    string a;
    while (cin >> a)//多组输入
    {
        string s;
        bool f = true;
        if (a.length() % 5 != 0)
        {
            for (int i = 1; i <= 5 - a.length() % 5; i++)
            {
                s += '0';
            }
        }
        s += a;//补0
        for (int i = 0; i < s.length(); i += 5)
        {
            int ans = 0, o = 4;
            for (int j = 0; j < 5; j++)
            {
                ans += (s[i + j] - '0') * pow(2, o);
                o--;//针对每五位再去计算,结果就一定在0~31之间
            }
            if (ans == 0 && f)
            {
                continue;//如果是0000000001,那么前面五个零转成的0就不输出了
            }
            else
            {
                f = false;
            }
            if (ans < 10)
            {
                cout << ans;
            }
            else
            {
                cout << char(ans - 10 + 'A');
            }//“朴实无华的输出”
        }
        cout << endl;
    }
}
int main()
{
    IOS;
    int test = 1;
    // cin >> test;
    while (test--)
        solve();
    return 0;
}

蒟蒻的第一篇题解,不喜勿喷

END.