Pages

Sunday 30 December 2012

UVA - 11466 - Largest Prime Divisor

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while (true) {
            long x = Math.abs(Long.parseLong(br.readLine()));
            if (x == 0) {
                break;
            }
            long temp = -1;
            long counterP=0;
            long tempX=x;
            for (int i = 2; i < Math.sqrt(x) + 1; i++) {
                while(tempX % i == 0) {
                    if(temp!=i){
                        counterP++;
                    }
                    temp=i;
                    tempX/=i;
                }
            }
            if(x!=tempX &&tempX!=1){
                temp=tempX;
                counterP++;
            }
            if(counterP<2)
                sb.append(-1).append("\n");
            else
                sb.append(temp).append("\n");
        }
        System.out.print(sb);
    }
}

Friday 28 December 2012

UVA - 438 - The Circumference of the Circle

#include <stdio.h>
#include <math.h>
#define pi 3.141592653589793

double circ(double x,double y,double z){
        double s=(x+y+z)/2;
        return (x*y*z)*pi/(2.0*sqrt(s*(s-x)*(s-y)*(s-z)));
}

double ecuD(double x1,double y1,double x2,double y2){
    return sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
}

int main() {
    double x1,y1,x2,y2,x3,y3,x,y,z;
    while (scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==6){
        x=ecuD(x1,y1,x2,y2);
        y=ecuD(x1,y1,x3,y3);
        z=ecuD(x2,y2,x3,y3);
        printf("%.2lf\n",circ(x,y,z));
    }
    return 0;
}

UVA - 10195 - The Knights Of The Round Table

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import javax.crypto.Mac;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            StringTokenizer st=new StringTokenizer(m);
            double x=Double.parseDouble(st.nextToken());
            double y=Double.parseDouble(st.nextToken());
            double z=Double.parseDouble(st.nextToken());
            double ans=rad(x, y, z);
            String f=String.format("The radius of the round table is: %.3f\n",ans);
            sb.append(f);
        }
        System.out.print(sb);
    }
   
    static double rad(double x,double y,double z){
        if(x==0 ||y==0 ||z==0)
            return 0;
        double halfP=(x+y+z)/2;
        return Math.sqrt((halfP-x)*(halfP-y)*(halfP-z)/halfP);
    }
}

UVA - 1339 - Ancient Cipher (C solution)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>

using namespace std;

int main() {
    char x[101], y[101];
    while(gets(x)){
        gets(y);
        int len=strlen(x);
        int arr1[26],arr2[26];
        for(int i=0;i<26;i++){
            arr1[i]=arr2[i]=0;
        }
        for(int i=0;i<len;i++){
            arr1[x[i]-65]++;
            arr2[y[i]-65]++;
        }
        sort(arr1, arr1 + sizeof (arr1) / sizeof (arr1[0]));
        sort(arr2, arr2 + sizeof (arr2) / sizeof (arr2[0]));
        bool flag=true;
        for(int i=0;i<26;i++){
            if(arr1[i]!=arr2[i]){
                flag=false;
                break;
            }
        }
        if(flag){
            puts("YES");
        }else{
            puts("NO");
        }
    }
    return 0;
}

UVA - 1339 - Ancient Cipher (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            String x=m;
            String y=br.readLine();
            if(distBetWords(x, y)){
                sb.append("YES\n");
            }else{
                sb.append("NO\n");
            }
        }
        System.out.print(sb);
    }
   
    static boolean distBetWords(String x,String y){
        int sum1=0,sum2=0;
        int[]arr1=new int[26],arr2=new int[26];
        for (int i = 0; i < x.length(); i++) {
            arr1[x.charAt(i)-65]++;
            arr2[y.charAt(i)-65]++;
        }
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        StringBuilder sb=new StringBuilder();
        StringBuilder sb2=new StringBuilder();
        for (int i = 0; i < 26; i++) {
           sb.append(arr1[i]);
           sb2.append(arr2[i]);
        }
        if(sb.toString().equals(sb2.toString())){
            return true;
        }
        return false;
    }
}

