Pages

Saturday, 17 November 2012

UVA - 541 - Error Correction

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(true){
           int n=Integer.parseInt(br.readLine());
           if(n==0) {
               break;
           }
           int[] cols=new int[n];
           int[] rows=new int[n];
           for(int i=0;i<n;i++){
              String[] str=br.readLine().split(" ");
              for(int j=0;j<n;j++){
                  if("1".equals(str[j])){
                      cols[j]++;
                      rows[i]++;
                  }
              }
           }
           int countRIm=0,tempR=0;
           int countCIm=0,tempC=0;
           for(int i=0;i<n;i++){
               if(cols[i]%2==1) {
                   countCIm++;
                   tempC=i+1;
               }
               if(rows[i]%2==1) {
                   countRIm++;
                   tempR=i+1;
               }
           }
          
           if(countCIm>1 ||countRIm>1){
               sb.append("Corrupt\n");
           }else if(countCIm==1 &&countRIm==0){
               sb.append("Corrupt\n");
           }else if(countCIm==0 &&countRIm==1){
               sb.append("Corrupt\n");
           }
           else if(countCIm==1 &&countRIm==1){
               sb.append("Change bit (").append(tempR).append(",").append(tempC).append(")\n");
           }else{
               sb.append("OK\n");
           }
       }
       System.out.print(sb);
    }
}

Thursday, 15 November 2012

UVA - 11203 - Can you decide it for ME?

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=1;i<cases+1;i++) {
            if(ax(br.readLine().trim())){
                sb.append("theorem\n");
            }else{
                sb.append("no-theorem\n");
            }
        }
        System.out.print(sb);
    }
   
    static boolean ax(String x) {
        boolean flagM = false, flagE = false;
        for (int i = 0; i < x.length(); i++) {
            if (x.charAt(i) == '?' || x.charAt(i) == 'M' || x.charAt(i) == 'E') {
                if (x.charAt(i) == 'M') {
                    if (flagE) {
                        return false;
                    }
                    if (flagM) {
                        return false;
                    }
                    flagM = true;
                }
                if (x.charAt(i) == 'E') {
                    if (flagE) {
                        return false;
                    }
                    flagE = true;
                }
            }else{
                return false;
            }
        }
        int count[] = new int[3];
        for (int i = 0, ind = 0; i < x.length(); i++) {
            if (x.charAt(i) == 'M' || x.charAt(i) == 'E') {
                ind++;
                continue;
            }
            count[ind]++;
        }
        if(count[0]==0 ||count[1]==0)
            return false;
        return (count[2] == count[1] + count[0]);
    }
}

UVA - 11202 - The least possible effort

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

int main()
{
    int x,y,temp,cases;
    scanf("%d",&cases);
    while(cases--)
    {
       scanf("%d %d",&x,&y);
       if(x==y){
           temp=ceil(x/2.0);
           printf("%d\n",(temp*(temp+1)/2));
       }
       else{
           printf("%d\n",(int)(ceil(x/2.0)*ceil(y/2.0)));
       }
    }

       return 0;
}

UVA - 10693 - Traffic Volume

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

int main()
{
    double vit,volume;
    int l,f;
    while(1)
    {
       scanf("%d %d",&l,&f);
       if(l==0&& f==0)
            break;
       vit=sqrt(l*f*2.0);
       volume=(vit*3600.0)/(2.0*l);
       printf("%0.8lf %0.8lf\n",vit,volume);
       }

       return 0;
}

Wednesday, 14 November 2012

UVA - 11121 - Base -2

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=1;i<cases+1;i++) {
            int x=Integer.parseInt(br.readLine());
            sb.append("Case #").append(i).append(": ").append(binary(x)).append("\n");
        }
        System.out.print(sb);
    }
   
static String binary(long n) {
    StringBuilder temp=new StringBuilder();
        while(true){
            if(n==0){
                temp.append("0");
                break;
            }
            if(n==1){
                temp.append("1");
                break;
            }
            if(n==-1){
                temp.append("11");
                break;
            }
            if(n%2==0){
               n/=-2;
               temp.append("0");
            }else{
               n--;
               n/=-2;
               temp.append("1");
            }
        }
        return temp.reverse().toString();
    }

}

UVA - 498 - Polly the Polynomial

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("");
        String m="";
        while ((m=br.readLine())!=null) {
            StringTokenizer st=new StringTokenizer(m);
            int []arr=new int[st.countTokens()];
            for(int i=arr.length-1;i>-1;i--){
                arr[i]=Integer.parseInt(st.nextToken());
            }
           
            st=new StringTokenizer(br.readLine());
            int val;
            int counterTok=st.countTokens();
            for(int i=0;i<counterTok;i++){
                if(i>0)
                    sb.append(" ");
                val=Integer.parseInt(st.nextToken());
                long sum=arr[0];
                long temp=1;
                for(int j=1;j<arr.length;j++){
                    temp*=val;
                    sum+=arr[j]*temp;
                }
                sb.append(sum);
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

Tuesday, 13 November 2012

UVA - 10006 - Carmichael Numbers

#include <stdio.h>

int main()
{   int x,flag,i;
    int arr[]={561, 1105, 1729, 2465, 2821,
     6601, 8911, 10585, 15841, 29341,
      41041, 46657, 52633, 62745, 63973};
    while(1){
        flag=0;
        scanf("%d",&x);
        if(x==0)
            break;
        for(i=0;i<15;i++){
            if(x==arr[i]){
                flag=1;
                break;
            }
        }
        if(flag)
            printf("The number %d is a Carmichael number.\n",x);
        else
            printf("%d is normal.\n",x);
    }

       return 0;
}