Search This Blog

Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Friday, 12 June 2015

10879 - Code Refactoring

10879 - Code Refactoring

Problem Link: Click Here!

Solution:

#include<stdio.h>
int main(void){
int i,t,m,ao[5];
long long int a,j;
scanf("%d",&t);
i=1;
while(i<=t){
m=0;
scanf("%lld",&a);
for(j=2;j<=a/2;j++){
if(a%j==0){
        ao[m]=j;
        ao[++m]=a/j;
        m++;
        }
if(m==4)break;
}
printf("Case #%d: %lld = %d * %d = %d * %d\n",i,a,ao[0],ao[1],ao[2],ao[3]);
i++;
}
return 0;
}

11192 - Group Reverse

11192 - Group Reverse

 

Problem Link:Click Here!


Solution:


#include<stdio.h>
#include<string.h>
int main(void){
      int a,l,i,j,m,n;
      char str[101],temp;


      while(scanf("%d",&a)==1 && a!=0){
         scanf(" %s",str);
         l=strlen(str);
         n=l/a;


         for(i=0;i<=l-n;i=i+n){
               m=i;
                j = (m+n)-1;


               while (m < j) {
                       temp = str[m];
                       str[m] = str[j];
                       str[j] = temp;
                       m++;
                        j--;
                  }


            }
       printf("%s\n",str);
       }
return 0;
}

Thursday, 11 June 2015

12577 - Hajj-e-Akbar

11530 - SMS Typing

Uva-11530 - SMS Typing

Problem link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2525

Solution:

#include<stdio.h>
int main(void){

int t,i=1,sum=0;
char a,b;

scanf("%d",&t);
scanf("%c",&b);

while(i<=t){

while(scanf("%c",&a)==1){

if(a=='\n'){
printf("Case #%d: %d\n",i,sum);
sum=0;
break;
}

switch(a){
case'a':
case'd':
case'g':
case'j':
case'm':
case'p':
case't':
case'w':
sum=sum+1;
break;

case'b':
case'e':
case'h':
case'k':
case'n':
case'q':
case'u':
case'x':
sum=sum+2;
break;

case 'c':
case'f':
case'i':
case'l':
case'o':
case'r':
case'v':
case'y':
sum=sum+3;
break;

case's':
case'z':
sum=sum+4;
break;

default:
sum=sum+1;
break;

}
}
i++;
}
return 0;
}

Tuesday, 9 June 2015

10812 - Beat the Spread!

494 - Kindergarten Counting Game

How to Count Words From a String

Counting Words From a String

Two Cases:

case no.1

If there is a single space between every words,like,
Have a nice day.
Then first you have to count the spaces,then the number of the words will be <number of spaces+1>.
In the above example we can see that there is 3 spaces .So the number of words is 3+1=4.

So the code will be in C,

#include<stdio.h>
#include<string.h>
 
int main(void){
   int i,l,count;
   char str[150];

   gets(str);
   count=0;
   l=strlen(str);
   for(i=0;i<=l;i++){
            if(str[i]==' ')count++;
          }
   printf("The number of Words: %d\n",count);
   return 0;
}

case no.2

But what if the sentence is,
Have  .. a nice   day.

here there is 7 spaces and 2 fullstops in the middle of the words.Here we can not apply the previous formula.
Here,There are some steps to count the words.Well,In most programming contest,you will find these types of sentences to calculate words.

Steps are:

1>> First find the start of a word by a loop and if case.
2>>then went on by i++ to find the last of that word.
3>>then continue the loop till the end of the string.

Here is a C code for better understanding the steps.

#include<stdio.h>

#include<string.h>

 int main(void)
    {
   int i,l,count;

   char str[150];   
//variables

   gets(str);

   count=0;      /
/the variable for counting the words

   l=strlen(str);

   for(i=0;i<=l;i++)
    {
      if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
                    //conditions if the ith member of the string is an alphabet(step1)
             {
            count++;

     while((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
                    {
             i++;        
//conditions for finding the last of the word(step 2)
                     }
             }
   }

   printf("The number of Words: %d\n",count);

   return 0;

   }

 if you have any question you can comment.Thank you.