Verifying 116725 = 116 * 725 or not? C/C++ [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Question: input an int number ex: ABCD, verify that is ABCD = AB*CD or not
(note that we don't how many digits this number got, just know it is an positive integer
and yes, it the number of digits is odd we can conclude result is No, immediately by... eye lol)
for example:
Enter a number: 88
Output: No
Enter a number: 12600
Output: No
Enter a number: 116725
Output: Yes, 116 * 725 = 116725 (**this is just example,
not actual result, just help to understand how output look like**)
Problem is that, you can't use array, jump and bitwise in order to solve this. Yes, if we can use array then not much to say, put input number in an array, check first half multiply other half....bla..bla... I need help with IDEA without using array to solve this problem, right now I'm stuck! Thank you guy very much!

Your program can safely output No for every input. Proof:
You are looking for integers A and B such that A*B = A*10^k + B, with A and B > 0 and B < 10^k.
If A*B = A*10^k + B, then B = 10^k + B/A > 10^k. But B had to be less than 10^k, so this is a contradiction. Therefore no such A and B exist.
A longer proof:
You are looking for integers A and B such that A*B = A*10^k + B, with A and B > 0 and B < 10^k.
Subtract B from both sides to get (A-1)*B = A*10^k.
Since A is a factor in the right hand side it is also a factor in the left hand side. But A and A-1 are coprime, so A must divide B. So, B = n*A for some integer n.
Now we have A*B = A*10^k + n*A, or A*B = (10^k + n)*A. Since A > 0 we can divide both sides by A to get B = 10^k+n. But this is impossible since B was supposed to be less than 10^k!

A little hint to your 6 digits number:
to get last 3 digits use % 1000
to get first 3 digits use (int) X/1000.
Notice that 1000 == 10^3.

Write a program that asks for the input and then prints "No".
Done.

It's not too clear what you're trying to do. ABCD == AB*CD suggests
four digit numbers. For a four digit number x, a test for the above
would be x == (x / 100) * (x % 100). For a six digit number, replace
100 with 1000, and more generally, for an n digit number, where
n is even, use 10^(n/2). If n is odd, however, I'm not sure what you're looking for, and the last example you give doesn't meet the criteria you mention; if you can permutate the digits in each half, the problem then becomes more complex.

You can read a string, split it in the middle and convert the both parts to an int.
You could also read an int, calculate the number of digits (writing your own loop or using the log function) and split the int after the calculated number of digits.
You can also write a loop that is taking an int ABCD, splitting it into ABC and D and moving the digits from ABC to D while the both have not the same number of digits (you do not need to calculate the number of digits here, there is a quite easy comparison you can do).

To get the number of digits, count how many times you had to divide the number by 10 until it gets smaller than 1.
For 116725, you would need to divide by 10 six times. After that you can print no if the number is odd or calculate the result like James Kanze and ProblemFactory described.

Here's a complete "C/C++" solution:
#include <stdio.h>
int main(void) {
while (fgetc(stdin) != '\n');
return puts("No.");
}

Related

How to round numbers in range after division (10.25 to 11)? [duplicate]

This question already has answers here:
Rounding up and down a number C++
(5 answers)
Closed 4 years ago.
For a school project I need to calculate the leap years. The problem is, I need to round the int.
For example someone is born in 1961.
double yearsbetween; yearsbetween = birthyear - 1918;
double leapyears; leapyears = ( yearsbetween / 4 );
If birthyear = 1961 I get leapyears = 10.75 but that is wrong.. I need to be 11.
If an answer is 10.25 it need to be rounded to 10. If an answer is 10.75 it need to be rounded to 11.
Can someone help me ?
you have to specifically look at each year to check for leap years. your assumption of every 4th year as leap is wrong, as there are some exceptions:
it is not a leap year, if it is divisible by 100 and not divisible by 400
examples:
1900 is not a leap year
2000 is a leap year
see https://en.wikipedia.org/wiki/Leap_year#Algorithm
Suppose we are working only in a period in which the leap years are those years, and only those years, that are divisible by four.
Given a number n, the number of positive numbers less than or equal to n that are multiples of four is floor(n/4). Let L(n) be this value, floor(n/4).
If we take a number m, the number of positive multiples of four up to m is L(m). When we subtract L(n), the result, L(m)−L(n), is the number of multiples of four greater than n but less than or equal to m.
Therefore, the number of leap years between year 1918 and year m is floor(m/4) − floor(1918/4) = floor(m/4) − 479. (If the base year were a leap year and we wanted to include it inside the period instead of outside, we could replace L(n) with L(n−1).
The above is easily extendable to the larger leap year pattern. By changing L(n) to floor(n/4) − floor(n/100) + floor(n/400), it becomes a count of positive numbers up to n that are multiples of four but are not multiples of 100 unless they are multiples of 400. Then L(m) − L(n) is the number of leap rules between n and m.
Thus a proper formula for the period covered by this rule is floor(n/4) − floor(n/100) + floor(n/400) − 464. Using C’s integer arithmetic, this is easily evaluated as n/4 - n/100 + n/400 - 464.
Seems like what you need is std::ceil
You also have in the std::ceil reference a link to other rounding functions, I recommend
going through those also.
So this isn't relevant until c++20 and I even had to ask a question to figure out how to use chrono::year. But the best solution for this, if available, would be something like:
const chrono::year birthyear{ 1961 };
auto leapyears = 0;
for(chrono::year i{ 1918 }; i <= birthyear; ++i) {
if(i.is_leap()) {
++leapyears;
}
}
I've linked a live example by forking #HowardHinnant's example from this answer.

Can't understand the proof behind the use of this map<> method

Drazil is playing a math game with Varda.
Let's define for positive integer x as a product of factorials of its
digits. For example, f(135) = 1! * 3! * 5! = 720.
First, they choose a decimal number a consisting of n digits that
contains at least one digit larger than 1. This number may possibly
start with leading zeroes. Then they should find maximum positive
number x satisfying following two conditions:
x doesn't contain neither digit 0 nor digit 1.
= f(x) = f(a)
Help friends find such number.
Input The first line contains an integer n (1 ≤ n ≤ 15) — the number
of digits in a.
The second line contains n digits of a. There is at least one digit in
a that is larger than 1. Number a may possibly contain leading zeroes.
Output Output a maximum possible integer satisfying the conditions
above. There should be no zeroes and ones in this number decimal
representation.
Examples
input
4
1234
output
33222
input
3
555
output
555
Here is the solution,
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
map<char, string> mp;
mp['0'] = mp['1'] = "";
mp['2'] = "2";
mp['3'] = "3";
mp['4'] = "223";
mp['5'] = "5";
mp['6'] = "35";
mp['7'] = "7";
mp['8'] = "2227";
mp['9'] = "2337";
int n;
string str;
cin>>n>>str;
string res;
for(int i = 0; i < str.size(); ++i)
res += mp[str[i]];
sort(res.rbegin(), res.rend());
cout<<res;
return 0;
}
I'd like if someone explains the reason why were the digits transformed into other form of digits rather than just with some way to compute the number with..sadly brute force would give a TLE(Time limit exceeded) in this question cause of the 15 digit thing so that's a big number to brute force to,so I kindly hope that someone can explain the "proof" below, cause idk what theory says that these numbers can be transformed to those numbers for example 4 to 223 and stuff.
Thanks in advance.
Picture: What the proof says
The theory behind these transformations is the following (Ill use 4 as an example):
4! = 3! * 2! * 2!
A longer sequence of digits will always produce a larger number than a shorter sequence (at least for positive integers). Thus this code produces a longer sequence where possible. With the above example we get:
4! = 3! * 4
We can't reduce the 3! any further, since 3 is a prime. 4 on the other hand is simply 2²:
4 = 2² = 2! * 2!
Thus we have found the optimal replacement for 4 in the number-sequence as "322". This can be done for all numbers, but prime-numbers aren't factorisable and will thus always be the best replacement available for them self.
And thanks to the fact that we're using prime factorization we also know that we have the only (and longest possible) string of digits that can replace a certain digit.

Translation of number into strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to know whether there is a way in which we can print the number alphabetically i.e
123 should be printed as one two three.
The only condition is that we should not reverse the number and we should not use array.
I only know these two ways:
"Reverse the number", that is, taking the last digit and cutting it off. For each cut-off digit, one can use an array to look up the correct string.
using switch and a lot of cases
Any ideas?
for hundreds place:
int hundreds = my_num / 100 //Needs "/", NOT "%"
if(hundreds == 0)
cout << "zero";
else if(hundreds == 1)
cout << "one";
//repeat for 2-9
This process could be tweaked to do the other digits as well. It is also worth mentioning that the if/else block a) could be done with a switch/case if preferred, and b) could pretty easily be made into a separate function to avoid having to repeat the block of code over and over, I just wrote out as much as I did for clarity's sake. Note that this assumes the number you're "translating" is an integer. With integers the "/" operator will return the full quotient WITHOUT the remainder, e.g. 123 / 100 = 1, not 1.23
Not necessarily the easiest route, but you can make a function, say DigitToWord which will take a digit 0, 1, 2, ...etc to its word with a switch statement. Then I recommend using a for loop over the number, continuously dividing by 10 and taking the mod for the loop:
int num; //my number i want to print
int div = pow(10, (int)log10(num)); //find the largest power of 10 smaller than num
while(num > 0) {
int remainder = num%div;
int digit = num/div;
DigitToWord();
num = remainder;
}

