C++ – Pro9ramming https://pro9ramming.com Software craftsman's blog Wed, 15 Apr 2020 17:51:08 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 Russian peasant multiplication method https://pro9ramming.com/russian-peasant-multiplication/ Wed, 16 Dec 2015 14:37:46 +0000 http://pro9ramming.com/blog/?p=427 Continue reading Russian peasant multiplication method]]> Russian Peasant multiplication (sometimes called “Ethiopian multiplication” or “Ancient Egyptian multiplication“)  is an interesting multiplication method that can be used to multiply large numbers (without overflow).

This task is interesting because different bitwise operators can be used while writing an algorithm in C++:

int a = 86, b = 57, product = 0;
while (b)
{
if (b & 1)
product += a;
a <<= 1;
b >>= 1;
}
cout << product;
cout << "\n";

First of all “while (b)” actually means “while (b!=0)”. (b & 1) is actually a check if number is odd. And << and >> are shifting operators which effectively give the result of integer multiplication and division respectively.

The solution can be written even more short by using for loop and ternary operator:

int a,b,product = 0;
for(;b;product+=(b&1?a:0), b>>=1, a<<=1);
cout << product;
cout << "\n";

 

 

]]>
Binary GCD Algorithm https://pro9ramming.com/binary-gcd-algorithm/ Tue, 02 Dec 2014 12:43:27 +0000 http://pro9ramming.com/blog/?p=226 This is an interesting algorithm for finding the greatest common divisor.

Euclid Algorithm that does the same thing (more here).
Except Euclid Algorithm, Binary Algorithm is used, more on wiki.
C++ code:

# include <iostream>
# include <cstdio>

using namespace std;

int gcd(int u,int v)
{
     int s;
     if (u == 0 || v == 0)
       return u | v;
     for (s = 0; ((u | v) & 1) == 0; ++s) {
         u >>= 1;
         v >>= 1;
     }
     while ((u & 1) == 0)
       u >>= 1;
     do {
         while ((v & 1) == 0) 
           v >>= 1;
         if (u < v) {
             v -= u;
         } else {
             int diff = u - v;
             u = v;
             v = diff;
         }
         v >>= 1;
     } while (v != 0);
     return u<<s;
}

int main()
{
    int a,b;
    cin>>a;
    cin>>b;
    cout<<gcd(a,b)<<endl;
    system("pause");
}

 

]]>
Reverse and Add task https://pro9ramming.com/reverse-and-add-task/ Sun, 23 Nov 2014 07:43:27 +0000 http://pro9ramming.com/blog/?p=305 Continue reading Reverse and Add task]]> The reverse and add function starts with a number, reverses its digits, and adds the reverse to the original. If the sum is not a palindrome (meaning it does not give the same number read from left to right and right to left), we repeat this procedure until it does.
For example, starting with 195 as the initial number, 9,339 is the resulting palindrome after the fourth addition:

195  
591   
___
786   

786
687
___
1,473

1,473

This method leads to palindromes in a few steps for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It has never been proven, however, that no such palindrome exists. The task is to write a program that takes a given number and gives the resulting palindrome (if one exists) and the number of iterations it took to find it. It can be assumed that all the numbers used as test data will terminate in an answer with less than 1,000 iterations (additions), and yield a palindrome that is not greater than 4,294,967,295.
Input
The first line will contain an integer N (0 < N ≤ 100), giving the number of test cases, while the next N lines each contain a single integer P whose palindrome you are to compute.
Output
For each of the N integers, print a line giving the minimum number of iterations to find the palindrome, a single space, and then the resulting palindrome itself.
Sample Input
3
195
265
750
Sample Output
4 9339
5 45254
3 6666
Pascal code:

program reverse_and_add;
uses wincrt;
var
n,steps,p,i:longint;
a:array[1..100] of longint;
function reverse(k:longint):longint;
var
  p,i,g,f:longint;
  a:array[1..100] of 0..9;
