Showing posts with label Ad Hoc. Show all posts
Showing posts with label Ad Hoc. Show all posts

Wednesday, May 3, 2017

URI Solution 1593 Binary Function - Solution in java | Ad Hoc

URI Solution 1593 Binary Function - Solution in java | Ad Hoc


URI Online Judge Solution  1593 Binary Function| Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1593

Problem Name: URI Problem 1593 Binary Function
Problem Number : URI Problem 1593 Binary Function Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : java

URI Solution 1593 Binary Function - Solution in java | Ad Hoc

URI Solution 1593 Binary Function Code in java:


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

public class Main {

 public static int countOccurrences(String haystack, char needle) {
  int count = 0;
  
  for (int i=0; i < haystack.length(); i++) {
   if (haystack.charAt(i) == needle){
     count++;
   }
  }
  
  return count;
 }
 
 public static void main(String[] args) throws IOException {
  
  InputStreamReader ir = new InputStreamReader(System.in);
  BufferedReader in = new BufferedReader(ir);
  
  int t, res;
  String bin;

  t = Integer.parseInt(in.readLine());

  for (int i = 0; i < t; ++i) {
   BigInteger bg = new BigInteger(in.readLine());
   bin = bg.toString(2);
   res = countOccurrences(bin, '1');
   System.out.println(res);
  }
 }
 
}
Read More

URI Solution 1588 Help the Federation - Solution in C++ | Ad Hoc

URI Solution 1588 Help the Federation - Solution in C++ | Ad Hoc


URI Online Judge Solution  1588 Help the Federation| Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1588

Problem Name: URI Problem 1588 Help the Federation
Problem Number : URI Problem 1588 Help the Federation Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1588 Help the Federation - Solution in C++ | Ad Hoc   URI Online Judge Solution  1588 Help the Federation| Ad Hoc URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1588  Problem Name: URI Problem 1588 Help the Federation Problem Number : URI Problem 1588 Help the Federation Solution Online Judge : URI Online Judge Solution Level: Ad Hoc Solution Language : C plus plus  URI Solution 1588 Help the Federation Code in CPP:

URI Solution 1588 Help the Federation Code in CPP:


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
 
#define SC1(a) scanf("%d", &a)
#define SC2(a, b) scanf("%d %d", &a, &b)
#define SC4(a, b, c, d) scanf("%d %s %d %s", &a, &b, &c, &d)
#define ZERO(x) memset(x, 0, sizeof(x))
#define FOR(i, n) for(int i = 0; i < (n); ++i)
 
using namespace std;
 
typedef struct
{
 char nome[21];
 int id, pon, vit, gol;
}times;
 
times arr[101];
 
int findv(int range, char * elmt)
{
 for (int i = 0; i < range; ++i)
  if(strcmp(arr[i].nome, elmt) == 0)
   return i;
 
 return -1;
}
 
bool cmp(times a, times b)
{
 if(a.pon > b.pon)
  return true;
 else if(a.pon < b.pon)
  return false;

 if(a.vit > b.vit)
  return true;
 else if(a.vit < b.vit)
  return false;

 if(a.gol > b.gol)
  return true;
 else if(a.gol < b.gol)
  return false;

 return a.id < b.id;
}
 
int main(int argc, char const *argv[])
{
 int t, n, m, x, y, pa, pb;
 char timeA[21], timeB[21];
 
 SC1(t);
 
 while(t--)
 {
  SC2(n, m);
  getchar();
  ZERO(arr);
 
  FOR(i, n)
  {
   gets(arr[i].nome);
   arr[i].id = i;
  }

  FOR(i, m)
  {
   cin >> x >> timeA >> y >> timeB;
    
   pa = findv(n, timeA);
   pb = findv(n, timeB);
    
   if(x > y){
    arr[pa].gol += x;
    arr[pb].gol += y;
    arr[pa].pon += 3;
    arr[pa].vit++;
   }else if(x < y){
    arr[pa].gol += x;
    arr[pb].gol += y;
    arr[pb].pon += 3;
    arr[pb].vit++;
   }else{
    arr[pa].gol += x;
    arr[pb].gol += y;
    arr[pa].pon += 1;
    arr[pb].pon += 1;
   }
  }
 
  stable_sort(arr, arr + n, cmp);
 
  FOR(i, n)
   printf("%s\n", arr[i].nome);
 }
 
 return 0;
}
Read More

URI Solution 1585 Making Kites - Solution in C, C++ | Ad Hoc

URI Solution 1585 Making Kites - Solution in C, C++ | Ad Hoc

URI Online Judge Solution  1585 Making Kites | Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1585

Problem Name: URI Problem 1585 Making Kites
Problem Number : URI Problem 1585 Making KitesSolution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1585 Making Kites Code in CPP:

URI 1585 Making Kites Solution in C:


#include <stdio.h>
 
int main() {
 int i = 0, n, x, y;
 scanf("%i", &n);
 for (i = 0; i < n; ++i)
 {
  scanf("%i %i", &x, &y);  
  printf("%d cm2\n", (x * y)/2);
 }

    return 0;
}


URI 1585 Making Kites Solution in C:


