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

相关推荐
小南家的青蛙5 分钟前
LeetCode面试题 04.06 后继者
算法·leetcode·职场和发展
IT·小灰灰12 分钟前
基于Python的机器学习/数据分析环境搭建完全指南
开发语言·人工智能·python·算法·机器学习·数据分析
wefg11 小时前
【C++】智能指针
开发语言·c++·算法
搂鱼1145141 小时前
一类判断包含颜色整体的题目
算法
Demon--hx1 小时前
[c++]string的三种遍历方式
开发语言·c++·算法
无敌最俊朗@1 小时前
力扣hot100 - 合并两个有序链表21
算法·leetcode·链表
墨染点香1 小时前
LeetCode 刷题【168. Excel 表列名称】
算法·leetcode·职场和发展
hans汉斯1 小时前
基于改进YOLOv11n的无人机红外目标检测算法
大数据·数据库·人工智能·算法·yolo·目标检测·无人机
Swift社区2 小时前
LeetCode 431 - 将 N 叉树编码成二叉树
算法·leetcode·职场和发展
子豪-中国机器人2 小时前
1030-csp 2019 入门级第一轮
算法