begin
  g:=1;
  i:=0;
  f:=0;
  repeat
    begin
     i:=i+1;
     a[i]:=k mod 10;
     k:=k div 10;
    end;
  until k=0;
  for p:=1 to i-1 do
   g:=g*10;
   for p:=1 to i do
     begin
      f:=f+g*a[p];
      g:=g div 10;
     end;
     reverse:=f;
end;
function add(k:longint):longint;
var
  i:integer;
begin
  i:=0;
  repeat
    begin
      k:=k+reverse(k);
      i:=i+1;
    end;
  until reverse(k)=k;
  steps:=i;
  add:=k;
end;
begin
readln(n);
for i:=1 to n do
readln(a[i]);
for i:=1 to n do
   begin
     p:=add(a[i]);
     writeln(steps,' ',p);
   end;
end.

C++ code:

#include <iostream>
#include <cstdio>

using namespace std;

int steps,p,i;
int a [100];

int reverse(int k)
{
  int p,i,g,f;
  int a[100];
  g=1;
  i=0;
  f=0;
  do
    {
     i++;
     a[i] = k % 10;
     k = k / 10;
    }
  while (!k == 0);
  for (p=1;p<=i-1;p++)
   g=g*10;
    for (p=1;p<=i;p++)
     {
      f=f+g*a[p];
      g=g / 10;
      }
     return f;
}
int add(int k)
{
  int i;
  i=0;
  while (reverse(k)>k | reverse(k)<k)
    {
      k = k + reverse(k);
      i++;
    }
  
  steps = i;
  return k;
}
int main()
{
    int n;
    cout<<"enter number of test cases"<<endl;
cin>>n;
for (i=1;i<=n;i++)
cin>>a[i];
for (i=1;i<=n;i++)
   {
     p=add(a[i]);
     cout<<steps<<" "<<p<<endl;
   }
   system("pause");
}

 

]]>
153 Theorem https://pro9ramming.com/153-theorem/ Wed, 12 Nov 2014 17:43:27 +0000 http://pro9ramming.com/blog/?p=105 Continue reading 153 Theorem]]> Array is formed this way: First member of array is natural number that can be divided with 3. Every other member is formed by sum of digits ^3 of the member before. For example if number is 45 than next will be 4^3+5^3=64+125=189. There is an interesting theorem that last number in this array is 153. As you can see 1^3+5^3+3^3=1+125+27=153.
Your task is to find after how many members reach 153.
Input
From standard input read an unsigned integer that can be divided with 3.
Output
Output an integer that shows how many members were before.
Sample Input
876
Sample Output
13
Solution
Without an array, just use 2 loops, one for counting members and one for making sum. Here, 3 repeat loops, one for input (if number can not be divided with 3) and 2 for task itself.
TPW:

program theorem_153;
uses wincrt;
var
n,i,k:longint;
begin
repeat
readln(n);
until n mod 3 = 0;
i:=1;
repeat
begin
   k:=n;
   n:=0;
   repeat
    begin
      n:=n+(k mod 10)*(k mod 10)*(k mod 10);
      k:=k div 10;
    end;
   until k=0;
   i:=i+1;
end;
until n=153;
writeln(i);
end.

C++:

# include <iostream>
# include <cstdio>

using namespace std;

int main ()
{
  int n,i,k;
cin>>n;
i=1;
while (n>153 | n<153)
{
   k=n;
   n=0;
   while (k > 0)
    {
      n=n+(k % 10)*(k % 10)*(k % 10);
      k=k / 10;
    }
   i++;
}
cout<<i<<endl;
system("pause");
}

 

]]>
Symmetry of Matrix https://pro9ramming.com/symmetry-of-matrix/ Sat, 01 Nov 2014 18:43:27 +0000 http://pro9ramming.com/blog/?p=113 This is program for declaring if matrix (2D array) is symmetric vertically, horizontally and diagonally (both diagonals) – Although some theories say that if matrix is symmetric in one diagonal, then it is in other.
TPW code:

