7.24日进制转换测试总结
7.24日进制转换测试总结
比赛传送门
补充知识点:
\(1.\)
\(X\) 进制 \(\to\) 十进制
位值累加法
所有进制位的最小单位都是1
①写出所有位的位号
②基数的位号次方 \(\implies\) 位权
③十进制数字 \(=\) 位权 \(\times\) 该位上的数字之和
\(Code:\)
int to_ten(string op, int x)
{
int sum = 0;
int len = op.size();
for (int i = 0;i < len;++i)
{
if (op[i] >= '0' and op[i] <= '9')
{
sum = sum * x + (op[i] - '0');
}
if (op[i] >= 'A' and op[i] <= 'Z')
{
sum = sum * x + (op[i] - 'A' + 10);
}
}
return sum;
}
直接写成函数,需要的时直接调用 \(\Uparrow\)
\(2.\)
十进制 \(\to\) \(X\) 进制
①每次除基数,得到商 (下一次的被除数) ,余数就是当前的最低位的数值
②继续被除数除基数
③最后的ans就是所有的余数反过来
\(Code:\)
string to_x(int a, int x)
{
while (a)
{
string s = "";
if (a % x < 10)
{
s = char('0' + a % x) + s;
}
else if (a % x >= 10)
{
s = char('A' + a % x - 10) + s;
}
a /= s;
}
return s;
}
直接写成函数,需要的时直接调用 \(\Uparrow\)
Q1: B3620 x 进制转 10 进制
题目传送门 提交记录传送门
使用位值累加法,具体方法看下面 \(\Downarrow\)
所有进制位的最小单位都是1
①写出所有位的位号
②基数的位号次方 \(\implies\) 位权
③十进制数字 \(=\) 位权 \(\times\) 该位上的数字之和
本题目一次过
\(Code:\)
#include <bits/stdc++.h>
using namespace std;
int to_ten(int n, string op)
{
int j = 0;
int len = op.size();
for (int i = 0;i < len;++i)
{
if (op[i] >= '0' and op[i] <= '9')
{
j = n * j + int(op[i] - '0');
}
else
{
j = n * j + int(op[i] - 'A' + 10);
}
}
return j;
}
int main()
{
int x;
string s;
cin >> x >> s;
cout << to_ten(x, s);
return 0;
}
Q2: B3619 10 进制转 x 进制
题目传送门 提交记录传送门
具体方法:
①每次除基数,得到商 (下一次的被除数) ,余数就是当前的最低位的数值
②继续被除数除基数
③最后的ans就是所有的余数反过来
原始代码(70分):
#include <bits/stdc++.h>
using namespace std;
string to_x(int x, int op)
{
string ans = "";
while (x > 1)
{
if (x % op < 10)
{
ans = char(x % op + '0') + ans;
}
else
{
ans = char(x % op - 10 + 'A' ) + ans;
}
x /= op;
}
return ans;
}
int main()
{
int a, b;
cin >> a >> b;
cout << to_x(a, b);
return 0;
}
问题分析:
问题:
while (x > 1) <-<-<-
{ ^
|
if (x % op < 10)
{
ans = char(x % op + '0') + ans;
}
else
{
ans = char(x % op - 10 + 'A' ) + ans;
}
x /= op;
}
while (x > 1) 这里应该是 while (x >= 1)
因为 \(x\) 等于 \(0\) 是还可以继续运行,这里一定要到位\(!!!\)
\(AC\) \(Code:\)
#include <bits/stdc++.h>
using namespace std;
string to_x(int x, int op)
{
string ans = "";
while (x)
{
if (x % op < 10)
{
ans = char(x % op + '0') + ans;
}
else
{
ans = char(x % op - 10 + 'A' ) + ans;
}
x /= op;
}
return ans;
}
int main()
{
int a, b;
cin >> a >> b;
cout << to_x(a, b);
return 0;
}
Q3: P2084 进制转换
题目传送门 \(无提交记录_{没做:(}\)
问题分析:
- 读取输入:首先读取输入的M和N。
- 转换进制:从N的最低位开始,将每一位数字转换为M的幂次,并计算其对应的十进制值。
- 构建输出:根据转换的结果,构建输出的式子。注意省略系数为0的项。
- 输出结果:将构建好的式子输出。
\(Code:\)
#include<bits/stdc++.h>
using namespace std;
string h;
int r, y = 0;
int main()
{
cin >> r >> h;
int e = h.size();
for (int i = 0; i < e; ++i)
{
if (h[i] != '0')
{
if (y != 0)
{
cout << '+';
}
else
{
y++;
}
cout << h[i] << '*' << r << '^' << e - i - 1;
}
}
return 0;
}
Q4: P8723 [蓝桥杯 2020 省 AB3] 乘法表
题目传送门 提交记录传送门
原始代码(20分,打表):
#include <bits/stdc++.h>
using namespace std;
//string to_x(int x, int op)
//{
// string ans = "";
// while (x > 1)
// {
// if (x % op < 10)
// {
// ans = char(x % op + '0') + ans;
// }
// else
// {
// ans = char(x % op - 10 + 'A' ) + ans;
// }
// x /= op;
// }
// return ans;
//}
//void chengfabiao(int a)
//{
// if (a == 10)
// {
// for (int i = 1;i <= a - 1;++i)
// {
// for (int j = 1;j <= i;++j)
// {
// cout << i << '*' << j << '=' << i * j << ' ';
// }
// cout << "\n";
// }
// return ;
// }
// if (a < 10)
// {
// for (int i = 1;i <= a - 1;++i)
// {
// for (int j = 1;j <= i;++j)
// {
// cout << to_x(a, i) << '*' << to_x(a, j) << '=' << to_x(a ,i * j) << ' ';
// }
// cout << "\n";
// }
// }
// return ;
//}
//int main()
//{
// int p;
// cin >> p;
// chengfabiao(p);
// return 0;
//}
int main()
{
int a;
cin >> a;
if (a == 2)
{
cout << "1*1=1";
return 0;
}
else if (a == 4)
{
cout << "1*1=1" << "\n";
cout << "2*1=2 2*2=10" << "\n";
cout << "3*1=3 3*2=12 3*3=21" << "\n";
}
else if (a == 8)
{
cout << "1*1=1" << "\n";
cout << "2*1=2 2*2=4" << "\n";
cout << "3*1=3 3*2=6 3*3=11" << "\n";
cout << "4*1=4 4*2=10 4*3=14 4*4=20" << "\n";
cout << "5*1=5 5*2=12 5*3=17 5*4=24 5*5=31" << "\n";
cout << "6*1=6 6*2=14 6*3=22 6*4=30 6*5=36 6*6=44" << "\n";
cout << "7*1=7 7*2=16 7*3=25 7*4=34 7*5=43 7*6=52 7*7=61" << "\n";
}
else if (a == 10)
{
for (int i = 1;i <= 9;++i)
{
for (int j = 1;j <= i;++j)
{
cout << i << '*' << j << '=' << i * j << ' ';
}
cout << "\n";
}
}
else if (a == 16)
{
for (int i = 1;i <= 9;++i)
{
for (int j = 1;j <= i;++j)
{
cout << i << '*' << j << '=' << i * j << ' ';
}
cout << "\n";
}
cout << "A*1=10 A*2=20 A*3=30 A*4=40 A*5=50 A*6=60 A*7=70 A*8=80 A*9=90 A*10=100" << "\n";
cout << "B*1=11 B*2=22 B*3=33 B*4=44 B*5=55 B*6=66 B*7=77 B*8=88 B*9=99 B*10=110 B*11" << "\n";
}
return 0;
}
打表居然拿了20分
分析问题:
- 先用双重循环枚举相应的 \(i\) 和 \(j\)
- 再将对应的 \(i\) 和 \(j\) 用 \(P\) 进制输出
- 后面紧接着 \(i\) 和 \(j\) 用 \(P\) 进制输出的乘积
- 最后换行进入新的循环
\(Code:\)
#include <bits/stdc++.h>
using namespace std;
int to_ten(int n, string op)
{
int j = 0;
int len = op.size();
for (int i = 0;i < len;++i)
{
if (op[i] >= '0' and op[i] <= '9')
{
j = n * j + int(op[i] - '0');
}
else
{
j = n * j + int(op[i] - 'A' + 10);
}
}
return j;
}
string to_x(int x, int op)
{
string ans = "";
while (x)
{
if (x % op < 10)
{
ans = char(x % op + '0') + ans;
}
else
{
ans = char(x % op - 10 + 'A' ) + ans;
}
x /= op;
}
return ans;
}
int main()
{
int n;
cin >> n;
for (int i = 1;i <= n - 1;++i)
{
for (int j = 1;j <= i;++j)
{
// cout << 1 << ' ';
cout << to_x(i, n) << '*' << to_x(j, n) << '=' << to_x(i * j, n) << ' ';
}
cout << "\n";
}
return 0;
}
Q5:B2141 确定进制
题目传送门 提交记录传送门
问题分析:
- 输入就不用讲了吧
for循环从 \(2\) 进制 枚举到 \(16\) 进制- 先用
check函数检查每位数字是否有大于或等于基数的情况,如果有,就直接continue - 将 \(p\) \(q\) \(r\) 转换为 \(i\) 进制上的数
- 再用 \(if\) 判断 $ p \times q$ 是否等于 \(r\)
\(AC\) \(Code:\)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll to_ten(string op, ll x)
{
ll sum = 0;
ll len = op.size();
for (ll i = 0;i < len;++i)
{
if (op[i] >= '0' and op[i] <= '9')
{
sum = sum * x + (op[i] - '0');
}
if (op[i] >= 'A' and op[i] <= 'Z')
{
sum = sum * x + (op[i] - 'A' + 10);
}
}
return sum;
}
bool check(ll a, ll jin)
{
ll maxx = 0;
while (a)
{
if (a%10 > maxx)
{
maxx = a%10;
}
a /= 10;
}
return maxx < jin;
}
signed main()
{
ll p, q, r;
cin >> p >> q >> r;
for (ll i = 2;i <= 16;++i)
{
if (check(p, i)==0 || check(q,i)==0 || check(r,i)==0 )
{
continue;
}
ll num1 = to_ten(to_string(p), i);
ll num2 = to_ten(to_string(q), i);
ll num3 = to_ten(to_string(r), i);
if (num1 * num2 == num3)
{
cout << i << "\n";
return 0;
}
}
cout << 0;
return 0;
}
总结
这一次测试情况不是很好
进制转换还不是很熟练
还是要多敲几遍代码
\(Goodbye!!!\)
本文来自博客园,作者:yucheng0630,转载请注明原文链接:https://www.cnblogs.com/yucheng0630/p/18321552

浙公网安备 33010602011771号