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

相关推荐
0 0 0几秒前
CCF-CSP 33-2 相似度计算(jaccard)【C++】考点:STL容器(set/map)
开发语言·c++·算法
每天要多喝水4 分钟前
图论Day38:孤岛基础
算法·深度优先·图论
瓦特what?12 分钟前
波 浪 排 序
c++·算法·排序算法
blackicexs21 分钟前
第六周第日天
数据结构·算法
逆境不可逃21 分钟前
LeetCode 热题 100 之 48.旋转图像
算法·leetcode·职场和发展
Frostnova丶30 分钟前
LeetCode 1022. 从根到叶的二进制数之和
算法·leetcode
不会敲代码134 分钟前
别再背柯里化面试题了,看完这篇你自己也会写
javascript·算法·面试
snowfoootball35 分钟前
优先队列/堆 题目讲解
学习·算法
SamtecChina202336 分钟前
Samtec连接器设计研究 | 载流量:温升为什么重要?
大数据·网络·人工智能·算法·计算机外设
程序员南飞42 分钟前
排序算法举例
java·开发语言·数据结构·python·算法·排序算法