目录
1. 字符串排序
2. Excel表列名称
3. 颠倒二进制位
附录:
位移运算符
左移运算符<<
1.无符号
2.有符号
右移运算符>>
1.无符号
2.有符号
程序测试
编写程序,输入若干个字符串。
要求:
(1)按字符串长度的大小升序输出各个字符串。
(2)按字符串中字符的ASCII码值大小升序输出各个字符串。
代码:
#include
#include
#include
#include
using namespace std;bool compare(string a,string b) {if (a.length() != b.length()) {return a.length() < b.length();}return a < b;
}int main()
{vectorlist;string inputString;while (cin>>inputString) {if (inputString == "0") {break;}list.push_back(inputString);}sort(list.begin(),list.end(),compare);for (int i=0; i
输入输出: (略)
给你一个整数 columnNumber
,返回它在 Excel 表中相对应的列名称。
例如:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
示例 1:
输入:columnNumber = 1 输出:"A"
示例 2:
输入:columnNumber = 28 输出:"AB"
示例 3:
输入:columnNumber = 701 输出:"ZY"
示例 4:
输入:columnNumber = 2147483647 输出:"FXSHRXW"
提示:
1 <= columnNumber <= 231 - 1
代码:
#include
using namespace std;class Solution
{
public:string convertToTitle(int n){string res;while (n){int temp = n % 26;n /= 26;if (temp)res.push_back('A' + temp - 1);else{res.push_back('Z');n--;}}reverse(res.begin(), res.end());return res;}
};int main()
{Solution s;cout << s.convertToTitle(1) << endl;cout << s.convertToTitle(28) << endl;cout << s.convertToTitle(701) << endl;cout << s.convertToTitle(2147483647) << endl;return 0;
}
输出:
A
AB
ZY
FXSHRXW
颠倒给定的 32 位无符号整数的二进制位。
提示:(仅对Java而言,C++用不到批提示)
-3
,输出表示有符号整数 -1073741825
。示例 1:
输入:n = 00000010100101000001111010011100 输出:964176192 (00111001011110000010100101000000) 解释:输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, 因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。
示例 2:
输入:n = 11111111111111111111111111111101 输出:3221225471 (10111111111111111111111111111111) 解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,因此返回 3221225471 其二进制表示形式为 10111111111111111111111111111111 。
提示:
32
的二进制字符串进阶: 如果多次调用这个函数,你将如何优化你的算法?
代码:
#include
using namespace std;
class Solution
{
public:uint32_t reverseBits(uint32_t n){uint32_t res = 0;for (int i = 0; i < 32; i++){res <<= 1;res |= n & 1;n >>= 1;}return res;}
};int main()
{Solution s;cout << s.reverseBits(43261596) << endl;cout << s.reverseBits(4294967293) << endl;return 0;
}
输出:
964176192
3221225471
是位操作运算符的一种。移位运算符可以在二进制的基础上对数字进行平移。按照平移的方向和填充数字的规则分为三种:<<(左移)、>>(带符号右移)和>>>(无符号右移)。
语法格式:需要移位的数字<<移位的次数n
运算规则:按二进制形式把所有数字向左移动相应的位数,高位移出(舍弃),低位的空位补0。相当于乘以2的n次方
例如:4<<2 ,就是将数字4左移2位
过程:4的二进制形式:00000000 00000000 00000000 00000100;然后把高位2个0移出,其余所有位向左移动2位,低位补0,得到:00000000 00000000 00000000 00010000;十进制数为16,16=4*22。
如果你左移有符号的数字,以至于符号位受影响,则结果是不确定的。
语法格式:需要移位的数字>>移位的次数n
运算规则:按二进制形式把所有数字向右移动相应的位数,低位移出(舍弃),高位的空位补0。相当于除以2的n次方
例如:4>>2 ,就是将数字4左移2位
过程:4的二进制形式:00000000 00000000 00000000 00000100;然后把低位2个0移出,其余所有位向右移动2位,高位补0,得到:00000000 00000000 00000000 00000001;十进制数为1,1=4÷22。
语法格式:需要移位的数字>>移位的次数n
运算规则:按二进制形式把所有数字向右移动相应的位数,低位移出(舍弃),正数,高位的空位补0。负数,高位的空位补1.
#include
#include
using namespace std;int main() {unsigned short short1 = 4; bitset<16> bitset1{short1}; // the bitset representation of 4cout << bitset1 << endl; // 0000000000000100unsigned short short2 = short1 << 1; // 4 left-shifted by 1 = 8bitset<16> bitset2{short2};cout << bitset2 << endl; // 0000000000001000unsigned short short3 = short1 << 2; // 4 left-shifted by 2 = 16bitset<16> bitset3{short3};cout << bitset3 << endl; // 0000000000010000return 0;
}
#include
#include
using namespace std;int main() {short short1 = 16384; bitset<16> bitset1{short1};cout << bitset1 << endl; // 0100000000000000 short short2 = short1 << 1;bitset<16> bitset2{short2}; // 16384 left-shifted by 1 = -32768cout << bitset2 << endl; // 100000000000000short short3 = short1 << 14;bitset<16> bitset3{short3}; // 4 left-shifted by 14 = 0cout << bitset3 << endl; // 000000000000000
}
#include
#include
using namespace std;int main() {unsigned short short11 = 1024;bitset<16> bitset11{short11};cout << bitset11 << endl; // 0000010000000000unsigned short short12 = short11 >> 1; // 512bitset<16> bitset12{short12};cout << bitset12 << endl; // 0000001000000000unsigned short short13 = short11 >> 10; // 1bitset<16> bitset13{short13};cout << bitset13 << endl; // 0000000000000001unsigned short short14 = short11 >> 11; // 0bitset<16> bitset14{short14};cout << bitset14 << endl; // 0000000000000000}
}
#include
#include
using namespace std;int main() {short short1 = 1024;bitset<16> bitset1{short1};cout << bitset1 << endl; // 0000010000000000short short2 = short1 >> 1; // 512bitset<16> bitset2{short2};cout << bitset2 << endl; // 0000001000000000short short3 = short1 >> 10; // 1bitset<16> bitset3{short3}; cout << bitset3 << endl; // 0000000000000001short neg1 = -16;bitset<16> bn1{neg1};cout << bn1 << endl; // 1111111111110000short neg2 = neg1 >> 1; // -8bitset<16> bn2{neg2};cout << bn2 << endl; // 1111111111111000short neg3 = neg1 >> 2; // -4bitset<16> bn3{neg3};cout << bn3 << endl; // 1111111111111100short neg4 = neg1 >> 4; // -1bitset<16> bn4{neg4}; cout << bn4 << endl; // 1111111111111111short neg5 = neg1 >> 8; // -1???bitset<16> bn5{neg5}; cout << bn5 << endl; // 1111111111111111return 0;}