UVA - 1419 - Binary Clock

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        int cases=Integer.parseInt(br.readLine().trim());
        for(int l=1;l<cases+1;l++){
            String[] str=br.readLine().trim().split(":");
            StringBuilder hour=lengthOfWord(Integer.parseInt(str[0]));
            StringBuilder minute=lengthOfWord(Integer.parseInt(str[1]));
            StringBuilder second=lengthOfWord(Integer.parseInt(str[2]));
            char[][]ver=new char[6][3];
            char[][]hor=new char[3][6];
            for(int i=0;i<6;i++){
                ver[i][0]=hour.charAt(i);
                hor[0][i]=ver[i][0];
                ver[i][1]=minute.charAt(i);
                hor[1][i]=ver[i][1];
                ver[i][2]=second.charAt(i);
                hor[2][i]=ver[i][2];
            }
            sb.append(l).append(" ");
            for(int i=0;i<6;i++){
                for(int j=0;j<3;j++){
                    sb.append(ver[i][j]);
                }
            }
            sb.append(" ");
            for(int i=0;i<3;i++){
                for(int j=0;j<6;j++){
                    sb.append(hor[i][j]);
                }
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
   
    static StringBuilder lengthOfWord(int h){
        StringBuilder x=new StringBuilder(Integer.toString(h, 2));
        for(int i=x.length();i<6;i++){
                x.insert(0, "0");
        }
        return x;
    }
}

Thursday 27 December 2012

UVA - 531 - Compromise

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            Queue<String> qu=new LinkedList<String>();
            while(true){
                StringTokenizer st=new StringTokenizer(m);
                int countTok=st.countTokens();
                for(int i=0;i<countTok;i++){
                    qu.add(st.nextToken());
                }
                m=br.readLine();
                if(m.trim().equals("#")){
                    break;
                }
            }
            String[]arr1=new String[qu.size()];
            for(int i=0;i<arr1.length;i++){
                arr1[i]=qu.remove();
            }
            Queue<String> qu2=new LinkedList<String>();
            while(true){
                StringTokenizer st=new StringTokenizer(m);
                int countTok=st.countTokens();
                for(int i=0;i<countTok;i++){
                    qu2.add(st.nextToken());
                }
                m=br.readLine();
                if(m.trim().equals("#")){
                    break;
                }
            }
            String[]arr2=new String[qu2.size()];
            for(int i=0;i<arr2.length;i++){
                arr2[i]=qu2.remove();
            }
            LinkedList<String> list=LCS(arr1, arr2);
            boolean first=true;
            while(!list.isEmpty()){
                if(!first)
                    sb.append(" ");
                sb.append(list.remove());
                first=false;
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
  
    static LinkedList<String> LCS(String[] m, String[] n) {
        int[][] arr = new int[m.length + 1][n.length + 1];
        for (int i = 0; i < m.length + 1; i++) {
            arr[i][0] = 0;
        }
        for (int i = 0; i < n.length + 1; i++) {
            arr[0][i] = 0;
        }
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < n.length; j++) {
                if (m[i].equals(n[j])) {
                    arr[i + 1][j + 1] = arr[i][j] + 1;
                }else{
                   arr[i+1][j+1]=Math.max(arr[i+1][j],arr[i][j+1]);
                }
            }
        }
        int mpos = m.length, npos = n.length;
        LinkedList<String> result = new LinkedList<String>();

        while (mpos != 0 && npos != 0) {
            if (m[mpos - 1].equals(n[npos - 1])) {
                result.add(m[mpos - 1]);
                mpos--;
                npos--;
            } else if (arr[mpos][npos - 1] >= arr[mpos][npos]) {
                npos--;
            } else {
                mpos--;
            }
        }
        Collections.reverse(result);
        return result;
    }

}

UVA - 10209 - Is This Integration ? (C solution)

#include <stdio.h>
#include <math.h>

