Pages

Tuesday, December 21, 2010

UVA Problem#541

Question:

Solution:

char  mat[105][105];
int N;

int row()  {

    int i,j,sum,c,save;
    sum = 0;
      c = 0;
    for(i = 0; i<N; i ++)  {
      sum = 0;
      for(j = 0; j<N; j ++)
       sum += mat[i][j];

UVA Problem#530

Question:
Solution:

double nCr(int n,int m){
    int k;
    register int i,j;
    double c,d;
    c=d=1;
    k=(m>(n-m))?m:(n-m);
    for(j=1,i=k+1;(i<=n);i++,j++){
        c*=i;
        d*=j;
        if( !fmod(c,d) && (d!=1) ){
            c/=d;
            d=1;
        }
    }
    return c;
}

int main(){
   ....
   ....
   ....
}

UVA Problem#495

Question:

Solution:

#include<iostream.h>
#include<string.h>
int main(){
    char *fibo[5001]={0};
    fibo[0]="0";
    fibo[1]="1";
    int l1=strlen(fibo[0]);
    int l2=strlen(fibo[1]);
    int l;
    for(long i=2;i<=5000;i++){

UVA Problem#483

Question:

Solution:
int main(){
   
    string line;
   
    while(getline (cin,line)) {
                  int t = line.size();
                  int count;
                  string sub;
                  for(int i=0;i<t;i++){
                       count=0;
                       if(line[i]!=' '){    
                                while(line[i+count]!=' ' && i+count<t){
                                       count++;         
                                }
                                for(int j=(i+count)-1;i<=j && j<t;j--){
                                          cout<<line[j];
                                }    
                                i+=count-1;  
                       }else{
                            cout<<line[i];
                       }
                  }
                  cout<<endl;
    }
   
 return 0;  
}

UVA Problem#412

Question:

Solution:

int gcd(int a,int b)  { return b ? gcd(b,a%b) : a;  }

int count()  {

   int i,j;
   k = 0;
   for(i = 0; i<N-1;  i ++)
     for(j = i+1; j<N;  j ++)
    if(gcd(a[i],a[j]) == 1)
       k ++;
   return 0;
}

int calculate()  {

    int pair;
    double pi,x,y;
    if( !k )  { printf("No estimate for this data set.\n");  return 0; }
    pair = (N * (N-1)) / 2;
     x = pair;
     y = k;
    pi = sqrt(( x * 6) / y);
    printf("%.6lf\n",pi);
    return 0;
}

int main(){
.......
......
}

UVA Problem#190

Question:

Solution:
1. take x1, y1, x2, y2, x3 and y3 as input.
2. calculate m1 and m2. m1= (y2-y1)/(x2-x1); m2= (y3-y1)/(x3-x1);
3. for different condition (values) of m1 and m2 the formula to find h and k (circles center) will be different.
4. Formulas of h and k on different conditions
     (i) m2=0;  k=(x2-x3)/m1/2+(y1+y2)/2; h=(x1+x3)/2;
     (ii) m1=0; k=(x3-x2)/m2/2+(y1+y3)/2; h=(x1+x2)/2;
     (iii) m1!=0 and m2!=0; h=((x1+x2)/m1-(x1+x3)/m2+y2-y3)/2/(1/m1 - 1/m2); 
      k=( h-(x1+x2)/2)/(-m1)+(y1+y2)/2;