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

相关推荐
高山上有一只小老虎6 分钟前
等差数列前n项的和
java·算法
sin_hielo14 分钟前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi20 分钟前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣20 分钟前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽20 分钟前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
牛客企业服务1 小时前
2025年AI面试防作弊指南:技术笔试如何识别异常行为
人工智能·面试·职场和发展
what_20181 小时前
list集合使用
数据结构·算法·list
hetao17338371 小时前
2025-11-13~14 hetao1733837的刷题记录
c++·算法
hansang_IR1 小时前
【题解】洛谷 P2476 [SCOI2008] 着色方案 [记搜]
c++·算法·记忆化搜索
趙卋傑1 小时前
常见排序算法
java·算法·排序算法