int main()
{
    double x,y,z,l,areaOfRect;
    double pi=acos(-1);
    while(scanf("%lf",&x)==1){
        areaOfRect=x*x;
        y = areaOfRect * (1 - sqrt(3) + pi / 3);
        z = areaOfRect * (2 * sqrt(3) - 4 + pi / 3);
        l = areaOfRect * (4 - sqrt(3) - 2 * pi/ 3);
        printf("%.3f %.3f %.3f\n", y,z,l);
    }
    return 0;
}

UVA - 10209 - Is This Integration ? (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            double a=Double.parseDouble(m);
            double areaOfRect=a*a;
            double x = areaOfRect * (1 - Math.sqrt(3) + Math.PI / 3); 
            double y = areaOfRect * (2 * Math.sqrt(3) - 4 + Math.PI / 3); 
            double z = areaOfRect * (4 - Math.sqrt(3) - 2 * Math.PI / 3);
            String temp=String.format("%.3f %.3f %.3f\n", x,y,z);
            sb.append(temp);
        }
        System.out.print(sb);
    }

}

UVA - 10375 - Choose and divide


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            StringTokenizer st=new StringTokenizer(m);
            int p=Integer.parseInt(st.nextToken());
            int q=Integer.parseInt(st.nextToken());
            int r=Integer.parseInt(st.nextToken());
            int s=Integer.parseInt(st.nextToken());
            BigInteger bg=c(p,q).multiply(BigInteger.valueOf(1000*1000));
            BigInteger bg2=c(r,s);
            bg=bg.divide(bg2);
            double ans=bg.doubleValue();
            ans/=1000000;
            String formattedString = String.format("%.5f\n", ans);
            sb.append(formattedString);
        }
        System.out.print(sb);
    }
   
    static BigInteger c(long n, long r) {
        BigInteger ans = BigInteger.ONE;
        r = Math.min(r, n - r);
        long i;
        for (i = 1; i < r+1; i++) {
            ans=ans.multiply(BigInteger.valueOf(n - r + i));
            ans=ans.divide(BigInteger.valueOf(i));
        }
        return ans;
    }
}

Sunday 23 December 2012

UVA - 191 - Intersection

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            double x1=Double.parseDouble(st.nextToken());
            double y1=Double.parseDouble(st.nextToken());
            double x2=Double.parseDouble(st.nextToken());
            double y2=Double.parseDouble(st.nextToken());
            Line line=new Line(x1, y1, x2, y2);
            x1=Double.parseDouble(st.nextToken());
            y1=Double.parseDouble(st.nextToken());
            x2=Double.parseDouble(st.nextToken());
            y2=Double.parseDouble(st.nextToken());
            Rectangle rect=new Rectangle(x1, y1, x2, y2);
            if(intersecRecLine(rect, line)){
                sb.append("T\n");
            }else{
                sb.append("F\n");
            }
        }
        System.out.print(sb);
    }
   
    static boolean intersecRecLine(Rectangle x,Line y){
        if(y.getSlope()==Integer.MAX_VALUE){
           if(y.getX1()<=x.getX2() && y.getX1()>=x.getX1()){
               for(double i=Math.min(y.getY1(),y.getY2());i<=Math.max(y.getY1(),y.getY2());i++){
                   if(i<=x.getY2() && i>=x.getY1()){
                       return true;
                   }
                }
               return false;
            }else{
               return false;
           }
        }else{
            for(double i=Math.min(y.getX1(),y.getX2()),j=0;i<=Math.max(y.getX1(),y.getX2());i++){
                if(i<=x.getX2() && i>=x.getX1()){
                    j=y.func(i);
                    if(j<=x.getY2() && j>=x.getY1()){
                        return true;
                    }
                }
            }
            return false;
        }
    }
  
}class Rectangle{
    double x1;
    double y1;
    double x2;
    double y2;

    public Rectangle(double x1, double y1, double x2, double y2) {
        this.x1 = Math.min(x1, x2);
        this.y1 = Math.min(y1, y2);
        this.x2 = Math.max(x1, x2);
        this.y2 = Math.max(y1, y2);
    }
   
    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }
   
    public double getWidth(){
        return x2-x1;
    }
   
    public double getHeight(){
        return y2-y1;
    }
   
    public double getArea(){
        return getWidth()*getHeight();
    }
   
    public double getPeri(){
        return 2*(getWidth()+getHeight());
    }
}
class Line{
    double x1;
    double y1;
    double x2;
    double y2;
    double slope;
    double c;

