Archive for the ‘Iterative’ Category

UVA – 11108 – Tautology   Leave a comment


Just another boring problem

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class Tautology {
	public static int map(char c) {
		if (c == 'p')
			return 0;
		if (c == 'q')
			return 1;
		if (c == 'r')
			return 2;
		if (c == 's')
			return 3;
		if (c == 't')
			return 4;
		return -1;
	}

	public static boolean evaluate(int mask, char[] arr) {
		Stack<Boolean> res = new Stack<Boolean>();
		for (int i = arr.length - 1; i >= 0; i--) {
			if (map(arr[i]) != -1)
				res.push((1 << map(arr[i]) & mask) != 0);
			else {
				if (arr[i] == 'K') {
					boolean a = res.pop();
					boolean b = res.pop();
					res.push(a && b);
				}
				if (arr[i] == 'A') {
					boolean a = res.pop();
					boolean b = res.pop();
					res.push(a || b);
				}
				if (arr[i] == 'N')
					res.push(!res.pop());
				if (arr[i] == 'C') {
					boolean a = res.pop();
					boolean b = res.pop();
					res.push(!a || b);
				}
				if (arr[i] == 'E') {
					boolean a = res.pop();
					boolean b = res.pop();
					res.push(a == b);
				}
			}
		}
		return res.pop();
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			char[] s = in.readLine().toCharArray();
			if (s.length == 1 && s[0] == '0')
				return;
			boolean ok = true;
			for (int i = 0; i < 1 << 5; i++) {
				ok = ok && evaluate(i, s);
			}
			if (ok)
				System.out.println("tautology");
			else
				System.out.println("not");
		}
	}

	static class InputReader {
		private BufferedReader reader;
		private StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}
	}
}

Posted July 5, 2012 by epicrado in Iterative

UVA – 10041 – Vito’s Family   Leave a comment


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class VitosFamily {

	public static void main(String[] args) throws Exception {
		InputReader in = new InputReader(System.in);
		int tc = in.nextInt();
		while (tc-- > 0) {
			int[] a = new int[in.nextInt()];
			for (int i = 0; i < a.length; i++)
				a[i] = in.nextInt();
			int min = 500 * 30000;
			for (int i = 0; i < a.length; i++) {
				int sum = 0;
				for (int j = 0; j < a.length; j++)
					sum += Math.abs(a[i] - a[j]);
				min = Math.min(min, sum);
			}
			System.out.println(min);
		}

	}

	static class InputReader {
		private BufferedReader reader;
		private StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}
	}
}

Posted April 30, 2012 by epicrado in Iterative

UVA – 10102 – The path in the colored field   Leave a comment


import java.io.*;
import java.util.*;

public class ThePathColoredField {
	public static int abs(int a) {
		return (a < 0) ? -a : a;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			String s = in.readLine();
			if (s == null)
				break;
			int m = Integer.parseInt(s);
			char[][] a = new char[m][];
			for (int i = 0; i < a.length; i++)
				a[i] = in.readLine().toCharArray();
			int maximin = 0;
			for (int i = 0; i < m; i++)
				for (int j = 0; j < m; j++) {
					if (a[i][j] == '1') {
						int min = 2 * m;
						for (int k = 0; k < m; k++)
							for (int l = 0; l < m; l++)
								if (a[k][l] == '3'
										&& abs(k - i) + abs(l - j) < min)
									min = abs(k - i) + abs(l - j);
						if (min > maximin)
							maximin = min;
					}
				}
			System.out.println(maximin);
		}
	}
}

Posted April 30, 2012 by epicrado in Iterative

UVA – 11059 – Maximum Product   Leave a comment


import java.util.*;

public class MaximumProduct {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int cc = 1;
		while (in.hasNext()) {
			int n = in.nextInt();
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = in.nextInt();
			}
			long max = 0;
			for (int i = 0; i < a.length; i++)
				for (int j = i; j < a.length; j++) {
					long cur = 1;
					for (int k = i; k <= j; k++)
						cur = cur * a[k];
					max = Math.max(max, cur);
				}
			System.out.printf("Case #%d: The maximum product is %d.\n\n", cc++,
					max);
		}
	}
}

Posted April 27, 2012 by epicrado in Iterative

UVA – 10365 – Blocks   Leave a comment


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Blocks {

	public static void main(String[] args) throws Exception {
		InputReader in = new InputReader(System.in);
		int tc = in.nextInt();
		while (tc-- > 0) {

			int v = in.nextInt();
			int min = Integer.MAX_VALUE;
			for (int i = 1; i <= v; i++)
				if (v % i == 0)
					for (int j = 1; j <= v / i; j++)
						if ((v / i) % j == 0) {
							int area = i * j + v / i + v / j;
							area <<= 1;
							min = Math.min(min, area);
						}
			System.out.println(min);
		}
	}

	static class InputReader {
		private BufferedReader reader;
		private StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}
	}
}

Posted April 27, 2012 by epicrado in Iterative

UVA – 386 – Perfect Cubes   Leave a comment


import java.util.Arrays;

public class PerfectCubes {
	public static void main(String[] args) {
		int[] cubes = new int[200];
		for (int i = 1; i <= 200; i++)
			cubes[i - 1] = i * i * i;
		StringBuilder[] res = new StringBuilder[200];
		for (int i = 0; i < res.length; i++)
			res[i] = new StringBuilder();
		for (int i = 2; i <= 200; i++)
			for (int j = i; j <= 200; j++)
				for (int k = j; k <= 200; k++) {
					int a = Arrays.binarySearch(cubes, i * i * i + j * j * j
							+ k * k * k);
					if (a >= 0) {
						a++;
						res[a - 1].append("Cube = " + (a) + ", Triple = (" + i
								+ "," + j + "," + k + ")\n");
					}
				}
		for (int i = 0; i < res.length; i++)
			System.out.print(res[i]);
	}
}

Posted April 27, 2012 by epicrado in Iterative

UVA – 10976 – Fractions Again?!   3 comments


#include <stdio.h>
int main()
{
	int k,i,count;
	while(scanf("%d",&k)==1)
	{
		count = 0;
		for(i = k+1;i<=2*k;i++)
			if((i*k)%(i-k)==0)
				count++;
		printf("%d\n",count);
		for(i = k+1;i<=2*k;i++)
			if((i*k)%(i-k)==0)
					printf("%d/%d = %d/%d + %d/%d\n",1,k,1,(i*k)/(i-k),1,i);

	}
	return 0;
}

Posted April 27, 2012 by epicrado in ad-hoc, Iterative, Mathematics