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

你可能感兴趣的文章
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
查看>>
org.springframework.beans.factory.BeanDefinitionStoreException
查看>>
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>