    public Line(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        if(x2-x1!=0)
            slope=(double)(this.y2-this.y1)/(this.x2-this.x1);
        else{
            slope=Integer.MAX_VALUE;
        }
        c=this.y2-this.x2*this.slope;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getSlope() {
        return slope;
    }

    public void setSlope(double slope) {
        this.slope = slope;
    }
   
    public double func(double x){
         return (x*slope+c);
    }
}

Saturday 22 December 2012

UVA - 10170 - The Hotel with Infinite Rooms

#include <stdio.h>

long fact(long x,long num,long ans){
    if(x>=ans)
        return num;
    return fact(x+num,++num,ans);
}

int main() {
    long x,y;
    while((scanf("%ld %ld",&x,&y))==2){
        long ans=fact(x,x,y);
        printf("%ld\n",ans);
    }
    return 0;
}

UVA - 11313 - Gourmet Games

#include <stdio.h>

int main() {
    int x,y,i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++) {
        scanf("%d %d",&x,&y);
        if((x-1)%(y-1)!=0){
            puts("cannot do this");
        }else{
            printf("%d\n",(x-1)/(y-1));
        }
    }
    return 0;
}

Thursday 20 December 2012

UVA - 11909 - Soya Milk (C solution)

#include <stdio.h>
#include <math.h>

int main()
{
    int l,w,h,theta;
    double pi=acos(-1);
    while(scanf("%d %d %d %d",&l,&w,&h,&theta)==4){
        double d=l*tan(theta*pi/180.0);
        double ans;
        if(d > h){
            ans=0.5*h*h*l*w/d;
        }else{
            ans=l*w*((h)-(d*0.5));
        }
        printf("%.3lf mL\n", ans);
    }
    return 0;
}

UVA - 11909 - Soya Milk (Java Solution)

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String m="";
        while((m=br.readLine())!=null){
            StringTokenizer st = new StringTokenizer(m);
            int l=Integer.parseInt(st.nextToken());
            int w=Integer.parseInt(st.nextToken());
            int h=Integer.parseInt(st.nextToken());
            int theta=Integer.parseInt(st.nextToken());
            double d=l*Math.tan(theta*Math.PI/180.0);
            double ans=l*w*((h)-(d*0.5));
            if(d > h){
                ans=0.5*h*h*l*w/d;
            }
            System.out.printf("%.3f mL\n", ans);
        }
    }
}

UVA - 10991 - Region (C Solution)

#include <stdio.h>
#include <math.h>

int main()
{
    double r1,r2,r3;
    int cases,i;
    scanf("%d",&cases);
    for(i=0;i<cases;i++){
      scanf("%lf %lf %lf",&r1,&r2,&r3);

      double m1= r2+r3;
      double m2= r1+r3;
      double m3= r1+r2;

      double theta1=acos((m2*m2+m3*m3-m1*m1)/(2*m2*m3));
      double theta2=acos((m1*m1+m3*m3-m2*m2)/(2*m1*m3));
      double theta3=acos((m2*m2+m1*m1-m3*m3)/(2*m2*m1));

      double med=(m1+m2+m3)/2.0;
      double areaOfTri=sqrt(med*(med-m1)*(med-m2)*(med-m3));

      double areaofArc1=0.5*theta1*r1*r1;
      double areaofArc2=0.5*theta2*r2*r2;
      double areaofArc3=0.5*theta3*r3*r3;

      double ans=areaOfTri-areaofArc1-areaofArc2-areaofArc3;
      printf("%.6f\n", ans);
    }
    return 0;
}

UVA - 10991 - Region (Java Solution)

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            double r1 = Double.parseDouble(st.nextToken());
            double r2 = Double.parseDouble(st.nextToken());
            double r3 = Double.parseDouble(st.nextToken());
           
            double m1= r2+r3;
            double m2= r1+r3;
            double m3= r1+r2;
           
            double theta1=Math.acos((m2*m2+m3*m3-m1*m1)/(2*m2*m3));
            double theta2=Math.acos((m1*m1+m3*m3-m2*m2)/(2*m1*m3));
            double theta3=Math.acos((m2*m2+m1*m1-m3*m3)/(2*m2*m1));
           
            double med=(m1+m2+m3)/2.0;
            double areaOfTri=Math.sqrt(med*(med-m1)*(med-m2)*(med-m3));
           
            double areaofArc1=0.5*theta1*r1*r1;
            double areaofArc2=0.5*theta2*r2*r2;
            double areaofArc3=0.5*theta3*r3*r3;
           
            double ans=areaOfTri-areaofArc1-areaofArc2-areaofArc3;
            System.out.printf("%.6f\n", ans);
        }
    }
}

UVA - 10347 - Medians (C solution)

#include <stdio.h>
#include <math.h>

int main()
{
    double m1,m2,m3;
    while(scanf("%lf %lf %lf",&m1,&m2,&m3)==3){
        double med=(m1+m2+m3)/2.0;
        double area=(4/3.0)*sqrt(med*(med-m1)*(med-m2)*(med-m3));
        if(!(area>0)){
            area=-1.0;
        }
        printf("%.3lf\n",area);
        }
    return 0;
}

UVA - 10347 - Medians (Java Solution)


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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String m = "";
        while ((m = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(m);
            double m1 = Double.parseDouble(st.nextToken());
            double m2 = Double.parseDouble(st.nextToken());
            double m3 = Double.parseDouble(st.nextToken());
            double med = (m1 + m2 + m3) / 2.0;
            double area = (4 / 3.0) * Math.sqrt(med * (med - m1) * (med - m2) * (med - m3));
            if (!(area > 0)) {
                area = -1;
            }
            System.out.printf("%.3f\n", area);
        }
    }
}

Wednesday 19 December 2012

UVA - 10226 - Hardwood Species

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int cases=Integer.parseInt(br.readLine());
        br.readLine();
        for(int i=0;i<cases;i++){
            if(i>0)
                System.out.println();
            HashMap<String,Integer> hm=new HashMap<String, Integer>();
            LinkedList<String> list=new LinkedList<String>();
            int counter=0;
            String temp;
            while((temp=br.readLine())!=null){
                temp=temp.trim();
                if(temp.equals("")){
                    break;
                }else{
                   if(hm.containsKey(temp)){
                       int ind=hm.get(temp);
                       hm.put(temp, ind+1);
                   }else{
                       hm.put(temp, 1);
                       list.add(temp);
                   }
                   counter++;
                }
            }
            Collections.sort(list);
            while(!list.isEmpty()){
                String str=list.remove();
                double val=(double)hm.get(str)*100/counter;
                System.out.printf("%s %.4f\n", str,val);
            }
        }
    }
}

Monday 17 December 2012

UVA - 11629 - Ballot evaluation

