Question

In: Computer Science

Lily likes to play games with integers. She has created a new game where she determines...

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):

  • i: the starting day number
  • j: the ending day number
  • k: the divisor

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;

}

Solutions

Expert Solution

#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.


Related Solutions

   Fred is a teenager who likes to listen to music and to play games on...
   Fred is a teenager who likes to listen to music and to play games on his computer. Suppose that he has an annual entertainment budget of $144 and that each music CD costs $12 and each computer game costs $24. Quantity Consumed                   Total Utility                    Total Utility      (per year)                             of Music CDs                of Computer Games             0                                           0                                     0             1                                       200                                 160             2                                       236                                 232             3                                       268                                 296...
Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
1. One of the most popular games ever created is called the Ultimatum Game. In this...
1. One of the most popular games ever created is called the Ultimatum Game. In this game, there are two players: proposer and responder. The proposer moves first and is given $10 to split. Suppose they can offer any integer amount from 0,1,2,...,10. The responder observes this offer and has two choices: accept or reject. If the responder accepts, the proposed offer goes through. If the responder rejects, both players receive $0. What is the Nash Equilibrium in this game?...
1. One of the most popular games ever created is called the Ultimatum Game. In this...
1. One of the most popular games ever created is called the Ultimatum Game. In this game, there are two players: proposer and responder. The proposer moves first and is given $10 to split. Suppose they can offer any integer amount from 0,1,2,...,10. The responder observes this offer and has two choices: accept or reject. If the responder accepts, the proposed offer goes through. If the responder rejects, both players receive $0. What is the Nash Equilibrium in this game?...
Morry Games developed a new play station called AWESOME. Morry Games has a standard cost system...
Morry Games developed a new play station called AWESOME. Morry Games has a standard cost system to help control its costs. The following standards have been established for AWESOME. Direct materials 15 grams per AWESOME. Standard direct material cost per AWESOME is $10. Direct labour 1.2 hours per AWESOME. Standard cost of direct labour is $14.40 per AWESOME. During May the company produced 9,000 AWESOME units. Production data for May are: Direct materials: 165,000 grams were purchased at a cost...
Stupendous Games developed a new play station called StupendouS. Stupendous Games has a standard cost system...
Stupendous Games developed a new play station called StupendouS. Stupendous Games has a standard cost system to help control its costs. The following standards have been established for StupendouS. • Direct materials 15 grams per StupendouS. • Standard direct material cost per StupendouS is $11. • Direct labour 1.2 hours per StupendouS. • Standard cost of direct labour is $15.00 per StupendouS. During October the company produced 9,000 StupendouS units. Production data for October are: • Direct materials: 165,000 grams...
You are to play three games. In the first game, you draw a card, and you...
You are to play three games. In the first game, you draw a card, and you win if the card is a heart. In the second game, you toss two coins, and you win if one head and one tail are shown. In the third game, two dice are rolled and you win if the sum of the dice is 7 or 11. What is the probability that you win all three games? What is the probability that you win...
Diane has decided to play the following game of chance. She places a $1 bet on...
Diane has decided to play the following game of chance. She places a $1 bet on each repeated play of the game in which the probability of her winning $1 is .6. She has further decided to continue playing the game until she has either accumulated a total of $3 or has lost all her money. What is the probability that Diane will eventually leave the game a winner if she started with a capital of $1? Of $2? capital...
Lorena likes to play golf. The number of times per year that she plays depends on...
Lorena likes to play golf. The number of times per year that she plays depends on both the price of playing a round of golf as well as Lorena’s income and the cost of other types of entertainment—in particular, how much it costs to go see a movie instead of playing golf. The three demand schedules in the table below show how many rounds of golf per year Lorena will demand at each price under three different scenarios. In scenario...
Lorena likes to play golf. The number of times per year that she plays depends on...
Lorena likes to play golf. The number of times per year that she plays depends on both the price of playing a round of golf as well as Lorena’s income and the cost of other types of entertainment—in particular, how much it costs to go see a movie instead of playing golf. The three demand schedules in the table below show how many rounds of golf per year Lorena will demand at each price under three different scenarios. In scenario...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT