实训week2:数制专题

这章的题真的好烦…函数全得手写

练习题目:

EOJ 1147

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stack>
using namespace std;
char str[] = {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXRZ"};
int main() {
int T;
cin >> T;
for(int step = 0;step < T;step++){
stack<char> s;
int N, R;
cin >> N >> R;
if(N < 0)
cout << '-', N = -N;
while(N)
s.push(str[N % R]), N /= R;
while(!s.empty())
cout << s.top(), s.pop();
cout << endl;
}
return 0;
}

EOJ 2966

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <stack>
#include <iomanip>
using namespace std;
void decTobin(int x){
stack<int> s;
if(!x){
cout << "0 ";
return;
}
while(x)
s.push(x % 2), x /= 2;
while(!s.empty())
cout << s.top(), s.pop();
cout << " ";
}
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
int n;
cin >> n;
cout << "case #" << step << ":\n";
decTobin(n);
cout << setiosflags(ios::uppercase) << hex << n << endl;
}
return 0;
}

EOJ 2876

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main(){
int T;
cin >> T;
while(T--){
int x, y;
cin >> x >> y;
int cnt = 0, z = x ^ y;
while(z)
cnt += z % 2, z >>= 1;
cout << cnt << endl;
}
}

EOJ 3040

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
int n, pos = 0, arr[32];
cin >> n;
while(n)
arr[pos++] = n % 2, n /= 2;
int max = 1, cnt = 0;
for(int i = 0;i < pos;i++){
int temp = 1;
for(int j = i;j < pos-1;j++)
if(arr[j] != arr[j+1]) temp++;
else break;
if(temp > max) max = temp;
}
cout << "case #" << step << ":\n";
cout << max << endl;
}
return 0;
}