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

相关推荐
徐凤年_14 分钟前
rog_map参数理解
算法
春日见15 分钟前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
叩码以求索1 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana21 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
txzrxz1 小时前
单调队列讲解
数据结构·c++·算法·单调队列
不会就选b2 小时前
算法日常・每日刷题--<快排>4
算法
Keven_112 小时前
算法札记:树状数组的用途
数据结构·算法
用户677437175812 小时前
C++函数参数传递方式详解:string、string&、const string、const string&该怎么选?
算法
cpp_25013 小时前
P1540 [NOIP 2010 提高组] 机器翻译
数据结构·c++·算法·队列·noip·洛谷题解
晚笙coding3 小时前
LeetCode 98:验证二叉搜索树 —— 从局部判断到全局范围约束的递归思想
算法·leetcode·职场和发展