题目 B1048 数字加密
-
题意
按规则按位进行计算。 -
思路
首先是从后往前处理字符串,这里奇偶位的判断会变化。其次,需要注意字符串里面存的是字符在进行数学运算的时候需要减去·‘0’
。再次,还需要注意的一点就是两个位数不一致的时候也需要按规则进行运算,不足的另一个需要补0
。这里13进制
我还是采用数组去存储。 -
Code in C++
#include <iostream>
#include <string>
char radix[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};
int main()
{
std::string A, B, ans;
std::cin >> A >> B;
int alenth = A.size();
int blenth = B.size();
int lenth = alenth > blenth ? alenth : blenth;
for (int i = 0; i < lenth; ++i) {
int tempA = i < alenth ? A[alenth - 1 - i] - '0': 0;
int tempB = i < blenth ? B[blenth - 1 - i] - '0': 0;
if (i % 2 == 0) {
ans.insert(0, 1, radix[(tempA + tempB)% 13]);
} else {
if (tempB >= tempA) {
tempB -= tempA;
} else {
tempB = tempB + 10 - tempA;
}
ans.insert(0, 1, tempB + '0');
}
}
std::cout << ans;
return 0;
}
题目 A1005
-
题意
将输入数各位相加,然后按位读出相加和。 -
思路
就是简单的将各位加起来然后再进行映射数字的读法。可能需要注意的就是数字的顺序问题,单词的拼写问题。 -
Code in C++
#include <iostream>
#include <string>
#include <map>
std::map<char, std::string> digits = {{'0', "zero"},
{'1', "one"},
{'2', "two"},
{'3', "three"},
{'4', "four"},
{'5', "five"},
{'6', "six"},
{'7', "seven"},
{'8', "eight"},
{'9', "nine"}};
int main()
{
std::string N;
std::cin >> N;
int sum = 0;
for (int i = 0; i < N.size(); ++i) {
sum += (N[i] - '0');
}
std::string ans = std::to_string(sum);
std::cout << digits[ans[0]];
for (int i = 1; i < ans.size(); ++i) {
std::cout << " " << digits[ans[i]];
}
return 0;
}
小结
map
定义初始化时里面键值对是用大括号{}
。
PS
:今天看到知乎有个leetcode提交记录的界面,羡慕。又是想弃PTA的一天。