如何检查一个数字是否只包含不重复的数字?

huangapple go评论131阅读模式
英文:

how do i check if a number has only unique digits?

问题

例如,2345是一个独特的数字,因为没有数字重复出现,但3324不是一个独特的数字,因为数字3出现了两次。我尝试使用%,但我得到的是数字而不是数字。编辑:您不能使用字符串

number % 10 == number % 100 || number % 10 == number % 1000 || number % 100 == number % 1000
英文:

for example, 2345 is a unique digit number because no digit is shown twice but 3324 isn't a unique number because 3 is there twice. I tried using % but I as (code) shows but I didn't get digits I got numbers, edit: you cant use strings

number%10==number%100||number%10==number%1000||number%100==number%1000

答案1

得分: 6

以下是您要翻译的代码部分:

You could use an array of 10 flags to indicate whether a digit has been seen yet. Work your way through the number in a loop, extracting the units digit, checking whether the digit has been seen, flagging the digit as seen, and dividing the number by 10 for the next iteration.

As pointed out by @Bathsheba, some care is needed when dealing with negative numbers.

For example:

int unique(long long int number)
{
    char seen[10] = {0};

    while (number) {
        int digit = number % 10;

        number /= 10;
        if (digit < 0) {
            /* 
             * The number was negative.  Make it positive.
             * (Note: Checking the number is negative before the while
             * loop could fail when number is LLONG_MIN, so do it here
             * instead.)
             */
            digit = -digit;
            number = -number;
        }
        if (seen[digit]++)
            return 0; /* not unique */
     }
     return 1; /* unique */
 }

Separate functions may be needed if it is necessary to deal with both long long int and unsigned long long int, but the one that handles long long int can make use of the one that handles unsigned long long int as follows:

#include <limits.h>

int unique_ull(unsigned long long int number)
{
    char seen[10] = {0};

    while (number) {
        int digit = number % 10;

        number /= 10;
        if (seen[digit]++)
            return 0; /* not unique */
     }
     return 1; /* unique */
 }

int unique_ll(long long int number)
{
    unsigned long long int n;

    /* Represent original number as a 2's complement number. */
    n = number;
    if (n > LLONG_MAX) {
        /*
         * Original number was negative, so take its 2's complement to "negate" it.
         * (Note: This works even when original number is LLONG_MIN.)
         */
        n = -n;
    }
    /* Handle as an unsigned long long int. */
    return unique_ull(n);
 }

I suppose it would be useful to support intmax_t and uintmax_t in the same way:

#include <stdint.h>

int unique_um(uintmax_t number)
{
    char seen[10] = {0};

    while (number) {
        int digit = number % 10;

        number /= 10;
        if (seen[digit]++)
            return 0; /* not unique */
     }
     return 1; /* unique */
 }

int unique_m(intmax_t number)
{
    uintmax_t n;

    /* Represent original number as a 2's complement number. */
    n = number;
    if (n > INTMAX_MAX) {
        /*
         * Original number was negative, so take its 2's complement to "negate" it.
         * (Note: This works even when original number is INTMAX_MIN.)
         */
        n = -n;
    }
    /* Handle as a uintmax_t. */
    return unique_um(n);
 }
英文:

You could use an array of 10 flags to indicate whether a digit has been seen yet. Work your way through the number in a loop, extracting the units digit, checking whether the digit has been seen, flagging the digit as seen, and dividing the number by 10 for the next iteration.

As pointed out by @Bathsheba, some care is needed when dealing with negative
numbers.

For example:

int unique(long long int number)
{
char seen[10] = {0};
while (number) {
int digit = number % 10;
number /= 10;
if (digit < 0) {
/* 
* The number was negative.  Make it positive.
* (Note: Checking the number is negative before the while
* loop could fail when number is LLONG_MIN, so do it here
* instead.)
*/
digit = -digit;
number = -number;
}
if (seen[digit]++)
return 0; /* not unique */
}
return 1; /* unique */
}

Separate functions may be needed if it is necessary to deal with both long long int and unsigned long long int, but the one that handles long long int can make use of the one that handles unsigned long long int as follows:

