Added Java code

This commit is contained in:
Anthony Wang 2020-08-01 15:17:07 -05:00
parent 39af2624d5
commit 25f432f7e1
5 changed files with 190 additions and 78 deletions

5
.gitignore vendored
View file

@ -1,7 +1,4 @@
*
!*/
!*.*
.vscode
.vscode/*
*.in
*.out
*.ans

74
.vscode/settings.json vendored
View file

@ -1,74 +0,0 @@
{
"files.associations": {
"iostream": "cpp",
"iomanip": "cpp",
"ostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"scoped_allocator": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp"
}
}

80
billboard.java Normal file
View file

@ -0,0 +1,80 @@
import java.io.*;
import java.util.*;
public class billboard {
public static void main(String[] args) throws IOException {
// initialize file I/O
BufferedReader br = new BufferedReader(new FileReader("billboard.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("billboard.out")));
// read in the locations of the first billboard
StringTokenizer st = new StringTokenizer(br.readLine());
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
// read in the locations of the second billboard
st = new StringTokenizer(br.readLine());
int x3 = Integer.parseInt(st.nextToken());
int y3 = Integer.parseInt(st.nextToken());
int x4 = Integer.parseInt(st.nextToken());
int y4 = Integer.parseInt(st.nextToken());
// read in the locations of the truck
st = new StringTokenizer(br.readLine());
int x5 = Integer.parseInt(st.nextToken());
int y5 = Integer.parseInt(st.nextToken());
int x6 = Integer.parseInt(st.nextToken());
int y6 = Integer.parseInt(st.nextToken());
// the visible area is the sum of the visible area of the first billboard and the second billboard
int combinedArea = visibleArea(x1, y1, x2, y2, x5, y5, x6, y6) + visibleArea(x3, y3, x4, y4, x5, y5, x6, y6);
// print the answer
pw.println(combinedArea);
pw.close();
}
/**
* Given the lower-left and upper-right corners of a rectangle, return the area of the rectangle
* @param x1 x-coordinate of lower-left corner
* @param y1 y-coordinate of lower-left corner
* @param x2 x-coordinate of upper-right corner
* @param y2 y-coordinate of upper-right corner
* @return area of the rectangle
*/
public static int areaOfRectangle(int x1, int y1, int x2, int y2) {
return (x2-x1)*(y2-y1);
}
/**
* Given the corners of two rectangles, return the area inside the first rectangle
* but outside the second
* @param x1 x-coordinate of lower-left corner of first rectangle
* @param y1 y-coordinate of lower-left corner of first rectangle
* @param x2 x-coordinate of upper-right corner of first rectangle
* @param y2 y-coordinate of upper-right corner of first rectangle
* @param x3 x-coordinate of lower-left corner of second rectangle
* @param y3 y-coordinate of upper-right corner of second rectangle
* @param x4 x-coordinate of lower-left corner of second rectangle
* @param y4 y-coordinate of upper-right corner of second rectangle
* @return
*/
public static int visibleArea(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
// start by computing the area that would be visible if there were no second rectangle
int visibleArea = areaOfRectangle(x1, y1, x2, y2);
// compute the boundaries of the intersection
int leftmostBlockedX = Math.max(x1, x3);
int rightmostBlockedX = Math.min(x2, x4);
int bottommostBlockedY = Math.max(y1, y3);
int topmostBlockedY = Math.min(y2, y4);
// if the second rectangle does exist, subtract out the area that it blocks
if(leftmostBlockedX < rightmostBlockedX && bottommostBlockedY < topmostBlockedY) {
visibleArea -= areaOfRectangle(leftmostBlockedX, bottommostBlockedY, rightmostBlockedX, topmostBlockedY);
}
return visibleArea;
}
}

60
measurement.java Normal file
View file

@ -0,0 +1,60 @@
import java.io.*;
import java.util.*;
public class measurement {
public static void main(String[] args) throws IOException {
// initialize file I/O
BufferedReader br = new BufferedReader(new FileReader("measurement.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("measurement.out")));
// read in all of the notes
int n = Integer.parseInt(br.readLine());
int[] day = new int[n];
String[] cow = new String[n];
int[] change = new int[n];
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
day[i] = Integer.parseInt(st.nextToken());
cow[i] = st.nextToken();
change[i] = Integer.parseInt(st.nextToken());
}
// the milk variables track the amount of milk that each cows was last known to produce
int bessieMilk = 7, elsieMilk = 7, mildredMilk = 7;
// the on variables are true if that cow produced the highest amount of milk on the previous day
boolean bessieOn = true, elsieOn = true, mildredOn = true;
int dayAdjust = 0;
for(int currDay = 1; currDay <= 100; currDay++) {
// look through the notes to see if there were any changes on this day
for(int i = 0; i < n; i++) {
if(day[i] == currDay) {
if(cow[i].equals("Bessie")) {
bessieMilk += change[i];
}
if(cow[i].equals("Elsie")) {
elsieMilk += change[i];
}
if(cow[i].equals("Mildred")) {
mildredMilk += change[i];
}
}
}
// compute the highest milk total and see which cows produced the most milk
int highestMilk = Math.max(bessieMilk, Math.max(elsieMilk, mildredMilk));
boolean bessieOnNext = bessieMilk == highestMilk;
boolean elsieOnNext = elsieMilk == highestMilk;
boolean mildredOnNext = mildredMilk == highestMilk;
if(bessieOn != bessieOnNext || elsieOn != elsieOnNext || mildredOn != mildredOnNext) {
dayAdjust++;
}
bessieOn = bessieOnNext;
elsieOn = elsieOnNext;
mildredOn = mildredOnNext;
}
// print the answer
pw.println(dayAdjust);
pw.close();
}
}

49
shuffle.java Normal file
View file

@ -0,0 +1,49 @@
import java.io.*;
import java.util.*;
public class shuffle {
public static void main(String[] args) throws IOException {
// initialize file I/O
BufferedReader br = new BufferedReader(new FileReader("shuffle.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("shuffle.out")));
// read in the number of cows
int n = Integer.parseInt(br.readLine());
// if a cow was in position i after shuffling, then moveTo[i] will
// be the location that they were in before the shuffle
int[] moveTo = new int[n+1];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 1; i <= n; i++) {
// destination is the location a cow would be after a shuffle
// if they were originally in position i
int destination = Integer.parseInt(st.nextToken());;
moveTo[destination] = i;
}
// allocate an array to store the observed locations of all cows
// read in the observations
int[] finalLocs = new int[n+1];
st = new StringTokenizer(br.readLine());
for(int i = 1; i <= n; i++) {
finalLocs[i] = Integer.parseInt(st.nextToken());
}
// allocate an array to store the original locations of all cows
int[] originalLocations = new int[n+1];
for(int finalPosition = 1; finalPosition <= n; finalPosition++) {
int currentLocation = finalPosition;
// reverse three shuffles
for(int iter = 1; iter <= 3; iter++) {
currentLocation = moveTo[currentLocation];
}
// store the original location of the cow that ended up in finalPosition
originalLocations[currentLocation] = finalLocs[finalPosition];
}
// print the answer
for(int i = 1; i <= n; i++) {
pw.println(originalLocations[i]);
}
pw.close();
}
}