Why does it crash? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Mr. Little Z is looking at a piece of paper and unsuccessfully trying to find the square root of a number written on the paper.
Help Mr. Little Z find the number B which is the square root of BIG number A. The number A has 1000 digits at most and the square root of A will always be an integer.
INPUT:
The first line of the standard input contains the number 0
OUTPUT:
To the standard output write number M, where M represents the length of number B (where B is the square-root of the number A) and in the next M lines write the digits of the number B (from the most significant digit to the least significant).
Input:
3
6
2
5
Output:
2
2
5
Explanation:
The number written on the paper was 625, and its square-root is 25.
My Code :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,m,k,i;
int niza[1001];
cin>>m;
for(int i=0;i<m;i++)
cin>>niza[i];
niza[i]=k;
cout<<sqrt(k);
return 0;
}
Most likely this is because you never set k, and the random value it contains at the start of the program is negative, so sqrt(k) crashes. Also, if m is entered as greater than 1000, you will go outside the array bounds.
Your program will crash at: niza[i] = k because i is past the end of the array.
k is not initialized. You are outputting the square root of an uninitialized variable.
Like #Vilx- said, you will have to check the value of m before your loop.
Also, the sqrt function does not work on an array of numbers, so you will have to come up with a different method to calculate square root based on digits.
You will need to check the status of cin after cin >> m and cin >> niza[i]. Users can and will enter anything, such as letters, which cause failure when a number is expected.
Also, try putting spaces between operators and function names. They don't add anything to the size or speed of an executable, but make reading and maintenance easier.