program matrix;
uses wincrt;
type ttip=array[1..50,1..50] of integer;
var
a:ttip;
m,n,i,j:integer;

function vertical(a:ttip):boolean;
var
  i,j:integer;
  h:boolean;
begin
  h:=true;
  for i:=1 to m do
  for j:=1 to n do
  begin
    if h=true then
    begin
     if a[i,j]<>a[i,m-j+1] then
     h:=false;
    end;
  end;
  if h=false then
  vertical:=false
  else
  vertical:=true;
end;

function horizontal(a:ttip):boolean;
var
  i,j:integer;
  h:boolean;
begin
  h:=true;
  for i:=1 to m do
  for j:=1 to n do
  begin
    if h=true then
    begin
     if a[i,j]<>a[n-i+1,j] then
     h:=false;
    end;
  end;
    if h=false then
  horizontal:=false
  else
  horizontal:=true;
end;

function diagonal1(a:ttip):boolean;
var
  i,j:integer;
  h:boolean;
begin
  h:=true;
  for i:=1 to m do
  for j:=1 to n do
  begin
    if h=true then
    begin
     if a[i,j]<>a[j,i] then
     h:=false;
    end;
  end;
  if h=false then
  diagonal1:=false
  else
  diagonal1:=true;
end;

C++:

#include <iostream>
#include <math.h>

using namespace std;
int main()
{
   cout<<"- - - - - - - - - - - - - - - - - - -\n";
   cout<<"Program declaring if matrix is symmetric";
   cout<<"vertically, horizontally and diagonally\n";
   cout<<"C++ Version\n";
   cout<<"- - - - - - - - - - - - - - - - - - -\n";
   int i,j,n,f;


   cout<<"Enter length of array ?n";
   cin>>n;
   cout<<"Enter hight of array ?n";
   cin>>f;
   cout<<"Enter array with parts separated with space !n";
   cout<<"After you finish one line press Enter to type another !n";
     int a[n][f];
      for (i=1;i<=n;i++)
      for (j=1;j<=f;j++)
       cin>>a[i][j];
    //VERTICAL EXAMINATION
    int i1,j1;
    bool h1;
    h1=true;
    
    for (i1=1;i1<=n;i1++)
    for (j1=1;j1<=f;j1++)
     if (h1==true)
     if (!(a[i1][j1]==a[i1][n-j1+1]))
     {
       h1=false;    
     }
     if (h1==true)
     cout<<"Matrix is simetric vertically !n";
     else
     cout<<"Matrix is not simetric vertically !n";
    
    //HORIZONTAL EXAMINATION
    int i2,j2;
    bool h2;
    h2=true;
    
    for (i2=1;i2<=n;i2++)
    for (j2=1;j2<=f;j2++)
     if (h2==true)
     if (!(a[i2][j2]==a[f-i2+1][j2]))
     {
       h2=false;    
     }
     if (h2==true)
     cout<<"Matrix is simetric horizontally !n";
     else
     cout<<"Matrix is not simetric horizontally !n";
    
     //DIAGONAL 1 EXAMINATION
     cout<<"Diagonal 1 =n";
     cout<<"1n";
     cout<<"  1n";
     cout<<"    1n";
     cout<<"      1n";
     cout<<"        1n";
    int i3,j3;
    bool h3;
    h3=true;
    
    for (i3=1;i3<=n;i3++)
    for (j3=1;j3<=f;j3++)
     if (h3==true)
     if (!(a[i3][j3]==a[j3][i3]))
     {
       h3=false;    
     }
     if (h3==true)
     cout<<"Matrix is simetric diagonally (1) !n";
     else
     cout<<"Matrix is not simetric diagonally (1) !n";
    
     //DIAGONAL 2 EXAMINATION
     cout<<"Diagonal 2 =n";
     cout<<"        1n";
     cout<<"      1n";
     cout<<"    1n";
     cout<<"  1n";
     cout<<"1n";
     int i4,j4;
     bool h4;
     h4=true;
    
     for (i4=n;i4>=1;i4--)
     for (j4=f;j4>=1;j4--)
     if (h4==true)
     if (!(a[i4][j4]==a[j4][i4]))
     {
       h4=false;    
     }
     if (h4==true)
     cout<<"Matrix is simetric diagonally (2) !n";
     else
     cout<<"Matrix is not simetric diagonally (2) !n";
     cin.get();
     cin.get();
   }

 

]]>
Queens on Chessboard https://pro9ramming.com/queens-on-chessboard/ Wed, 15 Oct 2014 17:43:27 +0000 http://pro9ramming.com/blog/?p=20 Continue reading Queens on Chessboard]]> Write a program that will generate matrix of zeros and ones where ones are queens and zeros empty fields. You need to make matrix (nxn) with n queens so they don’t attack each other (every column and row must have exactly one queen + every diagonal).
Input
From standard input in one line enter 3<n<15.
For 3 there is no solution, but for 4 there is one solution.
Output
Matrix nxn with ones as queens and zeros as empty fields !
Solution
Problem can be solved by using recursion (permutations) and diagonal check.
So here is the solution in TPW:

