Thursday, 21 November 2013

UVA sloved: problem number: 272 - TEX Quotes


                                               TeX Quotes

TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use `` and " to delimit quotations, rather than the mundane " which is what is provided by most keyboards. Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote ` and a right-single-quote '. Check your keyboard now to locate the left-single-quote key ` (sometimes called the ``backquote key") and the right-single-quote key ' (sometimes called the ``apostrophe" or just ``quote"). Be careful not to confuse the left-single-quote ` with the ``backslash" key \. TeX lets the user type two left-single-quotes `` to create a left-double-quote `` and two right-single-quotes '' to create a right-double-quote ''. Most typists, however, are accustomed to delimiting their quotations with the un-oriented double-quote ".

If the source contained
"To be or not to be," quoth the bard, "that is the question."
then the typeset document produced by TeX would not contain the desired form:


``To be or not to be," quoth the bard, ``that is the question."

In order to produce the desired form, the source file must contain the sequence:
``To be or not to be,'' quoth the bard, ``that is the question.''

You are to write a program which converts text containing double-quote (") characters into text that is identical except that double-quotes have been replaced by the two-character sequences required by TeX for delimiting quotations with oriented double-quotes. The double-quote (") characters should be replaced appropriately by either `` if the " opens a quotation and by '' if the " closes a quotation. Notice that the question of nested quotations does not arise: The first " must be replaced by ``, the next by '', the next by ``, the next by '', the next by ``, the next by '', and so on.

Input and Output

Input will consist of several lines of text containing an even number of double-quote (") characters. Input is ended with an end-of-file character. The text must be output exactly as it was input except that:

  • the first " in each pair is replaced by two ` characters: `` and
  • the second " in each pair is replaced by two ' characters: ''.

Sample Input

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

Sample Output

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''

_____________________________________________________________

solution:

#include<cstdio>
int main()
{
 char ch;
 long count=0;
 while(scanf("%c", &ch)==1)
 {
  if(ch=='"')
  {
   count++;

   if(count%2 == 1)
    printf("``");
   else if(count%2 == 0)
    printf("''");
  }
  else 
   printf("%c",ch);
 }
}


Wednesday, 20 November 2013

UVA sloved: problem number: 369 - Combinations

question:


                                          Combination

Computing the exact number of ways that N things can be taken M at a time can be a great challenge when N and/or M become very large. Challenges are the stuff of contests. Therefore, you are to make just such a computation given the following:


GIVEN:



Compute the EXACT value of:




You may assume that the final value of C will fit in a 32-bit Pascal LongInt or a C long.

For the record, the exact value of 100! is:

     93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,264,381,621,
        468,592,963,895,217,599,993,229,915,608,941,463,976,156,518,286,253,
        697,920,827,223,758,251,185,210,916,864,000,000,000,000,000,000,000,000

Input and Output

The input to this program will be one or more lines each containing zero or more leading spaces, a value for N, one or more spaces, and a value for M. The last line of the input file will contain a dummy N, M pair with both values equal to zero. Your program should terminate when this line is read.

The output from this program should be in the form:


N things taken M at a time is C exactly.

Sample Input


     100  6
      20  5
      18  6
       0  0

Sample Output
100 things taken 6 at a time is 1192052400 exactly.
20 things taken 5 at a time is 15504 exactly.
18 things taken 6 at a time is 18564 exactly.

_____________________________________________________________

solution:


#include <iostream>
#include <iomanip>
using namespace std;


long double factorial (long double n)
{
    long double x = 1;
    for (long double i = 2; i <= n; i++) { x*= i;}
    return x;
}
int main ()
{
    long double m, n;
 while (cin >> n >> m && m!=0 && n!=0)// && m<=n && 5<=n<=100 && 5<=m<=100)
    {
        long double f = (factorial(n))/(factorial(n-m)*factorial(m));
        cout << n << " things taken " << m << " at a time is " << fixed << setprecision(0) << f << " exactly." << endl;
    }
}


tools: Time check in C++

#include<iostream>
#include<time.h>
using namespace std;

int main()
{
time_t beg, end;
time(&beg);

for(int y=0; y<100; y++)
{

}

time(&end);
double dif = double(end - beg) / CLOCKS_PER_SEC;
cout <<"total time spended: " <<dif <<endl<<endl<<endl;
}



Tuesday, 19 November 2013

Source code: Conversion of decimal to Binary and Binary to decimal using C++ { conversion of 10 based integer to 2 based integer and vice versa } [ user friendly, easy, object oriented ]

#include<iostream>
using namespace std;

void d2b()
{
long decimal, d12, rem, x=1, sum=0;

cout <<"pick a decimal integer: "; cin>> decimal;
d12= decimal;

while(decimal>0)
{
rem= decimal % 2;
sum+= ( x*rem );
decimal/= 2;
x*= 10;
}
cout <<"\n\nBinary of " <<d12 <<" is: " <<sum <<endl <<endl;
}

void b2d()
{
long binary, m35, rem, y=1, sum=0;

cout <<"pick a binary number: "; cin>> binary;
m35= binary;

while(binary !=0)
{
rem= binary % 10;
sum+= (y*rem);
binary/=10;
y*=2;
}
cout <<"\n\ndecimal of " <<m35 <<" is: " <<sum <<endl <<endl;
}

int main()
{
char c;
cout <<"make your pick: \n";
cout <<"a. decimal to Binary.\n";
cout <<"b. Binary to decimal.\npick: ";
cin>> c;

switch(c)
{
case 'a': d2b(); break;
case 'b': b2d(); break;
}
}

UVA sloved: problem number: 458 - The Decoder

question:
                                                             The Decoder

Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that the characters contain. The code key for this simple coding is a one for one character substitution based upon a single arithmetic manipulation of the printable portion of the ASCII character set.

Input and Output

For example: with the input file that contains:

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

your program should print the message:
*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.

Your program should accept all sets of characters that use the same encoding scheme and should print the actual message of each set of characters.

Sample Input

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

Sample Output

*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.
______________________________________________________________________

solution:

#include <stdio.h>
int main()
{
char ch;
while (scanf("%c", &ch)==1)
    if (ch == '\n') printf("\n");
    else printf("%c",ch-7);
return 0;
}

**use the code only for practice purpose only. don't ruin your programming career copy-pasting this.

Monday, 21 October 2013

raw code: Integer Array Pre-Calculation [ easiest, user friendly and OOr ]

#include<iostream>
using namespace std;

int n, ar[100];


void takin()

{
cout <<"number of element: "; cin>> n;

cout <<"put in the elements: ";

for(int x=0; x<n; x++)
{
cin>> ar[x];
}
}

void show()

{
cout <<"\nelements are: ";
for(int y=0; y<n; y++)
{
cout <<ar[y] <<' ';
}

}


void precal()

{
cout <<"\n\nPre-Cal: ";
for(int z=0; z<n; z++)
{
ar[z+1]+=ar[z];
cout <<ar[z] <<' ';
}
}

int main()

{
takin(); show(); precal();
cout <<endl<<endl<<endl<<endl<<endl;
return 777;
}

Sunday, 13 October 2013

Source code: Morse code translator in C++ [ simple but freakish]

Morse Code

#include<iostream>
using namespace std;

char c, x;


int check(char);

void go(char);

void start()

{
cout <<"give your input hea: "; cin>> c;
check (c);
}

int check(char)

{
try
{
if((c>=65 && c<=90)||(c>=97 && c<=122))
{go(c);}

else

{throw x;}
}
catch(...)
{
cout<<"\n\ntry again fella!\ngive it again: ";
cin>> c;
check(c);
}
return 1;
}

void go(char c)

{
cout <<"\n\nin morse code it is: ";

if(c=='a' || c=='A')

{cout <<".-\n\n\n";}

else if(c=='b' || c=='B')

{cout <<"-...\n\n\n";}

else if(c=='c' || c=='C')

{cout <<"-.-.\n\n\n";}

else if(c=='d' || c=='D')

{cout <<"-..\n";}

else if(c=='e' || c=='E')

{cout <<".\n";}

else if(c=='f' || c=='F')

{cout <<"..-.\n\n";}

else if(c=='g' || c=='G')

{cout <<"--.\n";}

else if(c=='h' || c=='H')

{cout <<"....\n";}

else if(c=='i' || c=='I')

{cout <<"..\n";}

else if(c=='j' || c=='J')

{cout <<".---\n\n";}

else if(c=='k' || c=='K')

{cout <<"-.-\n";}

else if(c=='l' || c=='L')

{cout <<".-..\n";}

else if(c=='m' || c=='M')

{cout <<"--\n";}

else if(c=='n' || c=='N')

{cout <<"-.\n\n";}

else if(c=='o' || c=='O')

{cout <<"---\n\n";}

else if(c=='p' || c=='P')

{cout <<".--.\n";}

else if(c=='q' || c=='Q')

{cout <<"--.-\n";}

else if(c=='r' || c=='R')

{cout <<".-.\n";}

else if(c=='s' || c=='S')

{cout <<"...\n";}

else if(c=='t' || c=='T')

{cout <<"-\n";}

else if(c=='u' || c=='U')

{cout <<"..-\n\n";}

else if(c=='v' || c=='V')

{cout <<"...-\n";}

else if(c=='w' || c=='W')

{cout <<".--\n";}

else if(c=='x' || c=='X')

{cout <<"-..-\n";}

else if(c=='y' || c=='Y')

{cout <<"-.--\n";}

else if(c=='z' || c=='Z')

{cout <<"--..\n";}
}

int main(void)

{
start();
return 0;
}


To Remember It:
Morse Code