Professional Documents
Culture Documents
CSHW#9
CSHW#9
//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]);}
}
//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++;}
}
//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();
//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;
}
//Exercise 42
int[] first = new int[in.nextInt()];
int[] second = new int[in.nextInt()];
int count=0;