3 תשובות
איך אתם לןמדים את זה בכיתה י? אני בכיתה יא ורק התחלנו ללמוד את זה
שואל השאלה:
לא יודעת אבל את יודעת את הפתרון??? דחוף
לא יודעת אבל את יודעת את הפתרון??? דחוף
import java.util.arrays;
import java.util.random;
public class randomarray {
public static void main(string[] args) {
int[] arr = new int[50];
random random = new random();
// fill array with random numbers divisible by 10
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextint(10) * 10 + 10;
}
// count the most frequently generated number
int[] counts = new int[10];
for (int i = 0; i < arr.length; i++) {
int index = arr[i] / 10 - 1;
counts[index]++;
}
int maxcount = 0;
int maxindex = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] > maxcount) {
maxcount = counts[i];
maxindex = i;
}
}
int mostfrequentnumber = (maxindex + 1) * 10;
system.out.println("the most frequently generated number is " + mostfrequentnumber);
// print any missing numbers with a note
boolean missingnumbers = false;
for (int i = 10; i <= 90; i += 10) {
boolean found = false;
for (int j = 0; j < arr.length; j++) {
if (arr[j] == i) {
found = true;
break;
}
}
if (!found) {
system.out.println(i + " was not generated.");
missingnumbers = true;
}
}
if (!missingnumbers) {
system.out.println("all numbers from 10 to 90 were generated.");
}
}
}
import java.util.random;
public class randomarray {
public static void main(string[] args) {
int[] arr = new int[50];
random random = new random();
// fill array with random numbers divisible by 10
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextint(10) * 10 + 10;
}
// count the most frequently generated number
int[] counts = new int[10];
for (int i = 0; i < arr.length; i++) {
int index = arr[i] / 10 - 1;
counts[index]++;
}
int maxcount = 0;
int maxindex = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] > maxcount) {
maxcount = counts[i];
maxindex = i;
}
}
int mostfrequentnumber = (maxindex + 1) * 10;
system.out.println("the most frequently generated number is " + mostfrequentnumber);
// print any missing numbers with a note
boolean missingnumbers = false;
for (int i = 10; i <= 90; i += 10) {
boolean found = false;
for (int j = 0; j < arr.length; j++) {
if (arr[j] == i) {
found = true;
break;
}
}
if (!found) {
system.out.println(i + " was not generated.");
missingnumbers = true;
}
}
if (!missingnumbers) {
system.out.println("all numbers from 10 to 90 were generated.");
}
}
}
באותו הנושא: