7.24 知识点+错题总结
1.知识点
进制转换
进制转换,从字面意思就知道是一个进制的数字转到另一个进制,一个位上的数字一旦到了x(进制),那就进位。
常见的进制有:2、8、10、16……
c++版本
常见版本:
1.C++98:最老的,devc默认版本(4.9.8)
2.C++11:新语法:to_string(int),auto……很多新功能都是这个版本引入的
3.C++14:最常用的版本,洛谷和比赛默认的都是这个版本
……
2.模板
B3620 x 进制转 10 进制
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
const int N1 = 1e3 + 2;
typedef long long ll;
typedef unsigned long long ull;
//基础代码
int x;
string a;
//以上的变量因为函数中也需要使用,所以设置全局
int f() {
int sum = 0;
for (int i = 0; i < a.length(); i++)
{
if (a[i] >= '0' && a[i] <= '9')//如果是数字
{
sum = sum * x + (a[i] - '0');//-‘0’把字符变成数字
}
else//如果是字母
{
sum = sum * x + (a[i] - 'A' + 10);//+10是因为A代表的是10
}
}
return sum;//返回值
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);//关闭同步流
cin >> x >> a;//输入
cout << f() << '\n';//输出
return 0;
}
B3619 10 进制转 x 进制
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
const int N1 = 1e3 + 2;
typedef long long ll;
typedef unsigned long long ull;
//基础代码
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);//关闭同步流
int n, x, sum;
string a;
//由于程序只在主函数内运行,所以可以在main函数内打
cin >> n >> x;//输入
while (n) {//直到n没有了,就代表转完了,可以输出了
if (n % x == 0){//如果数字为零
a = '0' + a;//将字符加到字符串中
}
else if (n % x >= 10)//如果需要使用字母
{
a = char((n % x) - 10 + 'A') + a;//-10是因为A是十,要减去才能相加
}
else if (n % x < 10)//如果只需要存数字
{
a = char((n % x) + '0') + a;//将数字字符加到字符串中
}
n /= x;//准备下一轮循环
}
cout << a << '\n';//输出
return 0;
}
3.考试
考试情况
A | B | C | D | E | F(附加题) |
---|---|---|---|---|---|
100 | 80 | 100 | 100 | 70 | 0 |
考题
附加题(F).P1017 [NOIP2000 提高组] 进制转换
考点/易错点
A.B3620 x 进制转 10 进制:考模板掌握度
C.P2084 进制转换:前一天的作业题,不需要求,只需要写算式
D.P8723 [蓝桥杯 2020 省 AB3] 乘法表:按照十进制的数字求出数字,后面转进制(易错:乘的数字i,j也要转)
E.B2141 确定进制:用for循环一个一个枚举进制,那个对了选哪个
附加题(F).P1017 [NOIP2000 提高组] 进制转换:对数字不断除以负数进制,将余数存储,最后倒序输出。
错误题目
B.B3619 10 进制转 x 进制
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
const int N1 = 1e3 + 2;
typedef long long ll;
typedef unsigned long long ull;
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, x, sum;
string a;
cin >> n >> x;
while (n) {
if (n % x == 0)
a = '0' + a;
else if (n % x >= 10)
a = char((n % x) - 11 + 'A') + a; //there
else if (n % x < 10)
a = char((n % x) + '0') + a;
n /= x;
}
cout << a << '\n';
return 0;
}
错误分析:
在转大写的执行时,并不是-11,而是-10
a = char((n % x) - 11 + 'A') + a; char((n % x) - 10 + 'A') + a;
B2141 确定进制
错误代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll shi(ll x, ll n) {
string a;
while (n) {//there
a = char((n % x) + '0') + a;
n /= x;
}
ll cnt = 0;//there
for (ll i = 0; i < a.length(); i++) {//there
cnt = cnt * 10 + (a[i] - '0');
}
return cnt;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll p, q, r;
cin >> p >> q >> r;
for (ll i = 2; i <= 16; i++) {
if (shi(i, p * q) == r) {//there
cout << i << '\n';
return 0;
}
}
cout << "0\n";
return 0;
}
错误分析
在最开始就已经把思路搞错了,应该把数字变成字符串来进行操作,而且要检查数字大不大于进制。
while (n) {//there
a = char((n % x) + '0') + a;
n /= x;
}
ll cnt = 0;//there
for (ll i = 0; i < a.length(); i++) {//there
cnt = cnt * 10 + (a[i] - '0');
}//第一个错误点:字符串操作
->long long a = shi(to_string(p), i);
long long b = shi(to_string(q), i);
long long c = shi(to_string(r), i);
//第二个错误点:检查函数
->bool check(ll x, ll i) {
while (x) {
if (x % 10 >= i)
return 0;
x /= 10;
}
return 1;
}
//第三个错误点:字符串转变
while (n) {//there for (ll i = 0; i < a.length(); i++) {
a = char((n % x) + '0') + a; if (a[i] >= 'A' && a[i] <= 'Z') {
n /= x; cnt = cnt * n + (a[i] - 'A' + 10);
} -> } else {
ll cnt = 0;//there cnt = cnt * n + (a[i] - '0');
for (ll i = 0; i < a.length(); i++) {//there }
cnt = cnt * 10 + (a[i] - '0'); }
}
4.总结
知识点和模板还是记得不熟,还要再记模板。