program queens_recursion;
uses wincrt;
var
  a:array[1..1000] of integer;
  n:longint;
procedure output;
var
  i,j:integer;
begin
for i:=1 to n do
   begin
   writeln;
   for j:=1 to n do
     if j=a[i] then
       write('1 ')
     else
       write('0 ');
  end;
  writeln;
end;

function attack_check(k:integer):boolean;
var
  i:integer;
  b:boolean;
begin
  b:=false;
  for i:=1 to k-1 do
    if b=false then
      if a[i]=a[k] then
        b:=true;
if b=true then
   attack_check:=false
else
   attack_check:=true;
end;

function diagonal_check:boolean;
var
  i,j:integer;
  b:boolean;
  function one_case(x1,x2,y1,y2:integer):boolean;
  begin
    if abs(x1-y1)=abs(x2-y2) then
      one_case:=true
    else
      one_case:=false;
  end;
begin
  b:=false;
  for i:=1 to n do
    for j:=1 to n do
      if i<>j then
        if b=false then
          if one_case(i,a[i],j,a[j]) then
            b:=true;
  if b=true then
    diagonal_check:=false
  else
    diagonal_check:=true;
end;
    
procedure recursion(index:integer);
var
   i:integer;
begin
  if index>n then
  begin
    if diagonal_check then
      begin
        output;
        writeln('Press Enter if you want another solution !');
        readln;
        clrscr;
      end;

    end
  else
    begin
     for i:=1 to n do
      begin
       a[index]:=i;
        if attack_check(index) then    
          recursion(index+1);
       end; 
    end;
end;
begin
writeln('Enter n for chess board (nxn) !');
readln(n);
clrscr;
recursion(1);
end.

So here is the code in C++:

#include <iostream>
#include <cstdio>

using namespace std;

int n;
int a [100];

int output()
{
  int i,j;
for (i=1;i<=n;i++) 
   {
   cout<<endl;
    for (j=1;j<=n;j++) 
     if (j == a[i])
       cout<<"1 ";
     else
       cout<<"0 ";
  }
  cout<<endl;
}
int abs(int a)
{
    if (a<0)
      return -a;
    else
      return a;
}

bool one_case(int x1,int x2,int y1,int y2)
{
    if (abs(x1-y1) == abs(x2-y2)) 
      return true;
    else
      return false;
}

bool diagonal_check()
{
  int i,j;
  for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
     if (i>j | i<j) 
       if (one_case(i,a[i],j,a[j]))
          return false;
    return true;
}
    
