Day 46 Learning C && Python: Practice Problems

jay.dez
4 min readJan 18, 2023

--

Problem 1. Factorial

Difficulty Rating: 878

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.

The technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called “Traveling Salesman Problem” and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product:

1.2.3.4….N. The number is very high even for a relatively small N.

The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.

For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1​<N2​ then Z(N1​) ≤ Z(N2​). It is because we can never “lose” any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.

Input:

There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 ≤ N ≤ 10⁹¹.

Output:

For every number N, output a single line containing the single non-negative integer Z(N).

Classic C

I’ve skipped this one for a while because it was intimidating. But in the end, through all that problems mumbo-jumbo cell tower story, the question is — how many zeros are at the end of the factorial of a number?

What’s a factorial of a number? The factorial of 7 is 5040. To get that we multiply every number before seven by the number after it so 1 * 2 * 3 * 4 * 5 * 6 * 7. The trick is to notice a pattern which is after every 5 is an even number and 5 multiplied by any even number will produce a product (answer) that ends in zero. If you do five divided by whatever N is, this shortcut will give give us how many fives are in N aka zeros. However, a number like 26 will have six zeros even though five goes into it five times. That simple way of finding the zeros doesn’t account for that in the last factorial of 26, is 25 x 26 — there are two five’s in 25 (5 x 5) so we divide by 5 and if that five is greater than or equal to five, we divide again to get the 6th and final zero.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int n, count = 0;
scanf("%d", &n);
while(n!=0)
{
count += n/5;
n/= 5;
}
printf("%d\n", count);

}
return 0;
}

Python

Couldn’t do while(int(input()) != 0 because then it would always ask for input so had to separate those. Also had to use // to round down because then the output would be too high. Used int as well or else you’d get decimals.

for t in range(int(input())): 
count = 0
n = int(input())
while(n!=0):
count += n//5
n//= 5
print(int(count))

Problem 2. Peaceful Party

Difficulty Rating: 898

The mayor of your city has decided to throw a party to gather the favor of his people in different regions of the city.

There are 3 distinct regions in the city namely A, B, C comprising of PA​, PB​ and PC​ number of people respectively.

However, the mayor knows that people of the region B are in conflict with people of regions A and C. So, there will be a conflict if people from region B are present at the party along with people from region A or C.

The mayor wants to invite as many people as possible and also avoid any conflicts. Help him invite maximum number of people to the party.

Input Format

  • The first line contains a single integer T — the number of test cases. Then the test cases follow.
  • The first line of each test case contains three integers PA​, PB​ and PC​ — number of people living in regions A, B and C respectively.

Output Format

For each test case, output the maximum number of people that can be invited to the party.

Classic C

Fairly self explanatory. Dude wants to invite the most people and B is a pain in the ass. So if peeps from A and C are greater in numbers than B, invite accordingly.

#include <stdio.h>

int main(void)
{
int t;
scanf("%d", &t);
while(t--)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a + c >= b)
printf("%d\n", a + c);
else
printf("%d\n", b);
}
return 0;
}

Python

for t in range(int(input())):
a, b, c = map(int, input().split())
print(a + c if(a + c >= b) else b)

--

--