UVa 10773 – Back to Intermediate Math

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

using namespace std;

int main (){

	int n,t=0;
	double d,u,v,ans;
	cin>>n;

	std::cout.setf(std::ios::fixed);
	std::cout.precision(3);

	while (t++<n){
		cin>>d>>v>>u;
		ans = u*u - v*v ;
		if (ans<0.001||u<0.001||v<0.001)
           //cases that make the eqn of ans contains a NaN || d=0
			cout<<"Case "<< t <<": can't determine"<<endl;

		else {
			ans = d/sqrt(ans) -  d/u;
           //d/sqrt(u*u - v*v) = stearing with -ve angle so that the flow takes the boat to the destination
			cout<<"Case "<< t <<": "<< ans << endl;
		}
	}

	return 0;
}

Advertisements

UVa 11466 – Largest Prime Divisor

#include <iostream>
using namespace std;

int main() {
	long long n,ans,i;
	int counter;
	cin>>n;
	while (n!=0){

		if (n<0)
			n=-n;

		ans = -1;
		counter = 0;
		//print -1 for numbers that has 1 prime divisors as primes, pow of primes (4,8,9)
		for ( i = 2; i*i <= n && n!=1; i++) {
			while (n%i == 0){
				n/=i;
				ans = i;
			}
			if (ans == i)counter++;
		}


		if (n!=1 && ans!=-1)
			ans = n;
		else if (counter==1)
			ans = -1;

		cout<<ans<<endl;

		cin>>n;
	}

}

UVa 12024 – Hats


#include <iostream>
using namespace std;

//int  factorial[14] ;
int wrong[14];
int m;

int C(int n, int r){
	return factorial[n]/factorial[n-r]/factorial[r];
}

int solve (int n){
	if (wrong[n] != -1)return wrong[n];
	else{
		wrong[n] = 0;
		for(int i=1;i<=n;i++){
			wrong[n] += C(n,i)*solve(n-i);
		}
		wrong[n] = factorial[n] - wrong[n];
		return wrong[n];
	}
}

int main(){
	int tc,i;

   //calculating the first 14 factorial;
	factorial[0] = 1;
	for (i = 1 ; i <= 14; i ++){
		factorial[i] = i*factorial[i-1];
		wrong [i]= -1;
	}


	wrong[0]= 1;
	wrong[1]= 0;
	cin>>tc;
	while(tc--){
		cin>>m;
		cout<<solve(m)<<"/"<<factorial[m]<<endl;
	}

   //all possible cases = n! == P(n,n);
   //no one has a right hat = all possible cases - (1 has right hat + 2 have right hats + .... + n have right hats)
   // solve (n) = n! - solve (n-1);
   //it's the power of recursion 😀 😀

	return 0;
}

UVa 11879 – Multiple of 17


#include <cstdio>
using namespace std ;

int main(){
    int n[101],i,j,d,d1,d2;
    char temp ;
    while (true){
        i = 0;
        while (true){
            temp = getchar();

            if (temp == '\n')break;
            n[i++] = temp - '0';
        }
        if (i == 1 && n[0]==0)return 0;

        i--;

        for(j = i ; j>2 ; j--){

            d = n[j]*5; // d = d1+d2*10;
            d1 = d%10;
            d2 = d/10;

            if (n[j-1]>d1){
                n[j-2]--;
                n[j-1] = n[j-1]+10-d1;
            }else
                n[j-1] = n[j-1]-d1;

            if (n[j-2]>d2){
                n[j-3]--;
                n[j-2] = n[j-2]+10-d2;
            }else
                n[j-2] = n[j-2]-d2;

        }

        if (i == 1){
            temp = n[0];
            d = 5*n[1];
        }
        else {
            temp = n[1]+n[0]*10;
            d = 5*n[j];
        }

        if ((temp-d)%17==0)
            printf("1\n");
        else
            printf("0\n");

    }
    return 0;
}