bool checking_attack(int k)
{
     int i;
     bool b;
     b=false;
     for (i=1;i<=k-1;i++)
      if (b == false)
       if(a[i] == a[k])
        b=true;
     if (b == true)
       return false;
     else
       return true;
}

int recursion(int index)
{
    if (index>n)
    {
      if (diagonal_check())
      {
         output();
         cout<<endl;
         cout<<"Press Enter if you want another solution !"<<endl;
         cin.get();
     system("cls"); 
      }
    }
    else
        {
           int i;
          for (i=1;i<=n;i++)
          {
              a[index]=i;
              if (checking_attack(index) == true)
              recursion(index+1);
              }
        }
    }
int main()
{
    cout<<"Enter n !\n";
    cin>>n;
    int i;
    system("cls");
    recursion(1);
    system("pause");
}

 

For both solutions (in C++ and Pascal), just hold Enter and it will generate solutions rapidly.

]]>
Numbers in a row https://pro9ramming.com/numbers-in-a-row/ Tue, 07 Oct 2014 12:43:27 +0000 http://pro9ramming.com/blog/?p=101 Continue reading Numbers in a row]]> If sequence of numbers, like this, is given:

1101001000100001000001000000

As you can see those are powers of 10 starting with :
10^0=1
10^1=10
10^2=100
and so on . . .
You need to find on which position of that sequence is 0 or 1.
For example if input is 1 then output is 1 and if input is 3 then output is 0.
Input
First input number of test cases 0<n<1000 and in n lines read different cases.
Output
For each test case output a line with solution (1 or 0)
Sample Input
8
1
2
3
4
5
6
16
301
Sample Output
1
1
0
1
0
0
1
1
Solution
One repeat loop that goes from 1 till test case.
Here is the code in Pascal :

program numbers_in_row;
uses wincrt;
var
n,i:longint;
a:array[1..1000] of integer;
function find(n:longint):integer;
var
  i,k:longint;
begin
if n=1 then
  find:=1
else
  begin
   i:=1;
   k:=0;
  repeat
   begin
     k:=k+1;
     i:=i+k;
   end;
  until i>=n;
    if i=n then
      find:=1
    else
      find:=0;
  end;
end;
begin
readln(n);
  for i:=1 to n do
   readln(a[i]);
for i:=1 to n do
  writeln(find(a[i]));
end.

C++:

#include <iostream>
#include <cstdio>

using namespace std;

int n,i;
int a [1000];

int find(int n)
{
int i,k;

if (n == 1) 
  return 1;
else
  {
   i=1;
   k=0;
  while (i < n)
    {
     k++;
     i+=k;
    }
    if (i == n) 
      return 1;
    else
      return 0;
  }
}

int main()
{
cin>>n;
  for (i=1;i<=n;i++)
   cin>>a[i];
  for (i=1;i<=n;i++)
  cout<<find(a[i])<<endl;
  system("pause");
}

 

]]>
How many times digit are in the number https://pro9ramming.com/how-many-times-digit-are-in-the-number/ Tue, 07 Oct 2014 02:43:27 +0000 http://pro9ramming.com/blog/?p=90 Continue reading How many times digit are in the number]]> The task is to check how many times digit “k” is in number “n”.
For example number n is 123 and k is 2, k is only once in 123 and
if “n” is 334 and “k” is 3 then it is twice.
Input
Number n and one-digit number k from two separate lines.
Output
One line which represents solution.
Sample Input
8221
1
Sample Output
1
Now the code explanation. The task is to separate digits and to check how many times one digit “k” appears in that set of digits.
Algorithm for separating digits, is explained here.

Code in Pascal:

program num_of_digits;
uses wincrt;
var
  i,n,k:longint;
begin
  readln(n);
  readln(k);
  i:=0;
  repeat
    begin
      if n mod 10 = k then
        i:=i+1;
      n:=n div 10;
    end;
  until (n=0);
  writeln(i);
