Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

//Exercise 23

public static boolean BalancedArray(int[] arr) {


int sum=0, count=0;
for (int i:arr) {
sum+=i;}

double avg = (double)sum/10;


for (int i:arr) {
count = (i>avg) ? count+1 : (i<avg) ? count-1 : count;}

if (count==0) return true;


else return false;
}

//Exercise 24
public static void Print(int[] ar){
for (int i=1; i<ar.length; i++){
if (ar[i-1]<ar[i]) System.out.printf("%d ", ar[i-1]);}
}

public static void main(String[] args) {


int[] ar = new int[20];
for (int i=0; i<ar.length; i++){
ar[i] = (int)(Math.random()*10);}
Print(ar);
}
/* Part C: The maximum amount is 18.
(0,1,2,3,4,5,6,7,8,9. 0,1,2,3,4,5,6,7,8,9.) */

//We did 26 in class...

//Exercise 28
public static int Print(int[] arr){
int count=0, best=0;
for (int i=1; i<arr.length; i++) {
count = (arr[i]==arr[i-1]) ? count+1 : 0;
//if it's a sequence it adds 1 to "count", else it resets.
best = (count>best) ? count : best;
}
return best;
}
//Exercise 31
public static int Seating(int[] arr, int seat){
int i=0;
while (true) {
if (i<=seat) {
if (arr[seat-i]==0)
return seat-i; }
if (i<200-seat) {
if (arr[seat+i]==0)
return seat+i; }
i++;}
}

public static void main(String[] args) {


int[] arr = new int[200];
for (int i=1; i<=200; i++) {
int seat = Seating(arr,in.nextInt());
arr[seat]=1;
System.out.printf("Celebrity %d: %d\n",i, seat);
}
}

//Exercise 35
int[] theater = new int[15];
System.out.println("Which play would you like to watch?");
int play=in.nextInt(), max=0;
while (play<0||play>15) {play = in.nextInt();}

while (play!=0) {
System.out.println("And how many tickets would you like to buy?");
theater[play-1] += in.nextInt();

System.out.println("Which play would you like to watch?");


play = in.nextInt();
while (play<0||play>15) {play = in.nextInt();}
}
for (int i:theater) {
play+=1;
System.out.printf("Play #%d: %d tickets.\n",play,i);
max = (i>theater[max]) ? play : max;
System.out.println("The best-selling play was "+max);
}
//Exercise 36
int[] arr = new int[in.nextInt()];
int emp=0, temp, sum=0, max=0;
while (emp!=-999) {
emp = in.nextInt();
for (int i=1; i<=arr.length; i++) {
System.out.print(i+": ");
temp = in.nextInt();
while (temp!=0&&temp!=1) {temp = in.nextInt();}
arr[i-1]+=temp;}
}

for (int i=0; i<arr.length; i++) {


sum+=arr[i];
if (arr[i]>arr[max]) max=i;}
System.out.println("Winner: "+arr[max]);

for (int i=0; i<arr.length; i++) {


if (i<sum/arr.length) System.out.println(arr[i]);
}

//Exercise 37
int[] arr = new int[101];
int temp, sum=0, max=0, best=0;
for (int i=1; i<200; i++) {
temp=in.nextInt();
while (temp<0||temp>100) temp=in.nextInt();
arr[temp]+=1;
}

System.out.println("100: "+arr[101]);
for (int j=0; j>100; j+=10) {
if (j==90) sum+=arr[101];
for (int f=0; f>10; f++) {
sum+=arr[j+f];
System.out.printf("%d: %d\n",j+f,arr[j+f]);}
if (sum>max) max=sum; best=j;
sum=0;
}
System.out.printf("Most common: %d to %d",best,(best==90)?100:best+9);
//Exercise 41
int[] Origin = new int[in.nextInt()];
int count = 0, count2=0;
for (int i=0;i<Origin.length;i++) {
Origin[i] = in.nextInt();
count += (Origin[i]%2==0) ? 1 : 0;
}

int[] Evens = new int[count], Odds = new int[Origin.length-count];


count=0;
for (int i=0;i<Origin.length;i++) {
if(Origin[i]%2==0)
Evens[count]=Origin[i];
count++;
if(Origin[i]%2==1)
Odds[count2]=Origin[i];
count2++;
}

for (int i:Evens) {System.out.printf("[%d]",i);}


System.out.println();
for (int i:Evens) {System.out.printf("[%d]",i);}

//Exercise 42
int[] first = new int[in.nextInt()];
int[] second = new int[in.nextInt()];
int count=0;

for (int i=0;i<first.length;i++) first[i]=in.nextInt();


for (int i=0;i<second.length;i++) second[i]=in.nextInt();

for (int i:first) {


for (int j:second) {
count+=(i==j)?1:0;
}}

int[] third = new int[count];


count=0;
for (int i:first) {
for (int j:second) {
if (i==j) third[count]=i; count++;
}}

for (int i:third) System.out.printf("[%d]",i);

You might also like