Pages

Tuesday, November 30, 2010

UVA Problem#10424

Question:
http://uva.onlinejudge.org/external/104/10424.html

Solution:
1. Take 2 strings input. adjust the values of each character according to the problem describes. (here it was stored in ans1 and ans2 variables)
2. Then make the sum of these numbers until it comes in one digit. (here it is done in temp1 and temp2 variables)
3. Answer the value of the two ratio in percentage for the larger value of temp1 and temp2.

UVA Problem#10347

Question:

Solution:
It's a bit tricky to derive the formula for this problem. Basically, you can prove that the area of the triangle formed by the medians is 3/4 of the area of the original triangle. Then, you can use Heron's formula to get the area of the median triangle, and multiply by 4/3. So in all:

A = (4/3) * sqrt(s(s-m1)(s-m2)(s-m3))

Where m1, m2, and m3 are the median lengths, and s is the semiperimeter of the median triangle:

s = (m1 + m2 + m3) / 2

There are some tricky things to watch out for:

UVA Problem#11727

Question:

Solution:
1. Take an integer input indicating the number of test case. (say t)
2. Take 3 integers input in an array and sort them using the built in function sort().
3. Print the 2nd value from the array.

UVA Problem#10346

Question:
http://uva.onlinejudge.org/external/103/10346.html 

Solution:

Find the pattern and derive the following formula (total_cigar and butt initally set to 0) and simply simulate the process...

while (n > 0) {
  total_cigar += n; /* accumulate all new cigars so far */
  butt += n; /* after Peter smokes these n cigar, we have n butts */
  n = butt / k; /* so these n butts become new cigars */
  butt %= k; /* butts left are reserved for future cigars */
}

Source: http://www.comp.nus.edu.sg/~stevenha/programming/acmoj.html

int main(){
....
....
sum = n, x = n;
        while(x >= k)
            sum += (x / k), x = (x / k) + (x % k);
....
.....
}

UVA Problem#11764

Question:

Solution:
1. Take an integer input indating the test cases (say t). For each case take another integer input indicating the number of pillars (say n). Then take input of those pillars in an array (say a[ ] )
2. Just check for two conditions, whether a[j+1] > a[ j] or a[ j+1] < a [ j]

int main(){
....
....
...
       for(j=0; j<n-1; j++){
            if(a[j+1] > a[j])
                hj++;
            else if(a[j+1]< a[j])
                lj++;
        }
....
.....
}

UVA Problem#900

Question:

Solution:
1. if the length is 1 output will be 1, if the length is 2 out put will be 2, if the length is 3 output will be 3, if the length is 4 output will be 5. Doesn't the output series look familiar to you? 1 2 3 5 ...... Ya right! Its a Fibonacci series!
2. You can have the first 50 Fibonacci numbers in an array and then just print the answer according to the input.

int main(){
 int fib[ ] = {1,2,3,5,8,13,21,.   .. .  . .. . .. ,7778742049, 12586269025};
int n;
while(1){
scanf("%d", &n);
if(n==0)
break;
printf("%d\n", fib[n-1] );
}
return 0;
}