+ 2
#include <iostream> #include <bitset> int main() { int myDecimalNumber; std::cin >> myDecimalNumber; std::string binary = std::bitset<8>(myDecimalNumber).to_string(); //to binary std::cout<<binary<<"\n"; unsigned long decimal = std::bitset<8>(binary).to_long(); std::cout<<decimal<<"\n"; return 0; }
What is the time complexity of this programme?
5 Réponses
+ 2
Excellent! :-) The runtime complexity of your C++ program is O(1) because for a fixed size of int and a fixed size of the binary string (only 8 digits as you only request the lower 8 bits of an int), the runtime is constant.
Constant in this case means it does not depend on the actual value stored in the int. It would depend on the memory size of an int (if it was flexible) and on the number of bits you want to extract / input. But the memory size of int is fixed on all practical machines I know and the number of bits to extract / input is also fixed in your algorithm.
This is a very informal discussion that lacks details that can oftentimes be crucial for understanding a runtime analysis but a proper discussion of runtime complexity requires a bit theoretical computer science for which Learn C++ Q&A is not a suitable format (a mathematical proof for the runtime is just too long and ugly in Learn C++ Q&A :-) ). Nevertheless I hope this gives you some hints of how to think about an algorithm in order to find its runtime complexity.
If you have more questions, just ask.
+ 1
Do you need the big O-Notation? Or the polynomial?Does it have to be the particular implementation in the standard library (which version)? What level of detail? And also pls why... because it looks a bit like a homework exercise...
+ 1
What about big O notation?
0
Ok, you do not seem to be sure what you want so I assume you are not too familiar with the basics for determining the runtime complexity. So do you know what big O-Notation does?
0
Yeah I know about it!