alphacademy-cpp/hf2/hf6/1.cpp

63 lines
1.1 KiB
C++
Raw Normal View History

2022-11-22 17:59:11 +00:00
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int convert(string a) {
int out = 0;
int counter = 1;
//cout<<a<<endl;
2022-11-24 19:42:58 +00:00
for (int i = a.size() - 1; i >= 0; i--) {
if (a[i] == '1') {
2022-11-22 17:59:11 +00:00
out += counter;
2022-11-24 19:42:58 +00:00
//cout<< "found one ";
}
counter = counter *2;
//cout<< "out: "<< out<< " counter: "<< counter<< " index: "<<i <<endl;
2022-11-22 17:59:11 +00:00
}
return out;
}
string convertToBase(int n, int base) {
string out;
2022-11-24 19:42:58 +00:00
char a = '0';
while (n > 0) {
a += n%base;
out = a + out;
n = n / base;
a = '0';
}
2022-11-22 17:59:11 +00:00
return out;
}
int returnVecMax(vector<int> a) {
int out = a[0];
for (int i =1; i < a.size(); i++) {
if (a[i] > out) {
out = a[i];
}
}
return out;
}
int main() {
vector<int> test;
test.push_back(2);
test.push_back(6);
test.push_back(-7);
test.push_back(1);
test.push_back(0);
test.push_back(1);
test.push_back(4);
2022-11-24 19:42:58 +00:00
cout<<returnVecMax(test)<<endl<<convert("101101101")<<endl;
cout<<convertToBase(73,5)<<endl;
2022-11-22 17:59:11 +00:00
}