Friday 22 November 2013

UVA sloved: problem number: 11805 - Bafana Bafana

Bafana Bafana <3
Team practice is very important not only for programming contest but also for football. By team practice players can learn cooperating with team mates.  For playing as a team improvement of passing skill is very important. Passing is a great way of getting the ball upfield and reduces the risk of giving the ball away.

Carlos Alberto Parreira, the coach of Bafana Bafana, also wants his players to practice passing a lot. That’s why, while in the training camp for soccer world cup 2010, every day he asks all of the players who are present in practice to stand in a circle and practice passing. If N players are in practice, he gives each of the players a distinct number from 1 to N, and asks them to stand sequentially, so that player 2 will stand in right side of player 1 and player 3 will stand in right side of player 2, and so on. As they are in a circle, player 1 will stand right to player N.

The rule of passing practice is, Parreira will give the ball to player K, and practice will start. Practice will come to an end after P passes. In each pass, a player will give the ball to his partner who is in his immediate right side. After P passes, the player who owns the ball at that moment will give the ball back to Parreira.

Parreira wants to be ensured that his players practice according the rule. So he wants a program which will tell him which player will give him the ball back. So after taking the ball from the same person he can be happy that, the players play according to the rules. Otherwise he will ask them to start from beginning.


Input
Input starts with an integer (T <= 1000), the number of test cases. Each test case will contain three integers, N (2 <= N <= 23), K (1 <= K<= N), P(1<=P<=200).

Output
For each test case, output a single line giving the case number followed by the Bafana player number who will give the ball to Parreira. See sample output for exact format.
Sample Input
Sample Output
3
5 2 5
6 3 5
4 1 3
Case 1: 2
Case 2: 2
Case 3: 4
Problemsetter: Md. Arifuzzaman Arif, Special Thanks: Muntasir Khan, Sohel Hafiz

_______________________________________________________________



solution:

#include<cstdio>
int main()
{
 int n,k,p,t,x=1;
 scanf("%d",&t);
 while(x<=t)
 {
  scanf("%d%d%d",&n,&k,&p);
  printf("Case %d: %d\n",x,(((k-1+p)%n)+1));
  x++;
 }
 return 0;
}

Thursday 21 November 2013

UVA sloved: problem number: 11854 - Egypt

                                                                 
question:
                                                             Problem A: Egypt

A long time ago, the Egyptians figured out that a triangle with sides of length 3, 4, and 5 had a right angle as its largest angle. You must determine if other triangles have a similar property.


The Input
Input represents several test cases, followed by a line containing 0 0 0. Each test case has three positive integers, less than 30,000, denoting the lengths of the sides of a triangle.


The Output
For each test case, a line containing "right" if the triangle is a right triangle, and a line containing "wrong" if the triangle is not a right triangle.


Sample Input
6 8 10
25 52 60
5 12 13
0 0 0

Output for Sample Input
right
wrong
right

_____________________________________________________________________

solution:

#include<cstdio>

int main()
{
int a,b,c;
  while (scanf("%d %d %d",&a,&b,&c)==3)
        {
         if (a==0 && b==0 && c==0)
         break;
          if (a<=0 || b<=0 ||c<=0)
                     printf("wrong\n");
           else if (a*a==b*b+c*c)
                     printf("right\n");
          else if (b*b==a*a+c*c)
                     printf("right\n");
          else if (c*c==a*a+b*b)
                     printf("right\n");
          else
                     printf("wrong\n");
         }

}

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.