写了好长时间终于全部AC
查看原帖
写了好长时间终于全部AC
1536160
no_code楼主2024/12/18 23:00

主要就在于如何处理进位

1.进位时利用短除法先将其转化为p进制我们将其存到一个字符串中,最后将其逆置

2.因为要用AB……代替10之后的值,所以我们要有一个转换函数,将其转换为对应的字符。

#include <iostream>
#include <algorithm>
using namespace std;

char transfer(int i)
{
    if(i>9)
        return i - 10 + 65;
    else
    {
        return i+'0';
    }
}

void printMultiplicationTable(int N)
{
    int i = 0;
    for (i = 1; i < N; ++i)
    {
        int j = 0;
        for (j = 1; j <= i; ++j)
        {
            int ans = i * j;
            if (ans >= N)
            {
                string remainder;
                while (ans)
                {
                    char yushu = transfer(ans % N);
                    remainder.push_back(yushu);
                    ans /= N;
                }
                reverse(remainder.begin(), remainder.end());
                cout << transfer(i) << "*" << transfer(j) << "=" << remainder << " ";
            }
            else
            {
                cout << transfer(i) << "*" << transfer(j) << "=" << transfer(ans) << " ";
            }
        }
        cout << endl;
    }
}

int main()
{
    int N = 0;
    cin >> N;

    printMultiplicationTable(N);
    return 0;
}
2024/12/18 23:00
加载中...