CSES - Datatähti 2020 alku - Results
Submission details
Task:Lukuvälit
Sender:ArturT
Submission time:2019-10-04 02:28:56 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int calc(int, bool)':
input/code.cpp:31:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i <= inputStr.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:37:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int b = 0; b < inputStr.size()-i+1; b++) {
                     ~~^~~~~~~~~~~~~~~~~~~~~
input/code.cpp:43:21: error: 'pow' was not declared in this scope
    rounded += cur * pow(10, zeroes - i);
                     ^~~
input/code.cpp:43:21: note: suggested alternative: 'putw'
    rounded += cur * pow(10, zeroes - i);
                     ^~~
                     putw
input/code.cpp:50:13: error: 'pow' was not declared in this scope
     temp /= pow(10, i+1);
             ^~~
input/code.cpp:50:13: note: suggested alternative: 'putw'
     temp /= pow(10, i+1);
             ^~~
             putw
input/code.cpp:27:6: wa...

Code

#include<iostream>
#include<vector>
#include <string> 
using namespace std;

int btd(int n)
{
	int num = n;
	int dec_value = 0;

	int base = 1;

	int temp = num;
	while (temp) {
		int last_digit = temp % 10;
		temp = temp / 10;

		dec_value += last_digit * base;

		base = base * 2;
	}

	return dec_value;
}
	
int calc(int input, bool roundUp) {
	int result = 0;
	string inputStr = to_string(input);
	int zeroes = inputStr.size() - 1;
	int rounded = 0;
	for (int i = 0; i <= inputStr.size() - 1; i++) {
		
		if (!roundUp) {
			char curChar = inputStr[i];
			int cur = (int)curChar - 48;
			if (cur !=1 && cur > 0) {  // eg. 200 ->  111
				for (int b = 0; b < inputStr.size()-i+1; b++) {
					inputStr[inputStr.size()-b] = 1+48;
					cur = 1;
				}
				
			}
			rounded += cur * pow(10, zeroes - i);
		}
		else {
			char curChar = inputStr[inputStr.size()-i-1];
			int cur = (int)curChar - 48;
			if (cur > 1) {
				int temp = input;
				temp /= pow(10, i+1);
				temp *= pow(10, i+1);
				temp = temp + pow(10, i+1);
				rounded = temp;
			}
		}
	}
	return rounded;
}



int main()
{
	int rows = 1;
	cin >> rows;
	vector< vector<int> >  inputs;
	

	for (int i = 0; i < rows; i++) {
		int input1=0;
		int input2=0;

		cin >> input1;
		cin >> input2;
		vector<int> input = { input1, input2 };
		inputs.push_back(input);
	}
	for (int i = 0; i < rows; i++) {
		int result = 0;
		
		int roundedA = calc(inputs[i][0], true);
		int roundedB = calc(inputs[i][1], false);
		int a = btd(roundedA);
		a = a-1;
		//a--;
		int b = btd(roundedB);
		if (a<0) {
			a = 1;
		}
		result = b-a;
		
		cout << endl << result;
	}
}