博客
关于我
第十一届蓝桥杯 ——乘法表
阅读量:564 次
发布时间:2019-03-09

本文共 702 字,大约阅读时间需要 2 分钟。

进制转换

#include

#include
using namespace std;

int P;char w[36];string change(int n) {string s;while (n) {s += w[n % P];n /= P;}reverse(s.begin(), s.end());return s;}

int main() {cin >> P;for (int i = 0; i <= 9; i++) w[i] = (char)('0' + i);for (int i = 10; i <= 35; i++) w[i] = (char)('A' + i - 10);for (int i = 1; i < P; i++) {for (int j = 1; j <= i; j++) {cout << w[i] << '*' << w[j] << '=' << change(i * j) << ' ';}cout << endl;}return 0;}

题解

进制转换是解决本题的关键。我们需要将十进制数转换为给定进制数,以便正确生成乘法表。具体来说,函数change(int n)负责将十进制数n转换为P进制字符串。转换过程如下:首先,用模运算获取每一位的数字,接着将这些数字按顺序拼接成字符串。最后,通过reverse函数将字符串反转,得到正确的P进制表示。

在main函数中,我们首先读取进制数P。然后为数字0-9和字母A-Z分配相应的字符。接下来,遍历每一个可能的乘数i和j,生成对应的乘法表条目。使用change函数将乘积i*j转换为P进制,并按照指定的格式输出结果。

转载地址:http://dcopz.baihongyu.com/

你可能感兴趣的文章
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>