From b2a0c53037a1af6ec074a7370635573b70060e1e Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 18 Aug 2020 15:01:28 +0200 Subject: Create cbrt.cc --- I_anno/Programmazione_2/algorithms/cbrt.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 I_anno/Programmazione_2/algorithms/cbrt.cc (limited to 'I_anno/Programmazione_2/algorithms') diff --git a/I_anno/Programmazione_2/algorithms/cbrt.cc b/I_anno/Programmazione_2/algorithms/cbrt.cc new file mode 100644 index 0000000..bbd1ee1 --- /dev/null +++ b/I_anno/Programmazione_2/algorithms/cbrt.cc @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int cbrt(int n) { + int i = 0; + int j = n; + int q = (i+j)/2; + while(q*q*q != n) { + q = (i+j)/2; + if(q*q*q < n) + i=q; + else + j=q; + } + return q; +} + +int main() { + int i; + cin >> i; + cout << i << ' ' << cbrt(i) << endl; + return 0; +} -- cgit v1.2.3-18-g5258