UVA Problem 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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 488 Triangle Wave solution 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;

}

 

UVA Problem 488 Triangle Wave solution Solution

UVA Problem 492 Pig-Latin Solution

 

 

#include<bits/stdc++.h>

using namespace std;



bool vowel(char ch)

{

    if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')

    return true;

  else

    return false;

}

int main()

{

   

    char ch,c;

   

while((ch=getchar()) != EOF)    //while(ch=getchar())

    {

        //if(ch==EOF)

        //return 0;

       

        if(isalpha(ch))

        {

            if(vowel(ch))

            {

                while(isalpha(ch))

                {

                    printf("value=1 %c",ch);

                    ch=getchar();

                    printf("value=2 %c\n",ch);

                }

                printf("ay\n");

            }

            /*else

            {

                c=ch;

                ch=getchar();

                while(isalpha(ch))

                {

                    printf("%c",ch);

                    ch=getchar();   

                }

                printf("%cay",c);

               

            }*/

        }

        //printf("%c",ch);

   

    }

    return 0;

}

 

UVA Problem 488 Triangle Wave solution Solution

UVA Problem 543 Goldbach’s Conjecture Solution

#include <stdio.h>

#include<iostream>

#include<math.h>

using namespace std;

int primes[]={2,3 ,5, 7, 11, 13, 17, 19, 23 ,29 ,31 ,37, 41, 43, 47 ,53 ,59 ,61,

 67, 71, 73, 79, 83, 89, 97 ,101, 103, 107, 109, 113, 127 ,131, 137,

  139, 149, 151 ,157 ,163, 167, 173 ,179, 181 ,191, 193, 197, 199, 211 ,223 ,227,

  229, 233 ,239, 241, 251 ,257 ,263, 269 ,271, 277 ,281, 283, 293 ,307, 311, 313,

  317, 331, 337, 347, 349, 353, 359, 367, 373, 379 ,383 ,389, 397, 401, 409, 419,

  421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,

  521, 523, 541, 547, 557, 563, 569 ,571, 577, 587, 593 ,599, 601, 607, 613, 617,

   619, 631, 641 ,643, 647, 653 ,659, 661, 673, 677, 683, 691, 701 ,709, 719, 727,

  733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,

   839, 853 ,857, 859, 863 ,877, 881, 883 ,887, 907 ,911, 919, 929 ,937, 941, 947,

    953, 967, 971, 977, 983, 991, 997, 1009,1013};

bool isprimes(int n)

{

    //bool flag=false;

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

    {

        if(primes[i]*primes[i] >n ) //|| primes[i]==1013

        break;

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

        return false;

    }

    return true;

}

int main() {

    int n;

    while (cin>>n) {

        if(n==0) break;

        for (int i = 1;; i++) {

            int p1 = primes[i];

            int p2 = n - primes[i];

            if (isprimes(p1) && isprimes(p2)) {

                printf("%d = %d + %d\n", n, p1, p2);

                break;

            }

        }

    }



    return 0;

}