forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kdn251#29 from fatosmorina/master
Solve UVa problems
- Loading branch information
Showing
8 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/bin/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>interviews</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
</buildSpec> | ||
<natures> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="false"> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
/** | ||
* Given a base b and two non-negative base b integers | ||
* p and m, compute p mod m and print the | ||
* result as a base-b integer. p mod m is defined | ||
* as the smallest non-negative integer k such that | ||
* p = a ∗ m + k for some integer a. | ||
* Input | ||
* Input consists of a number of cases. Each case is | ||
* represented by a line containing three unsigned | ||
* integers. The first, b, is a decimal number between | ||
* 2 and 10. The second, p, contains up to 1000 digits between 0 and b − 1. The third, m, contains | ||
* up to 9 digits between 0 and b − 1. The last case is followed by a line containing ‘0’. | ||
* Output | ||
* For each test case, print a line giving p mod m as a base-b integer. | ||
* Sample Input | ||
* 2 1100 101 | ||
* 10 123456789123456789123456789 1000 | ||
* 0 | ||
* Sample Output | ||
* 10 | ||
* 789 | ||
* | ||
*/ | ||
|
||
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1492 | ||
|
||
import java.math.BigInteger; | ||
import java.util.Scanner; | ||
|
||
public class BasicRemains { | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
while (input.hasNext()) { | ||
int baseNumber = input.nextInt(); | ||
if (baseNumber == 0) { | ||
break; | ||
} | ||
BigInteger p = new BigInteger(input.next(), baseNumber); | ||
BigInteger m = new BigInteger(input.next(), baseNumber); | ||
System.out.println((p.mod(m)).toString(baseNumber)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
/** | ||
* A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between | ||
* successive elements take on all the values 1 through n − 1. For instance, | ||
* 1 4 2 3 | ||
* is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies | ||
* that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether | ||
* or not each of a number of sequences is a jolly jumper. | ||
* Input | ||
* Each line of input contains an integer n ≤ 3000 followed by n integers representing the sequence. | ||
* Output | ||
* For each line of input, generate a line of output saying ‘Jolly’ or ‘Not jolly’. | ||
* Sample Input | ||
* 4 1 4 2 3 | ||
* 5 1 4 2 -1 6 | ||
* Sample Output | ||
* Jolly | ||
* Not jolly | ||
*/ | ||
|
||
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=979 | ||
|
||
import java.util.HashSet; | ||
import java.util.Scanner; | ||
import java.util.Set; | ||
|
||
|
||
public class JollyJumpers { | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
while (input.hasNext()) { | ||
Set<Integer> numbersAlreadyAdded = new HashSet<Integer>(); | ||
int numberOfElements = input.nextInt(); | ||
int[] numbers = new int[numberOfElements]; | ||
for (int i = 0; i < numberOfElements; i++) { | ||
numbers[i] = input.nextInt(); | ||
} | ||
for (int i = 0; i < numberOfElements - 1; i++) { | ||
int difference = Math.abs(numbers[i] - numbers[i + 1]); | ||
if (difference > 0 && difference < numberOfElements) { | ||
numbersAlreadyAdded.add(difference); | ||
} | ||
} | ||
if (numbersAlreadyAdded.size() == (numberOfElements - 1)) { | ||
System.out.println("Jolly"); | ||
} else { | ||
System.out.println("Not jolly"); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* News agency pays money for articles according to some rules. Each character has its own value (some | ||
* characters may have value equals to zero). Author gets his payment as a sum of all character’s values | ||
* in the article. You have to determine the amount of money that news agency must pay to an author. | ||
* Input | ||
* The first line contains integer N (0 < N ≤ 5), it is a number of tests. Each test describes an integer | ||
* K (0 < K ≤ 100), the number of paid characters. On next K lines there are table of paid characters | ||
* and its values (character values are written in cents). If character can not be found in the table, then | ||
* its value is equal to zero. Next, there is integer M (0 < M ≤ 150000). Next M lines contain an article | ||
* itself. Each line can be up to 10000 characters length. Be aware of a large input size, the whole input | ||
* file is about 7MB. | ||
* Output | ||
* For each test print how much money publisher must pay for an article in format ‘x.yy$’. Where x is | ||
* a number of dollars without leading zeros, and yy number of cents with one leading zero if necessary. | ||
* Examples: ‘3.32$’, ‘13.07$’, ‘71.30$’, ‘0.09$’. | ||
* Sample Input | ||
* 1 | ||
* 7 | ||
* a 3 | ||
* W 10 | ||
* A 100 | ||
* , 10 | ||
* k 7 | ||
* . 3 | ||
* I 13 | ||
* 7 | ||
* ACM International Collegiate Programming Contest (abbreviated | ||
* as ACM-ICPC or just ICPC) is an annual multi-tiered competition | ||
* among the universities of the world. The ICPC challenges students | ||
* to set ever higher standards of excellence for themselves | ||
* through competition that rewards team work, problem analysis, | ||
* and rapid software development. | ||
* From Wikipedia. | ||
* Sample Output | ||
* 3.74$ | ||
*/ | ||
|
||
//https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2315 | ||
|
||
import java.text.DecimalFormat; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class Newspaper { | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
int numberOfTestCases = input.nextInt(); | ||
while (numberOfTestCases != 0) { | ||
Map<String, Integer> values = new HashMap<String, Integer>(); | ||
int numberOfValuableCharacters = input.nextInt(); | ||
while (numberOfValuableCharacters != 0) { | ||
values.put(input.next(), input.nextInt()); | ||
numberOfValuableCharacters--; | ||
} | ||
int numberOfLines = input.nextInt(); | ||
input.nextLine(); | ||
double sum = 0; | ||
while (numberOfLines != 0) { | ||
String textAsString = input.nextLine(); | ||
for (int i = 0; i < textAsString.length(); i++) { | ||
String c = textAsString.charAt(i) + ""; | ||
if (values.containsKey(c)) { | ||
sum = sum + values.get(c); | ||
} | ||
} | ||
numberOfLines--; | ||
} | ||
sum = sum / 100; | ||
DecimalFormat formatter = new DecimalFormat("0.00"); | ||
String sumFormatted = formatter.format(sum); | ||
System.out.println(sumFormatted + "$"); | ||
numberOfTestCases--; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* The most relevant definition for this problem is 2a: An integer g > 1 is said to be prime if and only | ||
* if its only positive divisors are itself and one (otherwise it is said to be composite). For example, the | ||
* number 21 is composite; the number 23 is prime. Note that the decompositon of a positive number g | ||
* into its prime factors, i.e., | ||
* g = f1 × f2 × · · · × fn | ||
* is unique if we assert that fi > 1 for all i and fi ≤ fj for i < j. | ||
* One interesting class of prime numbers are the so-called Mersenne primes which are of the form | ||
* 2 | ||
* p − 1. Euler proved that 2 | ||
* 31 − 1 is prime in 1772 — all without the aid of a computer. | ||
* Input | ||
* The input will consist of a sequence of numbers. Each line of input will contain one number g in the | ||
* range −2 | ||
* 31 < g < 2 | ||
* 31, but different of -1 and 1. The end of input will be indicated by an input line | ||
* having a value of zero. | ||
* Output | ||
* For each line of input, your program should print a line of output consisting of the input number and | ||
* its prime factors. For an input number g > 0, g = f1 × f2 × · · · × fn, where each fi | ||
* is a prime number | ||
* greater than unity (with fi ≤ fj for i < j), the format of the output line should be | ||
* g = f1 x f2 x . . . x fn | ||
* When g < 0, if | g |= f1 × f2 × · · · × fn, the format of the output line should be | ||
* g = -1 x f1 x f2 x . . . x fn | ||
* Sample Input | ||
* -190 | ||
* -191 | ||
* -192 | ||
* -193 | ||
* -194 | ||
* 195 | ||
* 196 | ||
* 197 | ||
* 198 | ||
* 199 | ||
* 200 | ||
* 0 | ||
* Sample Output | ||
* -190 = -1 x 2 x 5 x 19 | ||
* -191 = -1 x 191 | ||
* -192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3 | ||
* -193 = -1 x 193 | ||
* -194 = -1 x 2 x 97 | ||
* 195 = 3 x 5 x 13 | ||
* 196 = 2 x 2 x 7 x 7 | ||
* 197 = 197 | ||
* 198 = 2 x 3 x 3 x 11 | ||
* 199 = 199 | ||
* 200 = 2 x 2 x 2 x 5 x 5 | ||
*/ | ||
|
||
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=524 | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class PrimeFactors { | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
int number = input.nextInt(); | ||
boolean[] isPrime = generatePrimeNumbers(); | ||
while (number != 0) { | ||
boolean isNegative = false; | ||
if (number < 0) { | ||
isNegative = true; | ||
number = Math.abs(number); | ||
} | ||
int originalNumber = number; | ||
formatOutput(originalNumber, sieveOfEratosthenes(isPrime, originalNumber), isNegative); | ||
number = input.nextInt(); | ||
} | ||
} | ||
|
||
public static List<Integer> sieveOfEratosthenes(boolean[] isPrime, int number) { | ||
List<Integer> primeFactors = new ArrayList<Integer>(); | ||
int squareRootOfOriginalNumber = (int) Math.sqrt(number); | ||
for (int i = 2; i <= squareRootOfOriginalNumber; i++) { | ||
if (isPrime[i]) { | ||
while (number % i == 0) { | ||
primeFactors.add(i); | ||
number = number / i; | ||
} | ||
} | ||
} | ||
if (number != 1) { | ||
primeFactors.add(number); | ||
} | ||
return primeFactors; | ||
} | ||
|
||
static void formatOutput(int number, List<Integer> primeFactors, boolean isNegative) { | ||
if (isNegative) { | ||
number *= -1; | ||
} | ||
StringBuilder output = new StringBuilder(number + " = "); | ||
int numberOfPrimeFactors = primeFactors.size(); | ||
if (numberOfPrimeFactors == 1) { | ||
if (isNegative) { | ||
output.append("-1 x " + (number * (-1))); | ||
} else { | ||
output.append(number); | ||
} | ||
} else { | ||
Collections.sort(primeFactors); | ||
if (isNegative) { | ||
output.append("-1 x "); | ||
} | ||
for (int i = 0; i < numberOfPrimeFactors - 1; i++) { | ||
output.append(primeFactors.get(i) + " x "); | ||
} | ||
output.append(primeFactors.get(numberOfPrimeFactors - 1)); | ||
} | ||
System.out.println(output); | ||
} | ||
|
||
static boolean[] generatePrimeNumbers() { | ||
int number = (int) Math.sqrt(Integer.MAX_VALUE); | ||
boolean[] isPrime = new boolean[number + 1]; | ||
for (int i = 2; i < number + 1; i++) { | ||
isPrime[i] = true; | ||
} | ||
for (int factor = 2; factor * factor < number + 1; factor++) { | ||
if (isPrime[factor]) { | ||
for (int j = factor; j * factor < number + 1; j++) { | ||
isPrime[j * factor] = false; | ||
} | ||
} | ||
} | ||
return isPrime; | ||
} | ||
|
||
} |
Oops, something went wrong.