ECPC10C - Editorial

PROBLEM LINK: Cake and Precision

Author: Vaibhav Chauhan
Tester: Sumeet Pachauri
Editorialist: Vaibhav Chauham

Difficulty:

Easy

Prerequisites:

Strings,functions

Explanation:

As we know that in each minute cake gets double of it’s previous size so let’s say cake was fully baked at t seconds so at t-1 seconds it was half baked because at every second it doubles it’s size and baking is directly proportional to size.
For example- let cake is half baked in 4 seconds, so in next one second it will double it’s size and also it is fully baked now in 5 seconds.
As our task is to add 2 seconds to half baked cake so we will print 6 in our example.

General

Generally we can say that we have to take an integer n , and print n+1.

Solution:

Setter's Solution C++
#include <bits/stdc++.h>

using namespace std;

string answer_puzzle(string number)
{
	int carry = 0;
	
	//First add 1 to the last digit
	int digit = number[number.size() - 1] - '0' + 1;
	carry = digit/10;
	digit = digit % 10;
	number[number.size() - 1] = digit + '0';
	
	int i = number.size() - 2;
	for(; i >= 0 ; --i )
	{
		if ( carry == 0 )
			break;
		digit = number[i] - '0' + digit + carry;
		carry = digit/10;
		digit = digit % 10;
		
		number[i] = digit + '0';
	}
	
	string result;
	if ( carry )
	{
		char carry_char = carry + '0';
		result.push_back(carry_char);
	}
	
	result += number;
	
	
	return result;
}

int main()
{
	int T;
	cin >>T;
	
	while( T-- )
	{
		string a;
		cin >> a;
		
		cout << answer_puzzle(a) << endl;
		
	}
}
Setter's Solution Python
t = (int)(input())
for i in range(t):
        s = (int)(input())
        s = s + 1
        print(s)

Please give me suggestions if anything is unclear so that I can improve. Thanks :slight_smile:

1 Like