HJ15 求int型正整数在内存中存储时1的个数
题目来源
题目描述
题目解析
#include <iostream>
int main(int argc,char **argv){
int n = 0, cnt = 0;
std::cin >> n;
while (n != 0){
if(n % 2 == 1){
cnt++;
}
n = n / 2;
}
std::cout << cnt;
return 0;
}
HJ17 坐标移动
题目来源
题目描述
题目解析
#include <iostream>
#include <unordered_set>
#include <vector>
int main(int argc,char **argv){
std::string str, tmp;
getline(std::cin, str);
std::vector<std::string> vec;
for (int i = 0; i < str.size(); ++i) {
if(str[i] == ' '){
continue;
}
if(str[i] == ';'){
if(tmp.size() == 2 || tmp.size() == 3){
vec.emplace_back(tmp);
}
tmp.clear();
}else{
tmp += str[i];
}
}
vec.emplace_back(tmp);
int x = 0, y = 0, m = 0, t1 = 0, t2 = 0;
char w;
for (int i = 0; i < vec.size(); ++i) {
tmp = vec[i];
if(tmp.size() == 2){
w = tmp[0];
t1 = tmp[1] - '0';
if(t1 < 0 || t1 > 9){
continue;
}
m = t1;
}else{
w = tmp[0];
t1 = tmp[1] - '0';
if(t1 < 0 || t1 > 9){
continue;
}
t2 = tmp[2] - '0';
if(t2 < 0 || t2 > 9){
continue;
}
m = t1 * 10 + t2;
}
if(m == 0){
continue;
}
if(w == 'A'){
x -= m;
}else if(w == 'D'){
x += m;
}else if(w == 'S'){
y -= m;
}else if(w == 'W'){
y += m;
}
}
std::cout << x << "," << y;
return 0;
}
HJ21 简单密码
题目来源
题目描述
题目解析
#include <iostream>
#include <unordered_map>
int main(int argc,char **argv){
std::unordered_map<char, char> map;
map['a'] = '2'; map['b'] = '2'; map['c'] = '2';
map['d'] = '3'; map['e'] = '3'; map['f'] = '3';
map['g'] = '4'; map['h'] = '4'; map['i'] = '4';
map['j'] = '5'; map['k'] = '5'; map['l'] = '5';
map['m'] = '6'; map['n'] = '6'; map['o'] = '6';
map['p'] = '7'; map['q'] = '7'; map['r'] = '7'; map['s'] = '7';
map['t'] = '8'; map['u'] = '8'; map['v'] = '8';
map['w'] = '9'; map['x'] = '9'; map['y'] = '9'; map['z'] = '9';
std::string str, ans;
getline(std::cin, str);
for (int i = 0; i < str.size(); ++i) {
if(str[i] >= 'A' && str[i] <= 'Z'){
if(str[i] == 'Z'){
ans.push_back('a');
}else{
ans.push_back(str[i] + 33);
}
}else if(str[i] >= 'a' && str[i] <= 'z'){
ans.push_back(map[str[i]]);
}else{
ans.push_back(str[i]);
}
}
std::cout << ans << "\n";
return 0;
}