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 值)。通过这种特性,简单的数学运算就可以实现字符和数字的转换。

相关推荐
北顾笙9809 小时前
day38-数据结构力扣
数据结构·算法·leetcode
m0_629494739 小时前
LeetCode 热题 100-----14.合并区间
数据结构·算法·leetcode
xin_nai9 小时前
LeetCode热题100(Java)(5)普通数组
算法·leetcode·职场和发展
旖-旎10 小时前
深搜练习(组合)(5)
c++·算法·深度优先·力扣
fzil00110 小时前
自动投递简历 + 面试进度跟踪
人工智能·面试·职场和发展
@小码农10 小时前
2026年3月Scratch图形化编程等级考试一级真题试卷
开发语言·数据结构·c++·算法
其实防守也摸鱼10 小时前
面试常问问题总结--护网蓝队方向
网络·笔记·安全·面试·职场和发展·护网·初级蓝队
Wect11 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·算法·typescript
糖果店的幽灵11 小时前
决策树详解与sklearn实战
算法·决策树·sklearn
Lewiis11 小时前
趣谈排序算法
算法·排序算法