Reputation: 39
I am currently trying to solve a problem set on codeforce where I need to check if an positive integer number has unique digits. My solutions includes a while loop and two for loops, which is quite a lot of for such an easy task.
I found a more elegant solution but I don't fully understand how the code works. I have commented it with my remarks. Could someone explain to me the second 2) and fifth 5) part?
int unique(long long int number){
/* 1) create array/list with 10 elements, the first element seen[0]
* is equal to zero */
char seen[10] = {0};
/* 2) what is the meaning of while(some random integer number)? I thought
* that the argument must be a statement that is either true or false. */
while (number) {
int digit = number % 10; // 3) get the last digit of the number
number /= 10; // 4) removes last digit of the number
/* 5) Could someone explain to me what seen[digit]++ does. And when its
* true or false? */
if (seen[digit]++)
return 0; /* not unique */
}
return 1; /* unique */
}
Of course I tried to figure out the fifth part on my own but
#include <iostream>
using namespace std;
int main(){
char seen[10] = {0};
cout << seen[7]++ << endl;
}
print outs nothing.
Upvotes: 0
Views: 256
Reputation: 2219
I'll go by parts:
2 ) The implicit conversion between a numeric type and bool returns false if the number is zero and true otherwise. You could read while(number)
like while(number != 0)
5 ) This works the same way: seen[digit]++
is an expression with the same value as seen[digit]
but that then increments its value (check how post-increment works). Therefore, the first time that digit
is seen, seen[digit]++
has the value 0
(so the first time the condition is not met) and increments its value to 1
(so the second time the condition will be met, making the function return).
Upvotes: 2
Reputation: 323
In C++ 0
evaluates to false
and any other number evaluates to true
. That "random number" is actually modified inside the loop with number /= 10
. Division of integer numbers in C++ is special in the sense that it does not yield fractions so 51/10 = 5
and 5/10 = 0
. At some point number
equals 0 and the loop ends.
seen[digit]++
is a commonly used trick. You lookup the table seen
at position digit
return the current value and increment the value by 1. So if you would modify your example code like this:
#include <iostream>
using namespace std;
int main(){
int seen[10] = {0};
cout << seen[7]++ << endl;
cout << seen[7] << endl;
}
Your console output should be:
0
1
There is also ++seen[digit]
which would first increment and then return the value so you would get:
1
1
Upvotes: 0
Reputation: 3282
The condition if(number)
is same as if(number != 0)
.
number/10
will be 0 (as the last digit belongs to 0-9) and there we end our loop.1
and the if condition is satisfied.Upvotes: 0
Reputation: 41
Ok so:
while (number)
will iterate as long as number != 0
seen[digit]++
first returns the value, then increments itself by one after returning the value.Upvotes: 0
Reputation: 1434
Q.1 what is the meaning of while(some random integer number)? I thought that the argument must be a statement that is either true or false.
=> Yes you are right while condition checks for true and false. In case of integer, 0 is treated as false and rest of the integers as true. So, whenever number become 0, while loop will break.
Q.2 Could someone explain to me what seen[digit]++ does. And when its true or false?
=> seen
is declared as an array of size 10 and initialized all entries as 0. So initially every entry of array seen
is zero i.e. seen[0] = 0, seen[1] = 0, seen[2] = 1... seen[9] = 0. Now when we find digit
and perform seen[digit]++
it will increase value by 1 every time.
Upvotes: 0
Reputation: 96
while(number)
means the cycle will repeat until number is not zero. Non-zero number is equal to true
seen[digit]++
does following:
So for the second call it will return 1 - that mean this number already met, so it is not unique.
Upvotes: 0