//Avoid Using Double parsing
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
            StringTokenizer st=new StringTokenizer(br.readLine());
            int n=Integer.parseInt(st.nextToken());
            int m=Integer.parseInt(st.nextToken());
            HashMap<String,Integer> hm=new HashMap<String, Integer>();
            for(int i=0;i<n;i++){
                st=new StringTokenizer(br.readLine());
                hm.put(st.nextToken(), mult(st.nextToken()));
            }
            for(int i=1;i<m+1;i++){
                sb.append("Guess #").append(i).append(" was ");
                st=new StringTokenizer(br.readLine());
                int countofToken=st.countTokens();
                double ans=hm.get(st.nextToken());
                while(st.hasMoreTokens()){
                    String temp=st.nextToken();
                    if(temp.equals("+")){
                        ans+=hm.get(st.nextToken());
                    }
                    if(temp.equals("<")){
                        if(ans<10*Integer.parseInt(st.nextToken())){
                            sb.append("correct.\n");
                        }else{
                            sb.append("incorrect.\n");
                        }
                    }
                    else if(temp.equals(">")){
                        if(ans>10*Integer.parseInt(st.nextToken())){
                            sb.append("correct.\n");
                        }else{
                            sb.append("incorrect.\n");
                        }
                    }
                    else if(temp.equals(">=")){
                        if(ans>=10*Integer.parseInt(st.nextToken())){
                            sb.append("correct.\n");
                        }else{
                            sb.append("incorrect.\n");
                        }
                    }
                    else if(temp.equals("<=")){
                        if(ans<=10*Integer.parseInt(st.nextToken())){
                            sb.append("correct.\n");
                        }else{
                            sb.append("incorrect.\n");
                        }
                    }
                    else if(temp.equals("=")){
                        if(ans==10*Integer.parseInt(st.nextToken())){
                            sb.append("correct.\n");
                        }else{
                            sb.append("incorrect.\n");
                        }
                    }
                }
            }
        System.out.print(sb);
    }
   
    static int mult(String x){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<x.length();i++){
            if(x.charAt(i)>='0' &&x.charAt(i)<='9'){
                sb.append(x.charAt(i));
            }
        }
        return Integer.parseInt(sb.toString());
    }
}

UVA - 11286 - Conformity

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while(true){
            int n=Integer.parseInt(br.readLine());
            if(n==0){
                break;
            }
            HashMap<String,Integer> hm=new HashMap<String, Integer>();
            int max=Integer.MIN_VALUE;
            int counterMax=0;
            for(int i=0;i<n;i++){
                StringTokenizer st=new StringTokenizer(br.readLine());
                int[] arr=new int[5];
                for(int j=0;j<5;j++){
                    arr[j]=Integer.parseInt(st.nextToken());
                }
                Arrays.sort(arr);
                StringBuilder str=new  StringBuilder("");
                for(int j=0;j<5;j++){
                    if(j>0)
                        str.append(" ");
                    str.append(arr[j]);
                }
                String temp=str.toString();
                if(hm.containsKey(temp)){
                    int index=hm.get(temp);
                    hm.put(temp, index+1);
                    if(index+1>max){
                        max=index+1;
                        counterMax=max;
                    }
                    else if(index+1==max){
                        counterMax+=max;
                    }
                }else{
                    hm.put(temp, 1);
                    if(1>max){
                        max=1;
                        counterMax=1;
                    }
                    else if(max==1){
                        counterMax++;
                    }
                }
            }
            sb.append(counterMax).append("\n");
        }
        System.out.print(sb);
    }
}

Sunday 16 December 2012

UVA - 11849 - CD

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while(true){
            StringTokenizer st=new StringTokenizer(br.readLine());
            int n=Integer.parseInt(st.nextToken());
            int m=Integer.parseInt(st.nextToken());
            if(n==0 &&m==0){
                break;
            }
            HashSet<Integer> hs=new HashSet<Integer>();
            for(int i=0;i<n;i++){
                hs.add(Integer.parseInt(br.readLine()));
            }
            int counter=0;
            for(int i=0;i<m;i++){
                if(hs.contains(Integer.parseInt(br.readLine()))){
                    counter++;
                }
            }
            sb.append(counter).append("\n");
        }
        System.out.print(sb);
    }
}

UVA - 11526 - H(n) (C solution)

#include <stdio.h>
#include <math.h>

long H(int x){
    if(x<1)
        return 0;
    long sum = 0;
    int sqr = floor(sqrt(x)),i = 1, end = x,temp;
    for (; i < sqr + 1; i++) {
        temp = x / i;
        sum += temp + (end - temp) * (i - 1);
        end = temp;
    }
    if (x / sqr > sqr) {
        sum += sqr;
    }
    return sum;
}

