P8723 [蓝桥杯 2020 省 AB3] 乘法表

P8723 [蓝桥杯 2020 省 AB3] 乘法表

cpp 复制代码
#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>
#include <format>
string toBaseP(int  n,int  p){//这里不要取引用,因为会改变值
  if(n == 0) return 0;
  string res = ""; 
  while(n){
    int mod = n %p;
     res += (mod < 0?mod + '0':mod -10 + 'A');
     n /=p;
  }
  reverse(res.begin(),res.end());
  return res;
 
}
int main(){
  int n;
  cin>>n;
  for(int i=1;i<=n-1;i++){
    for(int j =1;j<=i;j++){
      int a = i *j;
      cout<<format("{}*{}={} ",toBaseP(i,n),toBaseP(j,n),toBaseP(a,n));
      /*这里的 n 是通过引用传递的(int &n),意味着在函数内部修改 n 会直接改变调
      用时的实际参数值。这在转换进制的过程中会破坏原始的值,
      导致调用 toBaseP 后 i 和 j 的值被修改。 */
      //cout<<i<<"*"<<j<<"="<<a/n<<a%n<<" ";
    }
  cout<<endl;
  }
}

C++ 中允许将字符和整数直接进行算术运算,这是因为字符在底层被存储为整数(ASCII 值)。通过这种特性,简单的数学运算就可以实现字符和数字的转换。

相关推荐
scx2013100416 小时前
20251201换根DP总结
算法·动态规划·换根dp
zd20057216 小时前
STREAMS指南:环境及宿主相关微生物组研究中的技术报告标准
人工智能·python·算法
TechNomad16 小时前
排序算法:基数排序算法
算法·排序算法
努力学算法的蒟蒻16 小时前
day43(12.24)——leetcode面试经典150
算法·leetcode·面试
jianfeng_zhu16 小时前
二叉树的一些基本运算
算法
元亓亓亓16 小时前
LeetCode--279. 完全平方数--中等
算法·leetcode·动态规划
TimberWill17 小时前
哈希-03-字母异位词分组
算法·哈希算法
轻微的风格艾丝凡17 小时前
matlab推导QPR离散公式并验证
算法·matlab·谐振
岁岁的O泡奶17 小时前
NSSCTF_crypto_[SWPU 2020]happy
经验分享·python·算法·密码学
EchoL、17 小时前
【论文阅读】SteganoGAN:High Capacity Image Steganography with GANs
论文阅读·人工智能·笔记·算法