#include <limits.h>
int unique_ull(unsigned long long int number)
{
char seen[10] = {0};
while (number) {
int digit = number % 10;
number /= 10;
if (seen[digit]++)
return 0; /* not unique */
}
return 1; /* unique */
}
int unique_ll(long long int number)
{
unsigned long long int n;
/* Represent original number as a 2's complement number. */
n = number;
if (n > LLONG_MAX) {
/*
* Original number was negative, so take its 2's complement to "negate" it.
* (Note: This works even when original number is LLONG_MIN.)
*/
n = -n;
}
/* Handle as an unsigned long long int. */
return unique_ull(n);
}

I suppose it would be useful to support intmax_t and uintmax_t in the same way:

#include <stdint.h>
int unique_um(uintmax_t number)
{
char seen[10] = {0};
while (number) {
int digit = number % 10;
number /= 10;
if (seen[digit]++)
return 0; /* not unique */
}
return 1; /* unique */
}
int unique_m(intmax_t number)
{
uintmax_t n;
/* Represent original number as a 2's complement number. */
n = number;
if (n > INTMAX_MAX) {
/*
* Original number was negative, so take its 2's complement to "negate" it.
* (Note: This works even when original number is INTMAX_MIN.)
*/
n = -n;
}
/* Handle as a uintmax_t. */
return unique_um(n);
}

答案2

得分: 1

以下是您提供的代码的翻译部分:

My five cents.:)

For starters the parameter of the function should have the type `long long int` (or `intmax_t`). In this case the function can be applied to objects of any signed integer type.

首先,函数的参数应该是 `long long int` 类型(或 `intmax_t`)。在这种情况下,该函数可以应用于任何有符号整数类型的对象。

Here is a demonstrative program.

这里是一个演示程序。

#include <stdio.h>

#include <stdio.h>

int unique_digits( long long int n )
{
    const long long int Base = 10;

    int unique = 1;

    while ( unique &amp;&amp; n )
    {
        long long int digit = n % Base;
        long long int tmp = n /= Base;

        while (tmp &amp;&amp; digit != tmp % Base ) tmp /= Base;

        unique = tmp == 0;
    }

    return unique;
}

int main(void) 
{
    int n = 12345678;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    n = 12345671;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    n = -12345678;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    n = -112345671;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    return 0;
}

The program output is

程序的输出是

12345678: unique
12345671: non_unique
-12345678: unique
-112345671: non_unique

If to include the header `&lt;stdint.h&gt;` then the type `long long int` can be substituted for the type `intmax_t`.

如果包含头文件 `&lt;stdint.h&gt;`,那么类型 `long long int` 可以替换为类型 `intmax_t`。

Another approach is to use an integer as an array of digits. Here is a demonstrative program.

另一种方法是将整数视为一个数字数组。以下是一个演示程序。

#include &lt;stdio.h&gt;

#include <stdio.h>

int unique_digits( long long int n )
{
    const long long int Base = 10;

    unsigned short unique = 0;

    long long int digit;
    int offset;

    do
    {
        digit = n % Base;
        if ( digit &lt; 0 ) digit = -digit;

        offset = 1 &lt;&lt; digit;
        unique ^= offset;
    } while ( ( unique &amp; offset ) &amp;&amp; ( n /= Base )  );        

    return n == 0;
}

int main(void) 
{
    int n = 12345678;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    n = 12345671;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    n = -12345678;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    n = -112345671;

    printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );

    return 0;
}

It output is the same as of the previous program

其输出与前一个程序相同

12345678: unique
12345671: non_unique
-12345678: unique
-112345671: non_unique

请注意,我已将一些HTML实体编码(如&lt;&quot;)翻译为它们的正常形式,以便代码更容易阅读。

英文:

My five cents.:)

For starters the parameter of the function should have the type long long int (or intmax_t). In this case the function can be applied to objects of any signed integer type.

Here is a demonstrative program.

#include &lt;stdio.h&gt;
int unique_digits( long long int n )
{
const long long int Base = 10;
int unique = 1;
while ( unique &amp;&amp; n )
{
long long int digit = n % Base;
long long int tmp = n /= Base;
while (tmp &amp;&amp; digit != tmp % Base ) tmp /= Base;
unique = tmp == 0;
}
return unique;
}
int main(void) 
{
int n = 12345678;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
n = 12345671;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
n = -12345678;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
n = -112345671;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
return 0;
}

The program output is