int main(){
    int n,x;
    scanf("%d", &n);
    while(n--){
        scanf("%d",&x);
        printf("%ld\n", H(x));
    }
    return 0;
}

UVA - 11526 - H(n) (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            int x=Integer.parseInt(br.readLine());
            sb.append(H(x)).append("\n");
        }
        System.out.print(sb);
    }
   
static long H(int x){
    if (x<1)
        return 0;
    long sum = 0;
    int sqr = (int) (Math.sqrt(x));
    for (int i = 1, end = x; i < sqr + 1; i++) {
        int temp = x / i;
        sum += temp + (end - temp) * (i - 1);
        end = temp;
    }
    if (x / sqr > sqr) {
        sum += sqr;
    }
    return sum;
    }
}

UVA - 725 - Division

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int[]idnum=new int[30240];
        for(int i=98765,j=0;i>1233;i--){
            boolean[]arr=new boolean[10];
            if(i<10000)
                arr[0]=true;
            int temp=i;
            boolean flag=true;
            while(temp>0){
                int index=temp%10;
                if(arr[index]){
                    flag=false;
                    break;
                }
                arr[index]=true;
                temp/=10;
            }
            if(flag){
               idnum[j]=i;
               j++;
            }
        }
        LinkedList<String>[] list=new LinkedList[80];
        for(int i=0;i<80;i++){
            list[i]=new LinkedList<String>();
        }
        Arrays.sort(idnum);
        for(int i=3024;i<idnum.length;i++){
            int tempX=idnum[i];
           for(int j=0;j<i;j++){
               int tempY=idnum[j];
               if(idnum[i]%idnum[j]==0 && checkIdent(idnum[i],idnum[j])){
                   int div=idnum[i]/idnum[j];
                   if(div>79){
                       break;
                   }else{
                       if(idnum[j]<10000){
                           list[div].add(idnum[i]+" / 0"+idnum[j]);
                       }else{
                           list[div].add(idnum[i]+" / "+idnum[j]);
                       }
                   }
               }
            }
        }
        boolean first =true;
        while(true){
            int x=Integer.parseInt(br.readLine());
            if(x==0){
                break;
            }
            if(!first){
                sb.append("\n");
            }
            first=false;
            if(!list[x].isEmpty()){
                for(int i=0;i<list[x].size();i++){
                    sb.append(list[x].get(i)).append(" = ").append(x).append("\n");
                }
            }else{
                sb.append("There are no solutions for ").append(x).append(".\n");
            }
        }
        System.out.print(sb);
    }
   
    static boolean checkIdent(int x,int y){
        boolean[] arr=new boolean[10];
        if(x<10000)
            arr[0]=true;
        int temp=x;
        while(temp>0){
            int rem=temp%10;
            arr[rem]=true;
            temp/=10;
        }
        temp=y;
        if(y<10000){
            if(arr[0])
               return false;
        }
        while(temp>0){
            int rem=temp%10;
            if(arr[rem]){
                return false;
            }
            temp/=10;
        }
        return true;
    }
}

UVA - 12554 - A Special "Happy Birthday" Song!!!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String []happy={"Happy","birthday","to","you","Happy","birthday","to","you"
        ,"Happy","birthday","to","Rujia","Happy","birthday","to","you"};
        int cases=Integer.parseInt(br.readLine());
        Queue<String> qu=new LinkedList();
        for(int i=0;i<cases;i++){
           qu.add(br.readLine());
        }
        while (cases>0){
            for(int i=0;i<happy.length;i++){
                String temp=qu.remove();
                sb.append(temp).append(": ").append(happy[i]).append("\n");
                qu.add(temp);
            }
            cases-=happy.length;
        }
        System.out.print(sb);
    }
   
}

Saturday 15 December 2012

