UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 496 ( Simply Subsets )Solution

 

#include <iostream>

#include <string>

#include <sstream>

#include <vector>



using namespace std;



int main()

{

   

    stringstream ss;

   

    string line1,line2;

   

    vector<int>A,B;

   

    int i,j,ch;

   

    while(getline(cin,line1) &&    getline(cin,line2))

    {

   

    A.clear();

    B.clear();

    ss.clear();

    ss<<line1;   

   

    while(ss>>ch)

    {

        A.push_back(ch);

    }

   



    ss.clear();

    ss<<line2;   

    while(ss>>ch)

    {

        B.push_back(ch);

    }   

   

    int Acnt=0;

    for(int m=0;m<A.size();m++)

    {

        for(int n=0;n<B.size();n++)

        {

            if(A[m]==B[n])

            {

                Acnt++;

       

            }

        }

    }

           

         if ((Acnt == A.size()) && (Acnt < B.size())) {

            cout << "A is a proper subset of B" << endl;

        }

        else if ((Acnt == B.size()) && (Acnt < A.size())) {

            cout << "B is a proper subset of A" << endl;

        }

        else if ((Acnt == A.size()) && (Acnt == B.size())) {

            cout << "A equals B" << endl;

        }

        else if ((Acnt == 0) ) {

            cout << "A and B are disjoint" << endl;

        }

        else {

            cout << "I'm confused!" << endl;   

        }

    }

   

    return 0;

}



 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 499( What’s The Frequency, Kenneth) Solution

#include <bits/stdc++.h>

#include <string.h>

#include <map>



using namespace std;



int main()

