博客
关于我
第十一届蓝桥杯 ——乘法表
阅读量: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/

你可能感兴趣的文章
Python APP自动化测试框架开发实战
查看>>
python argparse模块
查看>>
Python asyncio库的学习和使用
查看>>
Python AttributeError:“dict“对象没有属性“append“
查看>>
Python base64和hashlib模块
查看>>
python basic programs
查看>>
python bert_gen.py 报错Unable to load weights from pytorch checkpoint file for......
查看>>
python binascii.Error: Incorrect padding
查看>>
Python bool() 函数能否为无效参数引发异常?
查看>>
Python C 程序子进程在“for line in iter“处挂起
查看>>
Python Celery:自动化测试平台定时任务必备的三方库
查看>>
python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令
查看>>
Python CONNECT 4 CHECK WIN函数
查看>>
python cos,Python cos(90)和cos(270)不是0
查看>>
python进阶(4):Python 脚本文件重启自身进程
查看>>
python ctypes库中动态链接库加载方式
查看>>
python cv2 图像( np array )转 HObject
查看>>
python进阶(3):文件操作
查看>>
python cv2截取不规则区域图片
查看>>
python CV2裁剪图片并保存
查看>>