#include <cstdio>
using namespace std;

int main()
{
 int n, x, y;
 scanf("%i", &n);
 for (int i = 0; i < n; ++i)
 {
  scanf("%i %i", &x, &y);  
  printf("%d cm2\n", (x * y)/2);
 }
 return 0;
}

Read More

URI Solution - 1583 Contamination - Solution in C, C++ | Ad Hoc

URI Solution - 1583 Contamination - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  1583 Contamination| Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1583

Problem Name: URI Problem 1583 Contamination
Problem Number : URI Problem 1583 Contamination Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1583 Contamination Code in CPP:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define sc1(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d %d", &a, &b)
#define for(i,a,n) for(int (i) = (a); (i) < (n); (i)++)

int n, m, sz;
struct toV { int i, j; };
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

char map[64][64];
bool visi[64][64];
toV run[1000001];

void flood(int a, int b)
{
 if(visi[a][b])
  return;

 int na, nb;
 visi[a][b] = true;
 map[a][b] = 'T';

 for(i,0,4)
 {
  na = a + dir[i][0], nb = b + dir[i][1];
  if((na >= 0 && na < n) && (nb >= 0 && nb < m))
   if(map[na][nb] == 'T' || map[na][nb] == 'A')
    flood(na, nb);
 }
}

int main(void)
{
 while(sc2(n,m) && (n || m))
 {
  sz = 0;
  getchar();
  memset(visi, 0, sizeof visi);

  for(i,0,n)
  {
   for(j,0,m)
   {
    cin >> map[i][j];
    if(map[i][j] == 'T'){
     run[sz].i = i, run[sz].j = j;
     sz++;
    }
   }
   getchar();
  }

  for(i,0,sz)
   flood(run[i].i, run[i].j);    

  for(i,0,n)
  {
   for(j,0,m)
    printf("%c", map[i][j]);
   printf("\n");
  }

  printf("\n");
 }

 return 0;
}

Read More

URI Solution 1578 Matrix of Squares - Solution in C, C++ | Ad Hoc

URI Solution 1578 Matrix of Squares - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  1578 Matrix of Squares| Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1578

Problem Name: URI Problem 1578 Matrix of Squares
Problem Number : URI Problem 1578 Matrix of Squares Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1578 Matrix of Squares Code in CPP:



#include <cstdio>
using namespace std;

unsigned long long len (unsigned long long value)
{
   int l = 1;
   while(value > 9)
   {
    l++;
    value /= 10;
   }

   return l;
}

int main()
{
 int n, m, counter = 4, i, j, k;
 unsigned long long x, max, size, dif, sub;
 scanf("%i", &n);

 while(n--)
 {
  scanf("%i", &m);
  unsigned long long matriz[m][m], coluna[m];
  max = 0;

  for (i = 0; i < m; ++i)
  {
   for (j = 0; j < m; ++j)
   {
    scanf("%llu", &x);
    x *= x;
    matriz[i][j] = x;
   }
  }

  for (j = 0; j < m; ++j)
  {
   for (i = 0; i < m; ++i)
   {
    size = len(matriz[i][j]);
    if(max < size)
     max = size;
   }
   coluna[j] = max;
   max = 0;
  }

  printf("Quadrado da matriz #%i:\n", counter);

  for (i = 0; i < m; ++i)
  {
   for (j = 0; j < m; ++j)
   {
    sub = len(matriz[i][j]);
    dif = coluna[j] - sub;

    for (k = 0; k < dif; ++k)
     printf(" ");

    printf("%llu", matriz[i][j]);

    if(j != (m - 1))
     printf(" ");
   }
   printf("\n");
  }

  counter++;
  if(n >= 1)
   printf("\n");
 }

 return 0;
}
Read More

URI Solution 1574 Robot Instructions - Solution in C, C++ | Ad Hoc

URI Solution 1574 Robot Instructions - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  | Ad Hoc
URI Main Problem Link -

Problem Name: URI Problem 1574 Robot Instructions
Problem Number : URI Problem 1574 Robot Instructions  Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1574 Robot Instructions Code in CPP:


#include <iostream>
#include <string>
using namespace std;

int main()
{
 int t, n, p, val;
 string line, sub;
 cin >> t;

 for (int i = 0; i < t; ++i)
 {
  cin >> n;
  int array[n + 1];
  p = 0;
  cin.ignore();

  for (int j = 0; j < n; ++j)
  {
   getline(cin, line);

   if(line == "LEFT"){
    array[j + 1] = -1;
   }else if(line == "RIGHT"){
    array[j + 1] = 1;
   }else{
    sub = line.substr(8);
    val = stoi( sub );
    array[j + 1] = array[val];
   }
  }

  for (int j = 1; j < (n + 1); ++j)
   p += array[j]; 

  cout << p << '\n';
 }
 return 0;
}
Read More

URI Solution 1573 Chocolate Factory - Solution in C, C++ | Ad Hoc

URI Solution 1573 Chocolate Factory - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  | Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1573

Problem Name: URI Problem 1573 Chocolate Factory
Problem Number : URI Problem 1573 Chocolate Factory Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1573 Chocolate Factory Code in CPP:


#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
 int a, b, c, v, x;

 while(scanf("%i %i %i", &a, &b, &c) && (a || b || c))
 {
  v = a * b * c;
  x = (int) cbrt(v);
  printf("%i\n", x);
 }

 return 0;
}
Read More

