Saturday, December 28, 2013

1011 Sphere

URI Online Judge | 1011

Sphere

Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Make a program that calculates and print the volume of a sphere given the radius (R) of the circle. The formula to calculate the volume is: 4/3 * pi * R3. For the purposes of this problem the value of pi is 3.14159.

Input

The input file contain an integer number.

Output

The output file will contain a message like the following example. Remember the space before and after the equal signal. The value must be printed with 3 digits after the decimal point.
Sample Input Sample Output
3
VOLUME = 113.097
 
Solution:
#include <stdio.h>
int main(){
    register int R;
    scanf("%d", &R);
    printf("VOLUME = %.3f\n", 4.188786*R*R*R);
    return 0;
}

1005 Average 1


URI Online Judge | 1005

Average 1

Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Make a simple program that read two floating numbers corresponding to two tests for a student. After, calculate the average between them, considering that the first number has 3.5 weight and the second one have 7.5 weight. Each number can be from zero to ten, always with one digit after the decimal point. Don’t forget to print end line after the result otherwise you will get “Presentation Error”. Don’t forget the space before and after the equal sign.

Input

The input file will contain 2 floating-point numbers with one digit after the decimal point.

Output

Print MEDIA(average in portuguese) according to the following example, with 5 digits after the decimal point and a blank space before and after the equal signal.
Sample Input Sample Output
5.0
7.1
MEDIA = 6.43182

Solution:
#include <stdio.h>
int main(){
    float x,y;
    scanf("%f %f", &x, &y);
    printf("MEDIA = %.5f\n", (x*3.5+y*7.5)/(3.5+7.5));
    return 0;
}

Tuesday, December 24, 2013

Advanced Sorting Algorithm

#include <stdio.h> 
void sort(int a[], int n) {
   int i, j, t, p;
   re: if(n < 32) {
      for(i = 1; i < n; i++){
         for(t = a[i], j = i - 1; j >= 0 && a[j] > t; j--)
            a[j + 1] = a[j];
         a[j + 1] = t;
      }
      return;
   }
   p = a[(n + 1) >> 1];
   for(i = -1, j = n;;) {
      while(a[++i] < p);
      while(a[--j] > p);
      if(i >= j) break;   
      t = a[i];
      a[i] = a[j];
      a[j] = t;
   }
   if((n - i) > 1)
      sort(a + i, n - i);
   n = i;
   goto re;
}

int main() {
   int a[512], n = 512;
         
   sort(a, n); 
   return 0;
}

Monday, December 23, 2013

Advanced Seive Algorithm

#include <stdio.h>
#include <string.h>

static char sieve[131072];
#define PRIMEP(n) (((sieve[(n) >> 3] >> ((n) & 7)) & 1) == 0)

static void mksieve() {
    register int i, j;
    memset(sieve, 0x55, sizeof(sieve));
    for(sieve[0] = 0x53, i = 3; i < 1024; i += 2)
        if(PRIMEP(i))
            for(j = i * i; j < 1048576; j += i)
                 sieve[j >> 3] |= 1 << (j & 7);               
       
}

int main() {
    mksieve();

    int k;
    for(k = 2; k <= 100; k++)
       if(PRIMEP(k))
          printf("%d ", k);
    return 0;
}

Seive Algorithm

#include <stdio.h>
#include <math.h>
#define N 1000000

static char P[N];

int main() {
    int  n, s, t, SQR = sqrt(N);                    
 
    P[0] = P[1] = 0
    for(n = 2; n <= N; n++)      
       P[n] = 1;

    for(n = 2; n <= SQR; n++)       
       if(P[n])                    
          for(s = 2, t = N/n; s <= t; s++) 
             P[s*n] = 0;

    for(n = 2, t = 0; n <= 100; n++)
       if(P[n])
          printf("%d ", n);

    return 0;
}

113 - Power of Cryptography



 Power of Cryptography 

Background

Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers modulo functions of these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be of only theoretical interest.
This problem involves the efficient computation of integer roots of numbers.

The Problem

Given an integer tex2html_wrap_inline32 and an integer tex2html_wrap_inline34 you are to write a program that determines tex2html_wrap_inline36 , the positive tex2html_wrap_inline38 root of p. In this problem, given such integers n and p, p will always be of the form tex2html_wrap_inline48 for an integer k (this integer is what your program must find).

The Input

The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs tex2html_wrap_inline56 , tex2html_wrap_inline58 and there exists an integer k, tex2html_wrap_inline62 such that tex2html_wrap_inline64 .

The Output

For each integer pair n and p the value tex2html_wrap_inline36 should be printed, i.e., the number k such that tex2html_wrap_inline64 .

Sample Input

2
16
3
27
7
4357186184021382204544

Sample Output

4
3
1234
 
Solution: 
#include <stdio.h>
#include <math.h>
int main() {
    double n, P;
    while(scanf("%lf %lf", &n, &P) == 2)
        printf("%.lf\n", pow(P, 1/n));
    return 0;
}

11547 - Automatic Answer

Problem A

AUTOMATIC ANSWER

Last month Alice nonchalantly entered her name in a draw for a Tapmaster 4000. Upon checking her mail today, she found a letter that read:
“Congratulations, Alice! You have won a Tapmaster 4000. To claim your prize, you must answer the following skill testing question.”
Alice’s initial feelings of surprised joy turned quickly to those of dismay. Her lifetime record for skill testing questions is an abysmal 3 right and 42 wrong.
Mad Skills, the leading skill testing question development company, was hired to provide skill testing questions for this particular Tapmaster 4000 draw. They decided to create a different skill testing question to each winner so that the winners could not collaborate to answer the question.
Can you help Alice win the Tapmaster 4000 by solving the skill testing question?
Program Input
The input begins with t (1 ≤ t ≤ 100), the number of test cases. Each test case contains an integer n (-1000 ≤ n ≤ 1000) on a line by itself. This n should be substituted into the skill testing question below.
Program Output
For each test case, output the answer to the following skill testing question on a line by itself: “Multiply n by 567, then divide the result by 9, then add 7492, then multiply by 235, then divide by 47, then subtract 498. What is the digit in the tens column?”
Sample Input & Output
INPUT
2
637
-120
OUTPUT
1
3

Calgary Collegiate Programming Contest 2008
Solution:  
#include <stdio.h>
int main() {
    int n, T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        n = (n*63+7492)*5-498;
        printf("%d\n", n < 0 ? (-n/10)%10 : (n/10)%10);
    }   
    return 0;
}