Creating a histogram with C++ (Homework)

In my c++ class, we got assigned pairs. Normally I can come up with an effective algorithm quite easily, this time I cannot figure out how to do this to save my life.
What I am looking for is someone to explain an algorithm (or just give me tips on what would work) in order to get this done. I'm still at the planning stage and want to get this code done on my own in order to learn. I just need a little help to get there.
We have to create histograms based on a 4 or 5 integer input. It is supposed to look something like this:
Calling histo(5, 4, 6, 2) should produce output that appears like:
*
* *
* * *
* * *
* * * *
* * * *
-------
A B C D
The formatting to this is just killing me. What makes it worse is that we cannot use any type of arrays or "advanced" sorting systems using other libraries.
At first I thought I could arrange the values from highest to lowest order. But then I realized I did not know how to do this without using the sort function and I was not sure how to go on from there.
Kudos for anyone who could help me get started on this assignment. :)
Try something along the lines of this:
Determine the largest number in the histogram
Using a loop like this to construct the histogram:
for(int i = largest; i >= 1; i--)
Inside the body of the loop, do steps 3 to 5 inclusive
If i <= value_of_column_a then print a *, otherwise print a space
Repeat step 3 for each column (or write a loop...)
Print a newline character
Print the horizontal line using -
Print the column labels
Maybe i'm mistaken on your q, but if you know how many items are in each column, it should be pretty easy to print them like your example:
Step 1: Find the Max of the numbers, store in variable, assign to column.
Step 2: Print spaces until you get to column with the max. Print star. Print remaining stars / spaces. Add a \n character.
Step 3: Find next max. Print stars in columns where the max is >= the max, otherwise print a space. Add newline. at end.
Step 4: Repeat step 3 (until stop condition below)
when you've printed the # of stars equal to the largest max, you've printed all of them.
Step 5: add the -------- line, and a \n
Step 6: add row headers and a \n
If I understood the problem correctly I think the problem can be solved like this:
a= <array of the numbers entered>
T=<number of numbers entered> = length(a) //This variable is used to
//determine if we have finished
//and it will change its value
Alph={A,B,C,D,E,F,G,..., Z} //A constant array containing the alphabet
//We will use it to print the bottom row
for (i=1 to T) {print Alph[i]+" "}; //Prints the letters (plus space),
//one for each number entered
for (i=1 to T) {print "--"}; //Prints the two dashes per letter above
//the letters, one for each
while (T!=0) do {
for (i=1 to N) do {
if (a[i]>0) {print "*"; a[i]--;} else {print " "; T--;};
};
if (T!=0) {T=N};
}
What this does is, for each non-zero entered number, it will print a * and then decrease the number entered. When one of the numbers becomes zero it stops putting *s for its column. When all numbers have become zero (notice that this will occur when the value of T comes out of the for as zero. This is what the variable T is for) then it stops.
I think the problem wasn't really about histograms. Notice it also doesn't require sorting or even knowing the