Showing posts with label OJ SOLUTION. Show all posts
Showing posts with label OJ SOLUTION. Show all posts

UVA: 490 - Rotating Sentences - Solution in C/C++


Problem Name:  Rotating Sentences

Problem Source: Uva Online Judge

Problem No: 490

Problem Difficulty: Easy

Problem Link: Here

Submit Solution Here

Important Point

  • You must have to assign a white space to every null value of every sentence.
  • Top sentence will be on right most and last sentence will be on left most.
  • You have to print every character including white space. 
Source Code


 #include<bits/stdc++.h>  
 using namespace std;  
 //Code Written by Al Shah Reyaj  
 //BSMRSTU, Gopalganj, Bangladesh  
 int main()  
 {  
   char s[105][105];  
   int c=0,m=0,l;  
   for(int i=0;i<105;i++)  
   {  
     for(int j=0;j<105;j++) s[i][j]=' ';  
   }  
   while(gets(s[c]))  
   {  
     l=strlen(s[c]);  
     if(l>m) m=l;  
     c++;  
   }  
   for(int i=0;i<m;i++)  
   {  
     for(int j=c-1;j>=0;--j)  
     {  
       if(s[j][i]=='\0') cout<<' ';  
       else  
       cout<<s[j][i];  
     }  
     cout<<endl;  
   }  
   return 0;  
 }  


Thanks for Reading This. Check our more Activities.

UVA: 100 - The 3n + 1 problem - Solution in C/C++



Problem Name: The 3n+1 problem

Problem Source: Uva Online Judge

Problem No: 100

Problem Difficulty: Easy

Problem Link: Here

Submit Solution Here

Important Point

  • i can be bigger than j. if i is bigger than j then you have to run the loop from j to i;
  • i and j in the output must be same as input.
  • You have to count n=1 case.
  • You don't have to use long long. int is enough.
  • You have to run the loop including i and j.

Source Code


#include<bits/stdc++.h>  
 using namespace std;  
 //Code Written by Al Shah Reyaj  
 //BSMRSTU, Gopalganj, Bangladesh  
 /* ******* ****    **    *******  
   *   *  **    * *      *  
   *  *  **   *  *     *  
   ****   **   ********    *  
   * *   **  *    *   *  
   *  *   **  *     *  *  
   *  *  **** *      * *******  
 */  
 int fin(int n,int c)  
 {  
   c++;  
   if(n==1) return c;  
   if(n%2!=0) fin((3*n)+1,c);  
   else fin(n/=2,c);  
 }  
 int main()  
 {  
   int a,b;  
   while(cin>>a>>b)  
   {  
     int m=0;  
     int x=a,y=b;  
     if(a>b)  
     {  
       int x=a;  
       a=b;  
       b=x;  
     }  
     for(int i=a; i<=b; i++)  
     {  
       int l=fin(i,0);  
       if(l>m) m=l;  
     }  
     cout<<x<<" "<<y<<" "<<m<<endl;  
   }  
   return 0;  
 


Thanks for Reading This. Check our more Activities.

DOWNLOAD PC GAMES

DOWNLOAD PC APPS