end.

C++:

# include <iostream>
# include <cstdio>

using namespace std;

int main()
{
    int i,n,k;
    cin>>n;
    cin>>k;
    i=0;
    while (n > 0)
    {
        if (n % 10 == k)
         i++;
         n=n/10;
    }
    cout<<i<<endl;
    system("pause");
}

 

]]>
Perfect numbers https://pro9ramming.com/perfect-numbers-2/ Tue, 16 Sep 2014 06:43:27 +0000 http://pro9ramming.com/blog/?p=28 Continue reading Perfect numbers]]> Number n>1 is perfect if sum of all it’s positive divisors (including 1 and n) equals 2n. Competitors of math with little experience wouldnt be hard to solve a task using well known fact, every prime number bigger then 3 we can write down as 6k-1 or 6k+1 for some natural number k. However, there is an other solution. It requires some higher knowing of theory of numbers, but it is interesting, because it shows connection between this task and some famous unsolved problems of theory of numbers.

A little of history. Perfect numbers are very old in math. It were developed by the ancient greeks, and they were very easy counted few first represents of it… 6, 28, 496, 8128….

Some logical questions are:

1.) Is there some basic formula for calculating all perfect numbers?
2.) Is there uncountable amount of that numbers?
3.) First few perfect numbers are even… Are they all even?

Particular answer on the first question was found in the ancient time. Euklid formed next theorem : If p and 2^p – 1 are prime numbers, then Sp = 2^p-1((2^p)-1).

Proof of this theorem isnt so hard… It is enough to notice that all positive divisors of number Sp : 1,2,2^2, … , 2^p-1,

1((2^p)-1), 2((2^p)-1), (2^2)((2^p)-1), … , (2^p-1)((2^p)-1).

sum of divisors from the first line we can easy count as

1+2+2^2+…+2^p-1=(2^p)-1

sum of divisors in the second line is equal
((2^p-1)-1)(1+2+2^2+ … + 2^p-1) = (2^p – 1)(2^p – 1)

So whole sum of all divisors is
(2^p – 1) + (2^p -1)(2^p -1) = (2^p – 1)(1 + 2^p -1) = 2×2^p(2^p – 1) = 2Sp

Conclusion : Number Sp is a perfect number!

Is with given formula found all of even perfect numbers?

Old Greeks believed that is correct, but they did not succeed do proove it. That speculation is, however, correct, and first prove of that gathered Swis scientist Oyler in the 18th century.

Theoreme 2 :

Every even perfect number can be writed in shape :
Sp = (2^p-1)((2^p – 1))
where p and (2^p) – 1 are prime.

Table of first 4 perfect numbers

p 2^p – 1 Sp
2 3 6
3 7 28
5 31 496
7 127 8128

Next prime number (11) is not giving perfect number cos 2^11 – 1 = 2047 = 23×89… Sp = 2096128. So, for some prime numbers p number Sp is perfect, and for some it is not. Further tries indicates that for big prime numbers p perfect numbers are less. Until today it is found only 44 perfect numbers! The bigest of them is explored in spetember 2006 and have over 19 million digits!

and what is with eventual odd perfect numbers?

Here we have big lack of information. Til now on several different ways are checked all odd numbers with less then 300 digits , and all of them which have at least 1 prime divisor with less then 20 digits.They didn’t found not one perfect number?! O.o Again, most of scientists that this number isnt exist (answer on upper question is negative) but without of proves… It is one more very old and open math problem…

formula_perfect_numbers

And it is known that you can generate perfect numbers using this formula and this sequence of primes :

2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 
521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 
9689, 9941, 11213, 19937, 21701, 23209, 44497, 
86243, 110503, 132049, 216091, 756839, 859433, 
1257787, 1398269, 2976221, 3021377, 6972593, 13466917

