Arkusz
Rozwiązanie
Zasady oceniania i sprawozdanie
Zapis w C++:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Wpisz liczbe n ";
cin >> n;
int p = 1;
int q = n;
while (p < q) {
int s = (p + q) / 2;
if (s * s * s < n) {
p = s + 1;
} else {
q = s;
}
}
cout << "Wynik dla n = " << n << " to "<< p;
return 0;
}
using namespace std;
int main() {
int n;
cout << "Wpisz liczbe n ";
cin >> n;
int p = 1;
int q = n;
while (p < q) {
int s = (p + q) / 2;
if (s * s * s < n) {
p = s + 1;
} else {
q = s;
}
}
cout << "Wynik dla n = " << n << " to "<< p;
return 0;
}
Zapis w python:
n = int(input("Wpisz liczbe n "))
p = 1
q = n
while p < q:
s = (p + q) // 2
if s**3 < n:
p = s + 1
else:
q = s
print(f"Wynik dla n = {n} to {p}")
Zapis w java:
import java.util.Scanner;
public class CubeRoot {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Wpisz liczbe n: ");
int n = scanner.nextInt();
int p = 1;
int q = n;
while (p < q) {
int s = (p + q) / 2;
if (Math.pow(s, 3) < n) {
p = s + 1;
} else {
q = s;
}
}
System.out.println("Wynik dla n = " + n + " to " + p);
}
}