URI Solution 1561 Binary Watch - Solution in C, C++ | Ad Hoc

URI Solution 1561 Binary Watch - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  1561 Binary Watch | Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1561

Problem Name: URI Problem 1561 Binary Watch
Problem Number : URI Problem 1561 Binary Watch Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1561 Binary Watch Code in CPP:



#include <cstdio>

using namespace std;

int h8, h4, h2, h1, m32, m16, m8, m4, m2, m1;

void printWatch(int h, int m)
{
 h8 = h4 = h2 = h1 = m32 = m16 = m8 = m4 = m2 = m1 = 0;

 h8 = h / 8; h %= 8;
 h4 = h / 4; h %= 4;
 h2 = h / 2; h %= 2;

 m32 = m / 32; m %= 32;
 m16 = m / 16; m %= 16;
 m8 = m / 8; m %= 8;
 m4 = m / 4; m %= 4;
 m2 = m / 2; m %= 2;


 printf(" ____________________________________________\n");
 printf("|                                            |\n");
 printf("|    ____________________________________    |_\n");
 printf("|   |                                    |   |_)\n");
 printf("|   |   8         4         2         1  |   |\n");
 printf("|   |                                    |   |\n");
 
 printf("|   |   ");
 if(h8 == 1) printf("o         ");
 else printf("          ");
 if(h4 == 1) printf("o         ");
 else printf("          ");
 if(h2 == 1) printf("o         ");
 else printf("          ");
 if(h == 1) printf("o  |   |\n");
 else printf("   |   |\n");

 printf("|   |                                    |   |\n"); 
 printf("|   |                                    |   |\n");
 
 printf("|   |   ");
 if(m32 == 1) printf("o     ");
 else printf("      ");
 if(m16 == 1) printf("o     ");
 else printf("      ");
 if(m8 == 1) printf("o     ");
 else printf("      ");
 if(m4 == 1) printf("o     ");
 else printf("      ");
 if(m2 == 1) printf("o     ");
 else printf("      ");
 if(m == 1) printf("o  |   |\n");
 else printf("   |   |\n");

 printf("|   |                                    |   |\n");
 printf("|   |   32    16    8     4     2     1  |   |_\n");
 printf("|   |____________________________________|   |_)\n");
 printf("|                                            |\n");
 printf("|____________________________________________|\n");
 printf("\n");
}

int main(int argc, char const *argv[])
{
 int h, m;
 char w[5];

 while(scanf("%s", &w) == 1)
 {
  h = ((w[0] - '0') * 10) + (w[1] - '0');
  m = ((w[3] - '0') * 10) + (w[4] - '0');
  printWatch(h, m);
 }

 return 0;
}

Read More

URI Solution 1553 Frequent Asked Questions - Solution in C, C++ | Ad Hoc

URI Solution 1553 Frequent Asked Questions - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  | Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1553

Problem Name: URI Problem 1553 Frequent Asked Questions
Problem Number : URI Problem 1553 Frequent Asked Questions Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1553 Frequent Asked Questions Code in CPP:


#include <iostream>
using namespace std;

int main()
{
 int n, q, s, x;
 int ctr = 0, ctr_f, tmp, res = 10000000;
 
 cin >> n;
 
 for(int i = 0; i < n; ++i)
 {
  cin >> q >> s;
  res = 10000000;
  ctr_f = 0;
  ctr = 0;
  
  for(int j = 0; j < q; ++j)
  {
   cin >> x;
   ctr++;
   
   tmp = s - x;
   if(tmp < 0)
    tmp = -tmp;
    
   if(tmp < res){
    res = tmp;
    ctr_f = ctr;
   }   
  }
  
  cout << ctr_f << endl;
  
 }
 
 return 0;
}
Read More

URI Solution 1547 Guess What - Solution in C, C++ | Ad Hoc

URI Solution 1547 Guess What - Solution in C, C++ | Ad Hoc


URI Online Judge Solution  | Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1547

Problem Name: URI Problem 1547 Guess What Guess What
Problem Number : URI Problem 1547 Guess What  Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus

URI Solution 1547 Guess What Code in CPP:


#include <iostream>
using namespace std;

int main()
{
 int n, q, s, x;
 int ctr = 0, ctr_f, tmp, res = 10000000;
 
 cin >> n;
 
 for(int i = 0; i < n; ++i)
 {
  cin >> q >> s;
  res = 10000000;
  ctr_f = 0;
  ctr = 0;
  
  for(int j = 0; j < q; ++j)
  {
   cin >> x;
   ctr++;
   
   tmp = s - x;
   if(tmp < 0)
    tmp = -tmp;
    
   if(tmp < res){
    res = tmp;
    ctr_f = ctr;
   }   
  }
  
  cout << ctr_f << endl;
  
 }
 
 return 0;
}
Read More

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.