{

   

   

    string s;

    //map<char,int>m;

//        map<char,int>::iterator it;   

    while(getline(cin,s))

    {

        map<char,int>m;

       

        int mxfrq=0;

       

        int len=s.size();

       

        for(int i=0;i<len;i++)

        {

        if((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z'))

        {

            m[s[i]]++;

            mxfrq=max(mxfrq,m[s[i]]);

        }

       

    }   

    map<char,int>::iterator it;   

    for(it = m.begin(); it != m.end(); it++){

      if(it->second == mxfrq)

      cout<<it->first;

      //printf(" value == %c %c\n",it->first,it->second);

    }

    printf(" %d\n",mxfrq);

  }

   

    return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 455 Periodic Strings Solution

 

 

#include <bits/stdc++.h>

using namespace std;



int main()

{

    char s[140],charcter,l;

    int i,j;

   

    while(gets(s))

    {

   

    for(i=0; s[i] != '\0'; )

   

    {

        int freq = 0;

   

        while( (s[i] >='0' && s[i] <='9')  )

        {

           

            charcter = ( s[i++]-'0' );

            freq += charcter;

            //return 0*printf("ch = %c fre = %d\n",charcter,freq);   

        }

   

   

        for(j=0;j<freq;j++)

        {

            if(s[i]=='b')

            {

                cout<<" ";

            }

            else

            {

                printf("%c",s[i]);

            }

        }



            if ( s [i] == '!' )

               

                cout<<endl;

           i++;

           

        }

        

        cout<<endl;

    }



    return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 488 Triangle Wave solution Solution

 

#include <bits/stdc++.h>

using namespace std;



int main()

{

int a,f,tc,i,j,k,l,m,n;



scanf("%d",&tc);



while(tc--)

{

cin>>a;

cin>>f;



for(l=1;l<=f;l++)

{

   

    for(i=1;i<=a;i++)

    {

        for(j=0;j<i;j++)

        {

            cout<<i;

        }

        cout<<"\n";

    }

   

    for(m=a-1;m>0;m--)

    {

        for(n=0;n<m;n++)

        {

            cout<<m;

        }

        cout<<"\n";

    }

   

    if(l != f)  // excluding last one new line hobe

    cout<<"\n";

 }

 if(tc)

 cout<<"\n";   



}

return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 11946 (Code Number Solution) Solution

 

#include <bits/stdc++.h>

#include <string.h>

using namespace std;



int main()

{

int tc,i,j;

char s[100],l;

cin>>tc;



    getchar();

  for(i=1;i<=tc;i++)

  {

    while(gets(s)!='\0')

    {

        l=strlen(s);

        if(l==0)

        {

            break;

        }

        for(j=0;j<l;j++)

        {

            if(s[j]=='3') cout<<"E";

            else if (s[j]=='0') cout<<"O";

            else if (s[j]=='1') cout<<"I";

            else if (s[j]=='4') cout<<"A";

            else if (s[j]=='9') cout<<"P";

            else if (s[j]=='8') cout<<"B";

            else if (s[j]=='5') cout<<"S";

            else if (s[j]=='7') cout<<"T";

            else if (s[j]=='2') cout<<"Z";

            else if (s[j]=='6') cout<<"G";

            else

            cout<<s[j];

        }

       

        cout<<endl;

    }

    if(i != tc)

    {

        cout<<endl;

    }



}

return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 264 Count on Cantor Solution

 

 

#include <bits/stdc++.h>



using namespace std;



int main()

{



    int N,i,j,insum,line;



   while(cin>>N)

   {

    insum=0;



    for(i=0;;i++)

    {



     insum+=i;



     if(insum >= N)

     {

                line = i;

                break;

     }



    }

      printf("TERM %d IS ", N);



      if(line&1)//insum-line

      printf("%d/%d\n", 1+insum-N, line-(insum-N));

      else

      printf("%d/%d\n", line-(insum-N), 1+insum-N);

   }





return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 10189 Minesweeper Solution

 

#include <bits/stdc++.h>



using namespace std;



int main ()

{

    int n,m;

    int count=0;



   while(1)

   {

       cin>> m >> n;

       if (n ==0 && m ==0) break;

       count++;



       if (count > 1) cout << '\n';



       int board[102][102] = {0};



      for (int i=1; i<m+1; i++)

          for (int j=1; j<n+1; j++)

          {



              char mines;

              cin >> mines;



              if (mines == '*')

                  {

                      board[i][j] = -1;

                      if (board[i-1][j] != -1)

                      board[i-1][j]++; //top

                      if (board[i-1][j-1] != -1)

                      board[i-1][j-1]++; //top left

                       if (board[i-1][j+1] != -1)

                       board[i-1][j+1]++; //top right

                      if (board[i][j-1] != -1)

                      board[i][j-1]++;//left

                      if (board[i+1][j] != -1)

                      board[i+1][j]++; // bottom

                      if (board[i+1][j+1] != -1)

                      board[i+1][j+1]++; //bottom right

                      if (board[i+1][j-1] != -1)

                      board[i+1][j-1]++; //bottom right

                      if (board[i][j+1] != -1)

                      board[i][j+1]++; //right

                  }

          }



           cout << "Field #" << count << ":\n";

            for (int i=1; i<m+1; i++)

           {



                for (int j=1; j<n+1; j++)

                    {

                        if (board[i][j] == -1) cout << '*';

                        else cout << board[i][j];

                }

                cout << "\n";

           }



   }



    return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 12157 Tariff Plan Solution

 

 

#include <stdio.h>

#include <bits/stdc++.h>



using namespace std;



int main()

{



    int a,miles,juice,n,T,tc=1,b,c,i,arr,m,j,cells;



    cin>>T;



   for(int i=0;i<T;i++){



    cin>>n;



    miles=0,juice=0;



        while(n--)

        {



        cin>>cells;





         miles +=(cells/30)*10 + 10;



         juice +=(cells/60)*15 +15;





        }



        printf("Case %d: ",tc++);

         if(miles<juice)



         cout<<"Mile"<<" "<<miles<<endl;



         else if(miles>juice)

          cout<<"Juice"<<" "<<juice<<endl;

          else

           cout<<"Mile"<<" "<<"Juice"<<" "<<juice<<endl;







}

return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 123 Searching Quickly Solution

 

 

#include <bits/stdc++.h>

using namespace std;





long long int reverse(long long int n)

{

    long long int sum=0,r;

    while(n > 0)

    {

         r=n%10;

       

         sum=sum*10+r;

         n=n/10;

    }



    return sum;

}

int main()

{

    long long int t, sum, count;

    int cse;

    cin >> cse;



    for (int i=0; i<cse; i++)

    {



         sum=0, count=0;

        cin >> t;

        

         if(reverse(t) == t)

         {

             printf("%lld %lld\n",count,t);

         }

        else if(reverse(t) != t)

        {

            count++;

            t += reverse(t);

        }



        printf("%lld %lld\n",count,t);

    





    }





    return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 160 – Factors and Factorials Solutoin

 

 

#include<bits/stdc++.h>

bool flag[100];

int List[100],primes[100];

int cnt;  

using namespace std;





void sieve(int n)

{

   

cnt=0;

primes[cnt++] = 2;

for(int i=3; i<=n; i+=2)

{

if(flag[i] == 0)

{

primes[cnt++] = i;

if(i <= n/i)

{

for(int j=i*i; j<=n; j+=i*2)

flag[j] = 1;

}

}

}



return ;

}

void primeFactorize( int n)

 {

    

  

    for(int i = 0;primes[i]<=sqrt(n);i++ )

    {

        if( n % primes[i] == 0 )

        {

            while( n % primes[i] == 0 )

             {

                 List[primes[i]]++;

                n /= primes[i];

           

            }

           

        }

    }

   

    if( n > 1 )

    {

       

        List[n]++;

    }

   

    return;

}

int main()

{

   

    int n,c,nw,i;

    sieve(100);

    while(cin>>n)

    {

        if(n==0) break;

        c=0;

        nw=n;

        while(n>1)

        {

        primeFactorize(n);

        n-=1;

        }

    printf("%3d! =",nw);

   

    for( i = 2; i <=nw; i++ )

    {

   

        if(List[i]!=0)

        {

            if(c==15)

            printf("\n      ");

            printf("%3d", List[i]);

            c++;

            List[i]=0;

        }

       

    }

       

    cout<<endl;

}

return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 272 TEX Quotes Solution

 

#include <bits/stdc++.h>

using namespace std;

char s[200000];

int main()

{

   

    int c=0,i;

    while(gets(s))

    {

        for(i=0;i<strlen(s);i++)

    {

       

        if(s[i]=='"')

        {

            c++;

         if(c/2==0)

                printf("``");

                else

                printf("''");

                }

                else

                printf("%c",s[i]);;

        }

        printf("\n");

    }

    return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 299 train swap Solution

 

#include <bits/stdc++.h>



 using namespace std;

int main()

{

  int n, array[5000],k, c,count,d,T, t;



  while(cin>>T)

  {

     

      for(k=0;k<T;k++)

      {

     

      cin>>n;

    count=0;

    for (c = 0; c < n; c++)

       {

        cin>>array[c];

       }



      for (c = 1 ; c <= n - 1; c++) {

    d = c;

    

    while ( d > 0 && array[d] < array[d-1]) {

        count++;

      t          = array[d];

      array[d]   = array[d-1];

      array[d-1] = t;



      d--;

    }

  }





    printf("Optimal train swapping takes %d swaps.\n",count);

 

}

     

  }

 



  return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 336 (A node to far Solution) Solution

 

#include<bits/stdc++.h>



using namespace std;





int tc=1,position=0;



int bfs(int src, int TTL,map <int, vector<int> > edges)

{

    map<int,int> visited,level;



    int unreach=0;



    queue<int>Q;

    Q.push(src);



    position=1;



    visited[src]=1;

    level[src]=0;



    while(!Q.empty())

    {

        int u=Q.front();



        for(int i=0;i<edges[u].size();i++)

        {

            int v=edges[u][i];

            if(!visited[v])

            {

                level[v]=level[u]+1;

                if(level[v]>TTL)

                unreach++; //printf("unreach = %d\n",unreach); j node gula unreach



                visited[v]=1;

                Q.push(v);



                position++;//printf("%d unreach\n",unreach); koto num node teke jay na

            }

        }

        Q.pop();



    }



return unreach;

}



int main()

{

    vector<pair< int,int > >check(100); //pairakare chack ex: 35  2 , 35   3;



    int edge,x,y,node,source,ans;



    while(1)

    {

        map <int,vector<int> > edges; //a[0]=0,1,2;a[1]=2,3,4,5;

        scanf("%d",&edge);



      if(edge==0)

      return 0;



    for(int i=0;i<edge;i++)

    {

        cin>>x>>y;



        edges[x].push_back(y);

        edges[y].push_back(x);



    }

    int index=0,totalunreach;



    while(1)

    {

        cin>>check[index].first>>check[index].second; // 35   2



        if(check[index].first ==0 and check[index].second==0) break;

        index++;

    }



    for(int i=0;i<index;i++)

    {

        ans=bfs(check[i].first,check[i].second,edges);//        printf("val= %d" ,ans);

        totalunreach = ans + edges.size()-position;



        printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",tc++,totalunreach,check[i].first,check[i].second);

        position=0;

    }



    }



    return 0;

}

 

 

UVA Problem 496 ( Simply Subsets )Solution

UVA problem 386 Perfect Cubes solution

 

 

#include <bits/stdc++.h>

using namespace std;

int main()

{

   int a,b,c,d,n,i,j,k;

   for(n=6;n<=200;n++)

   {

       int a=n*n*n;

       for(i=2;i<=200;i++)

       {

           int b=i*i*i;

            for( j=i;j<n;j++)

           {

              int c = j*j*j;

               for( k=j;k<n;k++)

               {

                  int d = k*k*k;

                   if(a==(b+c+d))

                   {

                   printf("Cube = %d, Triple = (%d,%d,%d)\n",n,i,j,k);

                   }



               }

           }

   }

}

return 0;

}



 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 406 Prime Cuts Solution

 

 

#include <stdio.h>

#include<iostream>

using namespace std;

bool flag[1005];

int primes[1005];

int cnt;

void seive(int n)

{

cnt=0;

primes[cnt++] = 1;

primes[cnt++]=2;

for(int i=3; i<=n; i+=2)

{

if(flag[i] == 0)

{

primes[cnt++] = i;

if(i <= n/i)

{

for(int j=i*i; j<=n; j+=i*2)

flag[j] = 1;

}

}

}

return ;

}



int main()

{

    int n,c,res,hi,lo,m;

    while(cin>>n>>c)

    {

       

        if( n==1)

        {

            printf("%d %d: %d\n\n",n,c,n);

        }

        else

        {

            seive(n);

        if(cnt%2==1)

        {

            res=2*c-1;

        }

         else

        {

            res=2*c;

        }

       

        if(res<=cnt)

        {

            printf("%d %d:",n,c);

            m=(cnt-res)/2;

            for(int i=0;i<res;i++)

           

            printf(" %d",primes[m++]);

            printf("\n\n");

        }

   

        else

        {

            printf("%d %d:",n,c);

            for(int j =0;j<cnt;j++)

           

                 printf(" %d",primes[j]);

                     printf("\n\n");

           

        }





        /*{

        hi=2*c;

        lo=2*c + 2;

        for( int i=hi;i<=lo;i++)

        {

            cout<<primes[i] ;

        }

        cout<<"\n";   

    }*/

   

       

        }

    }

    return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 438 – The Circumference of the Circle Solution

 

 

#include<bits/stdc++.h>

#include<math.h>

#define pi 3.141592653589793



int main()

{

double x1,x2,y1,y2,x3,y3,a,b,c,s,radius,d,circle;



while(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3) != EOF)

{

a=sqrt(pow((x1-x2),2)+pow((y1-y2),2));

b=sqrt(pow((x2-x3),2)+pow((y2-y3),2));

c=sqrt(pow((x3-x1),2)+pow((y3-y1),2));



s=(a+b+c)/2;



radius=a*b*c/(4*sqrt((s*(s-a)*(s-b)*(s-c))));



circle=2*pi*radius;



printf("%.2lf\n",circle);

}

return 0;

}

 

UVA Problem 496 ( Simply Subsets )Solution

UVA Problem 483( Word Scramble) Solution

 

 

 #include<stdio.h>

#include<string.h>

int main()

{

int l,i,j,b,k;

char n[1000];

while(gets(n))

{

l=strlen(n);

b=-1;

for(i=0;i<l;i++)

 {

 if(n[i]==' ')



 {

 for(j=i-1;j>b;j--)

 printf("%c",n[j]);

 printf(" ");

 b=i;

 }



}



for(i=l-1;i>b;i--)

 printf("%c",n[i]);

printf("\n");

}

return 0;

}