Skip to content

Commit 3cf7d5c

Browse files
committed
Adding old CodeChef, Codeforces, CodinGame and Google CodeJam, HashCode, Kickstart files
1 parent c37e4e9 commit 3cf7d5c

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.Scanner;
2+
3+
public class SashaMagneticMachines
4+
{
5+
public static void main(String[] args)
6+
{
7+
String firstLine = new Scanner(System.in).nextLine();
8+
int numberOfMachines = Integer.parseInt(firstLine);
9+
String secondLine = new Scanner(System.in).nextLine();
10+
String[] powersOfMachinesStrings = secondLine.split(" ");
11+
int[] powersOfMachinesInt = new int[numberOfMachines];
12+
int[] xDivisors = new int [numberOfMachines];
13+
int smallestPower = Integer.parseInt(powersOfMachinesStrings[0]);
14+
15+
for (int i = 0; i < numberOfMachines; i++)
16+
{
17+
powersOfMachinesInt[i] = Integer.parseInt(powersOfMachinesStrings[i]);
18+
if (powersOfMachinesInt[i] < smallestPower)
19+
{
20+
smallestPower = powersOfMachinesInt[i];
21+
}
22+
}
23+
24+
for (int i = 0; i < numberOfMachines; i++)
25+
{
26+
for (int j = 0; j < numberOfMachines; j++)
27+
{
28+
if (i != j)
29+
{
30+
if (powersOfMachinesInt[i] / powersOfMachinesInt[j] > 2)
31+
{
32+
33+
}
34+
else if (powersOfMachinesInt[j] / powersOfMachinesInt[i] > 2)
35+
{
36+
37+
}
38+
/*if (!isPrime(powersOfMachinesInt[i]) && powersOfMachinesInt[i] != 1)
39+
{
40+
41+
smallestPower *
42+
}*/
43+
}
44+
}
45+
}
46+
}
47+
48+
public static boolean isPrime(int nunber)
49+
{
50+
int num = nunber;
51+
boolean flag = false;
52+
for(int i = 2; i <= num/2; ++i)
53+
{
54+
// condition for nonprime number
55+
if(num % i == 0)
56+
{
57+
flag = true;
58+
break;
59+
}
60+
}
61+
62+
return flag;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.Scanner;
2+
3+
public class SashaTrip
4+
{
5+
public static void main(String[] args)
6+
{
7+
String input = new Scanner(System.in).nextLine();
8+
String[] arrayInputs = input.split(" ");
9+
int cities = Integer.parseInt(arrayInputs[0]);
10+
int capacity = Integer.parseInt(arrayInputs[1]);
11+
int filled = 0;
12+
int cost = 0;
13+
14+
if (capacity >= (cities - 1))
15+
{
16+
System.out.println((cities - 1));
17+
}
18+
else
19+
{
20+
for (int i = 1; i < cities + 1; i++)
21+
{
22+
int distanceToDrive = cities - i;
23+
int newFill = 0;
24+
// System.out.println("cicle init filled of " + i + " = " + filled);
25+
// System.out.println("cicle init distanceToDrive of " + i + " = " + distanceToDrive);
26+
if (filled < distanceToDrive)
27+
{
28+
if (capacity >= (distanceToDrive - filled))
29+
{
30+
// System.out.println("if init filled of " + i + " = " + filled);
31+
// System.out.println("if init cost of " + i + " = " + cost);
32+
newFill = capacity - filled;
33+
filled = filled + newFill;
34+
cost = cost + newFill * i;
35+
// System.out.println("if newFill of " + i + " = " + newFill);
36+
// System.out.println("if end filled of " + i + " = " + filled);
37+
// System.out.println("if end cost of " + i + " = " + cost);
38+
}
39+
else
40+
{
41+
// System.out.println("else init filled of " + i + " = " + filled);
42+
// System.out.println("else init cost of " + i + " = " + cost);
43+
newFill = capacity - filled;
44+
filled = filled + newFill;
45+
cost = cost + newFill * i;
46+
// System.out.println("else newFill of " + i + " = " + newFill);
47+
// System.out.println("else end filled of " + i + " = " + filled);
48+
// System.out.println("else end cost of " + i + " = " + cost);
49+
}
50+
// drives 1
51+
filled--;
52+
}
53+
// drives 1
54+
else filled--;
55+
}
56+
System.out.println((cost));
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.Scanner;
2+
3+
public class BePositive
4+
{
5+
public static void main(String[] args)
6+
{
7+
Scanner input = new Scanner(System.in);
8+
int n = input.nextInt();
9+
10+
int halfN;
11+
12+
if (n % 2 == 0)
13+
{
14+
halfN = n/2;
15+
}
16+
else halfN = n/2 + 1;
17+
18+
input.nextLine();
19+
20+
String integersString = input.nextLine();
21+
String[] integersArray = integersString.split(" ");
22+
23+
int countPositive = 0;
24+
int countNegative = 0;
25+
26+
for (int i = 0; i < n; i++)
27+
{
28+
if (Integer.parseInt(integersArray[i]) > 0)
29+
{
30+
countPositive++;
31+
}
32+
else if (Integer.parseInt(integersArray[i]) < 0)
33+
{
34+
countNegative++;
35+
}
36+
}
37+
38+
if (countPositive >= halfN)
39+
{
40+
System.out.println(countPositive);
41+
}
42+
else if (countNegative >= halfN)
43+
{
44+
System.out.println((0 - countNegative));
45+
}
46+
else System.out.println(0);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Scanner;
2+
3+
public class Connect
4+
{
5+
6+
public static void main(String[] args)
7+
{
8+
Scanner input = new Scanner(System.in);
9+
int widthOfGrid = input.nextInt();
10+
11+
input.nextLine();
12+
13+
int rowPositionOfAlice = input.nextInt();
14+
int columnPositionOfAlice = input.nextInt();
15+
16+
input.nextLine();
17+
18+
int rowPositionDestination = input.nextInt();
19+
int columnPositionDestination = input.nextInt();
20+
21+
input.nextLine();
22+
23+
int[][] earthSeaArray = new int[widthOfGrid][widthOfGrid];
24+
25+
for (int i = 0; i < widthOfGrid; i++)
26+
{
27+
String earthSeaString = input.nextLine();
28+
29+
for (int j = 0; j < widthOfGrid; j++)
30+
{
31+
earthSeaArray[i][j] = Integer.parseInt(earthSeaString.substring(j, j + 1));
32+
}
33+
}
34+
35+
36+
}
37+
38+
int move()
39+
{
40+
return 0;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.ArrayList;
2+
import java.util.Scanner;
3+
4+
public class TwoCakes
5+
{
6+
public static void main(String[] args)
7+
{
8+
Scanner input = new Scanner(System.in);
9+
int sizes = Integer.parseInt(input.nextLine());
10+
11+
int houses = 2 * sizes;
12+
13+
int[] street = new int[houses];
14+
15+
String tiersString = input.nextLine();
16+
String[] tiersArray = tiersString.split(" ");
17+
18+
ArrayList<Integer> tiersArrayList = new ArrayList<Integer>();
19+
20+
for (int i = 0; i < houses; i++)
21+
{
22+
tiersArrayList.add(Integer.parseInt(tiersArray[i]));
23+
}
24+
25+
int cakesBoughtLeft = 0;
26+
int cakesBoughtRight = 0;
27+
int positionNowLeft = 0;
28+
int positionNowRight = 0;
29+
int cakeTierNowLeft = 0;
30+
int cakeTierNowRight = 0;
31+
int traveledDistanceLeft = 0;
32+
int traveledDistanceRight = 0;
33+
34+
while (cakesBoughtLeft + cakesBoughtRight < houses)
35+
{
36+
System.out.println("while");
37+
int[] resultLeft = distanceToCake(tiersArrayList, positionNowLeft, positionNowRight, cakeTierNowLeft);
38+
traveledDistanceLeft = resultLeft[0] + traveledDistanceLeft;
39+
positionNowLeft = resultLeft[1];
40+
cakeTierNowLeft = resultLeft[2];
41+
cakesBoughtLeft++;
42+
43+
int[] resultRight = distanceToCake(tiersArrayList, positionNowRight, positionNowLeft, cakeTierNowRight);
44+
traveledDistanceRight = resultRight[0] + traveledDistanceRight;
45+
positionNowRight = resultRight[1];
46+
cakeTierNowRight = resultRight[2];
47+
cakesBoughtRight++;
48+
}
49+
50+
System.out.println((traveledDistanceLeft + traveledDistanceRight));
51+
}
52+
53+
public static int[] distanceToCake(ArrayList<Integer> theTiersArrayList, int positionNowLeft, int positionNowRight, int cakeTierNow)
54+
{
55+
int[] result = new int[3]; // distance, new position, new cake tier
56+
int distance = 0;
57+
int newPosition = positionNowLeft;
58+
int newCakeTier = cakeTierNow;
59+
60+
for (int i = 1; i < theTiersArrayList.size(); i++)
61+
{
62+
System.out.println("for");
63+
int tierNow = theTiersArrayList.get(positionNowLeft);
64+
65+
if (positionNowLeft + i < theTiersArrayList.size() && positionNowLeft + i != positionNowRight && tierNow == theTiersArrayList.get(positionNowLeft + i) - 1)
66+
{
67+
System.out.println("if");
68+
distance = distance + i;
69+
newPosition = positionNowLeft + i;
70+
newCakeTier++;
71+
break;
72+
}
73+
else if (positionNowLeft - i >= 0 && positionNowLeft - i != positionNowRight && tierNow == theTiersArrayList.get(positionNowLeft - i) - 1)
74+
{
75+
System.out.println("else");
76+
distance = distance + i;
77+
newPosition = positionNowLeft - i;
78+
newCakeTier++;
79+
break;
80+
}
81+
}
82+
result[0] = distance;
83+
result[1] = newPosition;
84+
result[2] = newCakeTier;
85+
86+
return result;
87+
}
88+
}

0 commit comments

Comments
 (0)