12345678: unique
12345671: non_unique
-12345678: unique
-112345671: non_unique

If to include the header &lt;stdint.h&gt; then the type long long int can be substituted for the type intmax_t.

Another approach is to use an integer as an array of digits. Here is a demonstrative program.

#include &lt;stdio.h&gt;
int unique_digits( long long int n )
{
const long long int Base = 10;
unsigned short unique = 0;
long long int digit;
int offset;
do
{
digit = n % Base;
if ( digit &lt; 0 ) digit = -digit;
offset = 1 &lt;&lt; digit;
unique ^= offset;
} while ( ( unique &amp; offset ) &amp;&amp; ( n /= Base )  );        
return n == 0;
}
int main(void) 
{
int n = 12345678;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
n = 12345671;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
n = -12345678;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
n = -112345671;
printf( &quot;%d: %s\n&quot;, n, unique_digits( n ) ? &quot;unique&quot; : &quot;non_unique&quot; );
return 0;
}

It output is the same as of the previous program

12345678: unique
12345671: non_unique
-12345678: unique
-112345671: non_unique

答案3

得分: 0

你可以使用set STL来检查一个数字是否只包含唯一的数字。例如,让我们考虑数字2020,然后我们可以将这个数字转换为字符串:

int num;
cin >> num;
string s = to_string(num);

然后,使用字符串s的内容初始化一个set:

set<int> uniDigits(s.begin(), s.end());

然后,我们可以比较字符串s的大小和新创建的set uniDigits的大小:

if (s.size() == uniDigits.size()) { break; }

由于我们知道set的特性是存储唯一的数字并且丢弃重复的数字,因此如果两个大小相等,那意味着所有数字都是唯一的。如果集合的大小小于字符串的大小,那意味着该数字不包含不同的数字,并且所有重复的数字都被丢弃。

以下是一个示例代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s = "2020";
    set<int> digits(s.begin(), s.end());
    cout << s.size() << " " << digits.size() << endl;
    return 0;
}

在这个例子中,字符串的大小是4,而集合的大小是2,因为字符串包含两个重复的数字。

英文:

You can use set STL in order to check if a number has only unique digits. For example, let us consider number 2020, then we can convert the number into the string,

int num;
cin&gt;&gt;num
string s = to_string(num);

After that, initialise a set with the contents of string s.

set&lt;int&gt; uniDigits(s.begin(), s.end());

Then we can compare the size of string s and newly created set uniDigits.

if(s.size() == uniDigits.size()) { break; }

As we know the property of sets, that set stores only unique numbers and discards repeated digits, then if both sizes are equal that means all digits are unique. If size of set is less than that of the string, that means the number does not contain distinct digits and all the repeated numbers are discarded.

#include &lt;bits/stdc++.h&gt;
using namespace std;
int main()
{
string s = &quot;2020&quot;;
set&lt;int&gt; digits(s.begin(), s.end());
cout &lt;&lt; s.size() &lt;&lt; &quot; &quot; &lt;&lt; digits.size() &lt;&lt; endl;
return 0;
}

Here, the size of string is 4 and size of set is 2, because the string contains two repeated digits.

答案4

得分: -1

以下是翻译好的部分:

这是实现这一目标的一种方法

#include <stdio.h>

int main(){
    int n, i, digit;
    static int arr[10];
    scanf("%d", &n);
    while(n>0){
        digit = n%10;
        arr[digit]++;
        n=n/10;
    }
    for(i=0; i<10; i++){
        if(arr[i]>1){
            printf("不唯一!");
            break;
        }
    }
    if(i==10){
        printf("唯一");
    }
    return 0;
}

请注意,静态数组的元素已初始化为默认值0。

英文:

Here's one approach to achieve this

#include &lt;stdio.h&gt;
int main(){
int n, i, digit;
static int arr[10];
scanf(&quot;%d&quot;, &amp;n);
while(n&gt;0){
digit = n%10;
arr[digit]++;
n=n/10;
}
for(i=0; i&lt;10; i++){
if(arr[i]&gt;1){
printf(&quot;Not unique!&quot;);
break;
}
}
if(i==10){
printf(&quot;unique&quot;);
}
return 0;
}

Note that the elements of static array is initialized with the default value of 0.

huangapple
  • 本文由 发表于 2020年1月3日 19:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定