UVA - 11349 - Symmetric Matrix

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            String[]str=br.readLine().split("=");
            int n=Integer.parseInt(str[1].trim());
            long [][]arr=new long[n][n];
            for(int j=0;j<n;j++){
                String[] temp=br.readLine().split(" ");
              for(int z=0;z<n;z++){
                 arr[j][z]=Long.parseLong(temp[z]);
              } 
            }
            long[][] temp=rotate180(arr);
            sb.append("Test #").append((i+1)).append(": ");
            if(check(arr, temp)){
                sb.append("Symmetric.\n");
            }else{
                sb.append("Non-symmetric.\n");
            }
           }
        System.out.print(sb);
    }
   
   
   static public long[][] rotate180(long[][] arr){
        int x=arr.length,y=arr[0].length;
        long[][]temp=new long[x][y];
        for(int i=0;i<x;i++){
          for(int j=0;j<y;j++){
              temp[i][j]=arr[x-i-1][y-j-1];
            } 
        }
        return temp;
    }
  
   static boolean check(long[][]x,long[][]y){
       for(int i=0;i<x.length;i++){
           for(int j=0;j<y.length;j++){
               if(x[i][j]!=y[i][j] ||x[i][j]<0||y[i][j]<0){
                   return false;
               }
           }
       }
       return true;
   }
}

UVA - 10855 - Rotated square

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while(true){
            String[]str=br.readLine().split(" ");
            int n=Integer.parseInt(str[0]);
            int m=Integer.parseInt(str[1]);
            if(n==0 && m==0){
                break;
            }
            char [][]arr=new char[n][n];
            for(int i=0;i<n;i++){
                String temp=br.readLine();
              for(int j=0;j<n;j++){
                 arr[i][j]=temp.charAt(j);
              } 
            }
            char [][]arr2=new char[m][m];
            for(int i=0;i<m;i++){
                String temp=br.readLine();
              for(int j=0;j<m;j++){
                 arr2[i][j]=temp.charAt(j);
              } 
            }
            int[]ans=new int[4];
            ans[0]=search(arr, arr2);
            ans[1]=search(arr, rotate90(arr2));
            ans[2]=search(arr, rotate180(arr2));
            ans[3]=search(arr, rotate270(arr2));
            sb.append(ans[0]).append(" ").append(ans[1]).append(" ").append(ans[2]).append(" ").append(ans[3]).append("\n");
        }
        System.out.print(sb);
    }
   
    static public char[][] rotate90(char[][] arr){
        int x=arr.length,y=arr[0].length;
        char[][]temp=new char[x][y];
        for(int i=0;i<x;i++){
          for(int j=0;j<y;j++){
              temp[i][j]=arr[y-j-1][i];
            } 
        }
        return temp;
    }
   
   static public char[][] rotate180(char[][] arr){
        int x=arr.length,y=arr[0].length;
        char[][]temp=new char[x][y];
        for(int i=0;i<x;i++){
          for(int j=0;j<y;j++){
              temp[i][j]=arr[x-i-1][y-j-1];
            } 
        }
        return temp;
    }
  
    static public char[][] rotate270(char[][] arr){
        int x=arr.length,y=arr[0].length;
        char[][]temp=new char[x][y];
        for(int i=0;i<x;i++){
          for(int j=0;j<y;j++){
              temp[i][j]=arr[j][x-i-1];
            } 
        }
        return temp;
    }
   
    static int search(char[][]big,char[][]small){
        int counter=0;
        for(int l=0;l<big.length-small.length+1;l++){
            for(int z=0;z<big[0].length-small[0].length+1;z++){
                boolean flag=true;
                for(int i=z, x=0;x<small.length;i++,x++){
                   for(int j=l,y=0;y<small[0].length;j++,y++){
                       if(big[i][j]!=small[x][y]){
                           flag=false;
                           break;
                       }
                    }
                   if(!flag){
                       break;
                   }
                }
                if(flag){
                    counter++;
                }
            }
        }
        return counter;
    }
}