In: Computer Science
Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse. For instance, given the number 12, its reverse is 21. Their difference is 9. The number 120 reversed is 21, and their difference is 99.
She decides to apply her game to decision making. She will look at a numbered range of days and will only go to a movie on a beautiful day.
Given a range of numbered days, [i....j] and a number k, determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where |i - reverse(i)| is evenly divisible by k. If a day's value is a beautiful number, it is a beautiful day. Print the number of beautiful days in the range.
Function Description
Complete the beautifulDays function in the editor below. It must return the number of beautiful days in the range.
beautifulDays has the following parameter(s):
Input Format
A single line of three space-separated integers describing the respective values of
i, j , and k.
.
Constraints
Output Format
Print the number of beautiful days in the inclusive range between
and
.
Sample Input
20 23 6
Sample Output
2
Code
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
// Complete the beautifulDays function below.
int beautifulDays(int i, int j, int k) {
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string ijk_temp;
getline(cin, ijk_temp);
vector<string> ijk = split_string(ijk_temp);
int i = stoi(ijk[0]);
int j = stoi(ijk[1]);
int k = stoi(ijk[2]);
int result = beautifulDays(i, j, k);
fout << result << "\n";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
#include <iostream> #include <bits/stdc++.h> #include <fstream> #include <cstdlib> #include <string> #include <vector> using namespace std; vector<string> split_string(string); // Complete the beautifulDays function below. // Helper method which tells what is the difference // in original and reverse number int getReverseDiff(int x) { int originalVal = x; int reverseVal = 0; while(x != 0) { reverseVal = reverseVal * 10 + x % 10; x = x/10; } return abs(originalVal - reverseVal); } int beautifulDays(int i, int j, int k) { int count = 0; for(int n=i; n<=j; n++) { if(getReverseDiff(n) % k == 0) { count++; } } return count; } int main() { ofstream fout(getenv("OUTPUT_PATH")); string ijk_temp; getline(cin, ijk_temp); vector<string> ijk = split_string(ijk_temp); int i = stoi(ijk[0]); int j = stoi(ijk[1]); int k = stoi(ijk[2]); int result = beautifulDays(i, j, k); fout << result << "\n"; fout.close(); return 0; } vector<string> split_string(string input_string) { string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { return x == y and x == ' '; }); input_string.erase(new_end, input_string.end()); while (input_string[input_string.length() - 1] == ' ') { input_string.pop_back(); } vector<string> splits; char delimiter = ' '; size_t i = 0; size_t pos = input_string.find(delimiter); while (pos != string::npos) { splits.push_back(input_string.substr(i, pos - i)); i = pos + 1; pos = input_string.find(delimiter, i); } splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); return splits; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.