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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ITRF2004 {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int getA() throws NumberFormatException, IOException
{
System.out.printf("\nvaleur de A :");
int A = Integer.parseInt(in.readLine());
return A;
}
int getB() throws NumberFormatException, IOException
{
System.out.printf("\nvaleur de B :");
int B = Integer.parseInt(in.readLine());
return B;
}
int calculIteratif(int A, int B, int y) throws NumberFormatException, IOException
{
int x;
x = A;
while (y < B)
{
if ((B / y) >= 2)
{
x = x * x;
y = 2 * y;
} else
{
x = x * A;
y++;
}
System.out.printf("x= %d, y= %d\n", x, y);
}
return x;
}
int calculRecursif(int A, int B, int y) throws NumberFormatException, IOException
{
int x;
x = A;
if (y>=B) return x;
while (y < B)
{
if ((B / y) >= 2)
{
return calculRecursif (x*x, B, 2*y);
} else
{
return calculRecursif (x*A, B, y+1);
}
}
System.out.printf("\nx= %d, y= %d\n", x, y);
return x;
}
public static void main(String [] args) throws NumberFormatException, IOException
{
ITRF2004 it = new ITRF2004();
int A, B, y;
A = it.getA();
B = it.getB();
y = 1;
System.out.printf("\n\nLe résultat est %d", it.calculIteratif(A, B, y));
System.out.printf("\n\nLe résultat est %d", it.calculRecursif(A, B, y));
}
} |
Partager