| New file |
| | |
| | | public class BandMatrix { |
| | | public static void main(String[] args) { |
| | | int n = Integer.parseInt(args[0]); |
| | | int distanza = Integer.parseInt(args[1]); |
| | | |
| | | for (int i = 0; i < n; i++) { |
| | | for (int j = 0; j < n; j++) { |
| | | if (Math.abs(i - j) <= distanza) { |
| | | System.out.print("* "); |
| | | } else { |
| | | System.out.print("0 "); |
| | | } |
| | | } |
| | | System.out.println(); |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | public class CMYKtoRGB { |
| | | public static void main(String[] args) { |
| | | double c = Double.parseDouble(args[0]); |
| | | double m = Double.parseDouble(args[1]); |
| | | double y = Double.parseDouble(args[2]); |
| | | double k = Double.parseDouble(args[3]); |
| | | |
| | | double w = 1 - k; |
| | | int r = (int) Math.round(255 * w * (1 - c)); |
| | | int g = (int) Math.round(255 * w * (1 - m)); |
| | | int b = (int) Math.round(255 * w * (1 - y)); |
| | | |
| | | System.out.println("red = " + r); |
| | | System.out.println("green = " + g); |
| | | System.out.println("blue = " + b); |
| | | } |
| | | } |
| New file |
| | |
| | | public class CasualNumber { |
| | | public static void main(String[] args) { |
| | | double uno, due, tre, quattro, cinque; |
| | | double medio, massimo, minimo; |
| | | uno = Math.random(); |
| | | due = Math.random(); |
| | | tre = Math.random(); |
| | | quattro = Math.random(); |
| | | cinque = Math.random(); |
| | | medio = (uno + due + tre + quattro + cinque) / 5; |
| | | minimo = Math.min(uno, due); |
| | | minimo = Math.min(minimo, due); |
| | | minimo = Math.min(minimo, tre); |
| | | minimo = Math.min(minimo, quattro); |
| | | minimo = Math.min(minimo, cinque); |
| | | massimo = Math.max(uno, due); |
| | | massimo = Math.max(massimo, tre); |
| | | massimo = Math.max(massimo, quattro); |
| | | massimo = Math.max(massimo, cinque); |
| | | System.out.println("uno " + uno); |
| | | System.out.println("due " + due); |
| | | System.out.println("tre " + tre); |
| | | System.out.println("quattro " + quattro); |
| | | System.out.println("cinque " + cinque); |
| | | System.out.println("medio " + medio); |
| | | System.out.println("minimo " + minimo); |
| | | System.out.println("massimo " + massimo); |
| | | } |
| | | } |
| New file |
| | |
| | | public class CouponCollector { |
| | | public static void main(String[] args) { |
| | | int n = Integer.parseInt(args[0]); |
| | | boolean[] trovato = new boolean[n]; |
| | | int distinti = 0; |
| | | int tentativi = 0; |
| | | |
| | | while (distinti < n) { |
| | | int r = (int) (Math.random() * n); |
| | | tentativi++; |
| | | if (!trovato[r]) { |
| | | trovato[r] = true; |
| | | distinti++; |
| | | } |
| | | } |
| | | |
| | | System.out.println(tentativi + " tentativi"); |
| | | } |
| | | } |
| New file |
| | |
| | | public class GeneralizedHarmonic { |
| | | public static void main(String[] args) { |
| | | int n = Integer.parseInt(args[0]); |
| | | int m = Integer.parseInt(args[1]); |
| | | double sum = 0.0; |
| | | |
| | | for (int i = 1; i <= n; i++) { |
| | | sum += 1.0 / Math.pow(i, m); |
| | | } |
| | | |
| | | System.out.println(sum); |
| | | } |
| | | } |
| New file |
| | |
| | | public class GreatCircle { |
| | | public static void main(String[] args) { |
| | | double x1 = Double.parseDouble(args[0]); |
| | | double y1 = Double.parseDouble(args[1]); |
| | | double x2 = Double.parseDouble(args[2]); |
| | | double y2 = Double.parseDouble(args[3]); |
| | | |
| | | double r = 6371.0; // Earth radius |
| | | |
| | | double dx = Math.toRadians(x2 - x1) / 2.0; |
| | | double dy = Math.toRadians(y2 - y1) / 2.0; |
| | | |
| | | double under = Math.sin(dx) * Math.sin(dx) + |
| | | Math.cos(Math.toRadians(x1)) * Math.cos(Math.toRadians(x2)) |
| | | * Math.sin(dy) * Math.sin(dy); |
| | | |
| | | double distance = 2.0 * r * Math.asin(Math.sqrt(under)); |
| | | |
| | | System.out.println(distance + " kilometers"); |
| | | } |
| | | } |
| New file |
| | |
| | | public class HelloGoodbye { |
| | | public static void main(String[] args) { |
| | | System.out.println("Hello " + args[0] + " and " + args[1]); |
| | | System.out.println("Goodbye " + args[1] + " and " + args[0]); |
| | | } |
| | | } |
| New file |
| | |
| | | class HelloWorld { |
| | | public static void main(String[] args) { |
| | | System.out.println("Hello World"); |
| | | } |
| | | } |
| New file |
| | |
| | | public class PrimeSieve { |
| | | public static void main(String[] args) { |
| | | int n = Integer.parseInt(args[0]); |
| | | boolean[] primo = new boolean[n + 1]; |
| | | |
| | | // Inizializza tutti i numeri come primi |
| | | for (int i = 2; i <= n; i++) { |
| | | primo[i] = true; |
| | | } |
| | | |
| | | // Implementa il Crivello di Eratostene |
| | | for (int i = 2; i * i <= n; i++) { |
| | | if (primo[i]) { |
| | | for (int j = i * i; j <= n; j += i) { |
| | | primo[j] = false; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // conto tutti i numeri primi trovati |
| | | int count = 0; |
| | | for (int i = 2; i <= n; i++) { |
| | | if (primo[i]) { |
| | | count++; |
| | | } |
| | | } |
| | | |
| | | System.out.println(count + " numeri primi trovati fino a " + n); |
| | | } |
| | | } |
| New file |
| | |
| | | public class RandomWalker { |
| | | public static void main(String[] args) { |
| | | int x = 0; |
| | | int y = 0; |
| | | int passi = 0; |
| | | int limite = Integer.parseInt(args[0]); |
| | | |
| | | System.out.println("(" + x + "," + y + ")"); |
| | | while (Math.abs(x) + Math.abs(y) < limite) { |
| | | double r = Math.random(); |
| | | if (r < 0.25) { |
| | | x++; |
| | | } else if (r < 0.5) { |
| | | x--; |
| | | } else if (r < 0.75) { |
| | | y++; |
| | | } else { |
| | | y--; |
| | | } |
| | | passi++; |
| | | System.out.println("(" + x + "," + y + ")"); |
| | | } |
| | | System.out.println("steps = " + passi); |
| | | } |
| | | } |
| New file |
| | |
| | | public class RandomWalkers { |
| | | public static void main(String[] args) { |
| | | |
| | | int limite = Integer.parseInt(args[0]); |
| | | int cicli = Integer.parseInt(args[1]); |
| | | int totalePassi = 0; |
| | | for (int i = 0; i < cicli; i++) { |
| | | |
| | | int x = 0; |
| | | int y = 0; |
| | | int passi = 0; |
| | | |
| | | while (Math.abs(x) + Math.abs(y) < limite) { |
| | | double r = Math.random(); |
| | | if (r < 0.25) { |
| | | x++; |
| | | } else if (r < 0.5) { |
| | | x--; |
| | | } else if (r < 0.75) { |
| | | y++; |
| | | } else { |
| | | y--; |
| | | } |
| | | passi++; |
| | | } |
| | | |
| | | totalePassi += passi; |
| | | } |
| | | |
| | | System.out.println("average number of steps = " + ((double) totalePassi / cicli)); |
| | | } |
| | | |
| | | } |
| New file |
| | |
| | | public class RightTriangle { |
| | | public static void main(String[] args) { |
| | | int a = Integer.parseInt(args[0]); |
| | | int b = Integer.parseInt(args[1]); |
| | | int c = Integer.parseInt(args[2]); |
| | | int a1, a2, last, first, middle; |
| | | |
| | | a1 = Math.min(a, b); |
| | | a2 = Math.max(a, b); |
| | | last = Math.max(a2, c); |
| | | a2 = Math.min(a2, c); |
| | | first = Math.min(a1, a2); |
| | | middle = Math.max(a1, a2); |
| | | |
| | | boolean r1 = first > 0; |
| | | boolean r2 = middle > 0; |
| | | boolean r3 = last > 0; |
| | | |
| | | boolean result = (first * first + middle * middle) == (last * last); |
| | | System.out.println(r1 && r2 && r3 && result); |
| | | } |
| | | } |
| New file |
| | |
| | | public class Sample { |
| | | |
| | | /* |
| | | * Genera un array di interi di dimensione 'stack', |
| | | * poi lo riempie con 'stack' numeri casuali |
| | | * stampa i primi 'campione' numeri dell'array |
| | | */ |
| | | public static void main(String[] args) { |
| | | int campione = Integer.parseInt(args[0]); |
| | | int stack = Integer.parseInt(args[1]); |
| | | |
| | | int[] matrix = new int[stack]; |
| | | |
| | | for (int i = 0; i < stack; i++) { |
| | | matrix[i] = i; |
| | | } |
| | | |
| | | //mescola l'array |
| | | for (int i = 0; i < stack; i++) { |
| | | int dove = (int) (Math.random() * stack); |
| | | int temp = matrix[i]; |
| | | matrix[i] = matrix[dove]; |
| | | matrix[dove] = temp; |
| | | } |
| | | |
| | | for (int i = 0; i < campione; i++) { |
| | | System.out.println(matrix[i]); |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | public class SortTreeInteger { |
| | | public static void main(String[] args) { |
| | | int a = Integer.parseInt(args[0]); |
| | | int b = Integer.parseInt(args[1]); |
| | | int c = Integer.parseInt(args[2]); |
| | | int a1, a2, last, first, middle; |
| | | |
| | | a1 = Math.min(a,b); |
| | | a2 = Math.max(a,b); |
| | | last = Math.max(a2,c); |
| | | a2 = Math.min(a2,c); |
| | | first = Math.min(a1,a2); |
| | | middle = Math.max(a1,a2); |
| | | |
| | | System.out.println("i tre numeri ordinati: " + first + " " + middle + " " + last); |
| | | } |
| | | } |