Biggest perfect number is explored in 2006 and have over 19 million digits, and for primes it is around 10^6 digits.
Here is the code in TPW (with conjencture that all perfect numbers are even):

program perfect_numbers;
uses wincrt;
var
i,n:longint;
function is_perfect(a:longint):boolean;
var
  i,s:longint;
begin
  s:=0;
  for i:=1 to a do
    if a mod i = 0 then
      s:=s+i;
  if s div 2 = a then
    is_perfect:=true
  else
    is_perfect:=false;
end;
begin
readln(n);
  for i:=2 to n do
    if not odd(i) then
      if is_perfect(i) then
        writeln(i);
end.

Solution in C by using “unsigned long long”, the biggest integer built in data type in C or C++ programming language:

# include <stdio.h>

short is_perfect(unsigned long long a)
{
  unsigned long long i,s;
  s=0;
  for (i=1;i<=a;i++)
    if (a % i == 0) 
      s+=i;
  if (s == a*2) 
    return 1;
  else
    return 0;
}

int main()
{
   unsigned long long i,n;
   printf("Enter to which num you want perfect numbers : \n");
   scanf("%d",&n);
   for (i=2;i<=n;i++)
     if (i % 2 == 0 && is_perfect(i)==1)
         printf("%d\n",i); 
   scanf("%d");    
   return 0; 
}

 

]]>
The Knights of the Round Table https://pro9ramming.com/the-knights-of-the-round-table/ Fri, 08 Aug 2014 09:43:27 +0000 http://pro9ramming.com/blog/?p=279 Continue reading The Knights of the Round Table]]> Task

King Arthur is planning to build the round table in a room which has a triangular window in the ceiling. He wants the sun to shine on his round table. In particular, he wants the table to be totally in the sunlight when the sun is directly overhead at noon. Thus the table must be built in a particular triangular region of the room. Of course, the king wants to build the largest possible table under the circumstances. As Merlin is out to lunch, write a program which finds the radius of the largest circular table that fits in the sunlit area.
Input
There will be an arbitrary number of test cases, each represented by three real numbers (a, b, and c), which stand for the side lengths of the triangular region. No side length will be greater than 1,000,000, and you may assume that max(a, b, c) ≤ (a + b + c)/2. You must read until you reach the end of the file.
Output
For each room configuration read, you must print the following line:

The radius of the round table is: r

where r is the radius of the largest round table that fits in the sunlit area, rounded to three decimal digits.
Sample Input
12.0 12.0 8.0
Sample Output
The radius of the round table is: 2.828
Solution
This is not much of an algorithm, but it is more about check of mathematical skills, preferable geometry.
To find solution to this problem, you need to know few formulas about triangle, firstly we need a formula to get “r” – half diameter or radius of circle inside, like in the picture:

tri_1

Formula for that is:

tri_4

Where P is area of triangle and s is semi-perimeter of the triangle.
We have 3 sides of triangle and we need to find area, and we need to use Heron’s formula:

tri_3

And semi-perimeter is:

tri_2

So you can add condition if (s>a) and (s>b) and (s>c), because then triangle is possible, but task says that those examples will not be included.
So here is the code in TPW:

program the_knights_of_the_round_table;
uses wincrt;
var
  p,s,a,b,c,r:real;
begin
readln(a,b,c);
s:=(a+b+c)/2;
p:=sqrt(s*(s-a)*(s-b)*(s-c));
r:=p/s;
writeln('The radius of the round table is: ',r:8:3);
end.

C++:

# include <iostream>
# include <cstdio>
# include <math.h>

using namespace std;

int main()
{
  float p,s,a,b,c,r;
  cin>>a>>b>>c;
  s=(a+b+c)/2;
  p=sqrt(s*(s-a)*(s-b)*(s-c));
  r=p/s;
   printf ("The radius of the round table is: %8.3f \n", r);
  system("pause");
}

 

]]>