Base Conversion—Arbitrary Base Multiplication Table

Title description:
Drawing a multiplication table of arbitrary bases is similar to:
Insert picture description hereAlgorithm idea:

  • Regarding the base conversion, I first thought of writing another function and then enumerating and then converting the base. However, this topic has the biggest feature, the result does not exceed two digits (in the corresponding base), but the base How to convert it? , Use arrays!
const int N = 17;
char c[N] = "0123456789ABCDEF";// 不局限于16进制内
  • After all, the multiplier is a numeric value and not a character. How does it mean that the left side multiplies? Use subscripts (c[i]).
int tmp=j*i;
printf("%c*%c=",c[j],c[i]);
if(tmp<p)	printf("%c  ",c[tmp]);
else		printf("%c%c ",c[tmp/p],c[tmp%p]);

All codes:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 17;
char c[N] = "0123456789ABCDEF";
int p;

int main(){
    
    
	scanf("%d",&p);
	for(int i=1;i<p;i++){
    
    
		for(int j=1;j<=i;j++){
    
    
			int tmp=j*i;
			printf("%c*%c=",c[j],c[i]);
			if(tmp<p)	printf("%c  ",c[tmp]);
			else		printf("%c%c ",c[tmp/p],c[tmp%p]);
		}
		printf("\n");
	}
	return 0;
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45666249/article/details/114606063