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

相关推荐
CS创新实验室17 分钟前
算法、齿轮与硅基大脑:数值计算发展简史
人工智能·算法·数值计算
海石2 小时前
1563分的简单题,可能就简单在能被暴力AC
算法·leetcode
海石2 小时前
1400分的dp汗流浃背之【交替子数组计数】
算法·leetcode
奋发向前wcx2 小时前
P2590 树的统计 题目解析
数据结构·算法·深度优先
imbackneverdie3 小时前
AI4S不止于分子药物:以MedPeer为代表的科研基建打开产业新增量
大数据·人工智能·算法·aigc·科研·学术·ai 4s
额鹅恶饿呃4 小时前
C语言中的数据结构和变量
c语言·数据结构·算法
运行时记录5 小时前
prompt-optimizer skill
算法
万法若空5 小时前
【数据结构-哈希表】哈希表原理
数据结构·算法·散列表
退休倒计时6 小时前
【每日一题】LeetCode 437. 路径总和 III TypeScript
算法·leetcode·typescript
学逆向的6 小时前
汇编——内存
开发语言·汇编·算法·网络安全