题目列表:
2018年:明码
2019年:年号字串
1.明码
答案: 387420489
分析:
1.32个字节表示一个字,如何表示这个字?一行放两个字节共16位,一个字用16行表示
2.计算机中存储的就是二进制,所以不用特意去求;正数在计算机中存储的形式就是原码,负数在计算机中存储的形式就是补码,所以你不需要求原码,再每位取反,末位加1,只需要输出就好了
代码:
#include<iostream>
using namespace std;
int main(){
int m,n;
int w[16];
while( cin>>m>>n ){
for(int i = 7;i >= 0;i--){
w[i]=m&1;
m>>=1;
}
for(int i = 15;i >= 8;i--){
w[i]=n&1;
n>>=1;
}
for(int i = 0;i <= 15;i++){
if(w[i] == 1 ){
cout << w[i];
}else{
cout << " ";
}
}
cout << endl;
}
}
10个汉字:九的九次方等于多少?
2.年号字串
答案:BYQ
分析:
26,52,……等26的倍数无法完成进制转换,2019确定不是26的倍数
代码:
#include<iostream>
using namespace std;
char a[] = {'.','A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int main(){
int x = 329;
string s;
while(x){
s+=a[x%26];
x/=26;
}
cout << s;
return 0;
}
3.十进制转二进制
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
string s;
while(n){
s += n%2 + '0';
n/=2;
}
for(int i = s.length()-1;i >= 0;i--){
cout << s[i];
}
return 0;
}
二进制转十进制
#include<iostream>
using namespace std;
int main(){
string s;
cin >> s;
int ans = 0;
for(int i = 0;i < s.size();i++){
ans = ans*2+s[i] -'0';
}
cout << ans;
return 0;
}
4.十进制转八进制
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
string s;
while(n){
s += n%8 + '0';
n/=8;
}
for(int i = s.length()-1;i >= 0;i--){
cout << s[i];
}
return 0;
}
八进制转十进制
#include<iostream>
using namespace std;
int main(){
string s;
cin >> s;
int ans = 0;
for(int i = 0;i < s.size();i++){
ans = ans*8+s[i] -'0';
}
cout << ans;
return 0;
}
5.十进制转十六进制
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
string s;
while(n){
int t = n % 16;
if(t >= 0 && t <= 9){
s += t + '0';
}else{
s += t-10 + 'A';
}
n /= 16;
}
for(int i = s.length()-1;i >= 0;i--){
cout << s[i];
}
return 0;
}
十六进制转十进制
#include<iostream>
using namespace std;
int main(){
string s;
cin >> s;
int ans = 0;
for(int i = 0;i < s.size();i++){
char c = s[i];
if(c >= '0' && c <= '9'){
ans = ans*16+c-'0';
}else{
ans = ans*16+c-'A'+10;
}
}
cout << ans;
return 0;
}