一、bitset
T0517 整数原码
有一个short int 的整数,编写一个signMagnitude函数输出它的16位二进制原码。
输入格式
一个整型整数。
输出格式
输出一行,即该整数的原码表示。
样例输入
-10
样例输出
1000000000001010B
#include<iostream>
#include<bitset>
using namespace std;
string signMagnitude(short int num)
{
bitset<16>binary;
if(num < 0)
{
binary.set(15);
num = -num;
}
for(int i = 0;i < 15;i++)
{
if(num & (1 << i))
{
binary.set(i);
}
}
return binary.to_string();
}
int main()
{
short int num;
cin >> num;
string magnitude = signMagnitude(num);
cout << magnitude << "B" << endl;
return 0;
}
T0518 整数反码
有一个short int 的整数,编写一个_1sComplement函数输出他的16位二进制反码。
输入格式
一个整型整数。
输出格式
输出一行,即该整数的反码表示。
样例输入
-1
样例输出
1111111111111110B
#include <iostream>
#include <bitset>
using namespace std;
// 函数:计算并返回 short int 的 16 位反码
string _1sComplement(short int num)
{
// 使用 bitset<16> 来表示 16 位二进制数
bitset<16> binary(num);
// 计算反码
if (num < 0)
{
binary.set(15);
num = -num;
for (int i = 0; i < 15; i++)
{
if (num & (1 << i))
{
binary.reset(i);
}
else
{
binary.set(i);
}
}
}
if (num > 0)
{
bitset<16>binary = ~binary;
}
// 返回反码的字符串表示
return binary.to_string();
}
int main() {
short int num;
cin >> num;
string complement = _1sComplement(num);
cout << complement << "B" << endl;
return 0;
}
T0516 整数补码
有一个int 的整数,编写一个_2sComplement函数输出它的32位二进制补码。
输入格式
一个整型整数。
输出格式
输出一行,即该整数的补码表示。
样例输入
9
样例输出
00000000000000000000000000001001B
<正数的补码就是原码>
#include<iostream>
#include<bitset>
using namespace std;
string _2sComplement(int num)
{
bitset<32>binary(num);
/*if(num > 0)
{
bitset<32>binary = ~binary;
}
if(num < 0)
{
binary.set(31);
num = -num;
for(int i = 0;i < 31 ; i++)
{
if(num & (1 << i))
{
binary.reset(i);
}
else
{
binary.set(i);
}
}
}*/
return binary.to_string();
}
int main()
{
int num;
cin >> num;
string complement = _2sComplement(num);
cout << complement << "B" << endl;
return 0;
}
二、ISBN-13校验码验证
如下图: 978-7 表示中国大陆-汉语 ; 出版社代码为302;书序码为38141;校验码为9。
校验码的计算方法如下:
第1位数字乘以1加上第2位数字乘以3,再第3位数字乘以1,再加上第4位数字乘以3……以此类推,用所得的结果mod 10,再用10减去所得的余数即为校验码,如果校验码为10,则校验码为0。
例如ISBN号码978-7-302-38141-9,它的校验码的计算方法是
(1) 9*1+7*3+8*1+7*3+3*1+0*3+2*1+3*3+8*1+1*3+4*1+1*3= 91
(2) 91%10 =1
(3) 10-1=9
所以校验码为: 9。
编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出是正确的ISBN号码。
输入格式
输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN号码的格式要求)。
输出格式
输出一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。
样例1输入
978-7-302-38141-9
样例1输出
Right
样例2输入
978-7-04-054518-3
样例2输出
978-7-04-054518-0
#include <iostream>
#include <string>
// 计算ISBN-13的校验码
char calculateCheckDigit(const std::string& isbn)
{
int total = 0;
for (size_t i = 0; i < isbn.length() - 1; ++i)
{
//将字符串转化为整数
/*isbn[i] 是ISBN字符串中的第i个字符。
'0' 是字符'0'的ASCII值。
isbn[i] - '0' 将字符转换为其对应的整数值。例如,如果isbn[i]是字符'5',那么'5' - '0'的结果是整数5。*/
if (i % 2 == 0)
{
total += (isbn[i] - '0') * 1;
}
else
{
total += (isbn[i] - '0') * 3;
}
}
int checkDigit = (10 - (total % 10)) % 10;
return '0' + checkDigit;
// 将整数校验码转换为字符
/*checkDigit: 这是一个整数值,范围从 0 到 9。
'0': 在 C++ 中,字符 '0' 的 ASCII 值是 48。
'0' + checkDigit: 将整数 checkDigit 加到字符 '0' 的 ASCII 值上,从而得到相应的字符。*/
}
// 验证ISBN-13码是否正确,并在必要时生成正确的ISBN-13码
std::string validateISBN(const std::string& isbn)
{
// 去掉连字符
std::string isbnCleaned;
for (char ch : isbn)
{
if (ch != '-')
{
isbnCleaned += ch;
}
}
// 计算预期的校验码
char calculatedCheckDigit = calculateCheckDigit(isbnCleaned);
// 比较计算出的校验码与提供的校验码
if (calculatedCheckDigit == isbnCleaned.back())
{
return "Right";
}
else
{
// 替换错误的校验码为正确的校验码
std::string correctISBN = isbnCleaned.substr(0, 12) + calculatedCheckDigit;
// 重新插入连字符以匹配输入格式
std::string correctISBNFormatted = correctISBN.substr(0, 3) + "-" +
correctISBN.substr(3, 1) + "-" +
correctISBN.substr(4, 3) + "-" +
correctISBN.substr(7, 5) + "-" +
correctISBN.substr(12, 1);
return correctISBNFormatted;
}
}
int main()
{
std::string isbnInput;
std::cout << "请输入ISBN-13码: ";
std::cin >> isbnInput;
std::string result = validateISBN(isbnInput);
std::cout << result << std::endl;
return 0;
}
substr
是 std::string
类的一个成员函数,用于提取子字符串。其语法如下:
std::string substr(size_t pos, size_t len) const;
pos
: 子字符串的起始位置(从0开始)。len
: 子字符串的长度。如果省略,则提取从pos
开始到字符串末尾的所有字符。
分析每一部分
-
第一部分:
correctISBN.substr(0, 3)
- 起始位置: 0
- 长度: 3
- 提取前3个字符。例如,对于
9787302381419
,这部分是978
。
-
第二部分
correctISBN.substr(3, 1)
- 起始位置: 3
- 长度: 1
- 提取第4个字符。例如,对于
9787302381419
,这部分是7
。
-
第三部分:
correctISBN.substr(4, 3)
- 起始位置: 4
- 长度: 3
- 提取第5到第7个字符。例如,对于
9787302381419
,这部分是302
。
-
第四部分:
correctISBN.substr(7, 5)
- 起始位置: 7
- 长度: 5
- 提取第8到第12个字符。例如,对于
9787302381419
,这部分是38141
。
-
第五部分:
correctISBN.substr(12, 1)
- 起始位置: 12
- 长度: 1
- 提取第13个字符。例如,对于
9787302381419
,这部分是9
。