产生独特的价值 [英] Generate Unique Values

查看:108
本文介绍了产生独特的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个C程序以生成0到999999之间的数字,请记住,所生成的数字中不应包含任何重复的数字.例如,"123"是可接受的值,但不是"121",因为重复'1'.我还提供了其他程序代码来检查整数是否具有重复的数字:

I want to create a C program to generate numbers from 0 to 999999, keeping in mind that the number generated should not have any digits that are repetitive within it. For example, "123" is an acceptable value but not "121" as the '1' is repeated. I have sourced other program codes that check if an integer has repeated digits:

检查整数是否具有重复数字.没有字符串方法或数组

>什么是最快的方法来检查数字的重复数字?

但是,这些并不能真正解决我的问题,如果我要检查1,000,000个不同的值,它们将是非常低效的解决方案.此外,提供的解决方案是针对int的,而不是针对我在程序中使用的char[]char *的.下面是到目前为止的代码.如您所见,我对处理不超过"012"的值没有问题,但是具有3位及以上数字的值的可能性太多,无法列出,并且编码效率低下.希望能有所帮助.

However these do not really solve my problem and they are very inefficient solutions if I were to perform the check for 1,000,000 different values. Moreover, the solution provided is for int and not char[] and char *, which I use in my program. Below is my code thus far. As you can see I have no problems handling values of up to "012", however the possibilities for values with 3 digits and above are too many to list and too inefficient to code. Would appreciate some help.

int i, j;
char genNext[7] = "0";
printf("%s\n", genNext);

// loop through to return next pass in sequence
while (1) {
    for (i = 0; i < sizeof(genNext) / sizeof(char); i++) {
        if (genNext[i] == '9') {
            char * thisPass = strndup(genNext, sizeof(genNext));
            int countDigit = (int) strlen(thisPass);
            switch (countDigit) {
                case 1:
                genNext = "01";
                break;
                case 2:
                if (strcmp(genNext, "98")) {
                    if (i == 0) {
                        genNext[1] += 1;
                    } else {
                        genNext[0] += 1;
                        genNext[1] == '0';
                    }
                } else {
                    genNext = "012";
                }
                break;
                case 3:
                if (strcmp(genNext, "987")) {
                    // code to handle all cases
                } else {
                    genNext = "0123";
                }
                break;
                case 4:
                case 5:
                case 6:
                    // insert code here
            }
            break;
        } else if (genNext[i] == '\0') {
            break;
        } else if (genNext[i+1] == '\0') {
            genNext[i] += 1;
            for (j = 0; j < i; j++) {
                if (genNext[i] == genNext[j]) {
                    genNext[i] += 1;
                }
            }
        } else {
            continue;
        }
    }
    printf("%s\n", genNext);
    if (strcmp(genNext, "987654") == 0) {
        break;
    }
}

我面临的主要问题是'9'是要测试的值的一部分的情况.例如,基于非重复性和结果顺序返回的规则,"897"之后的序列中的下一个值为"901",而"067895"之后的序列中的下一个值为"067912".

The main problem that I am facing is the cases when '9' is part of the value that is being tested. For example, the next value in the sequence after "897" is "901" and after "067895" comes "067912" based on the rules of non-repetitiveness as well as sequential returning of the result.

所需的输出如下:

0
1
2
3
...
8
9
01
02
03
...
09
10
12
13
...
97
98
012
013
014
...
098
102
103
...
985
986
987
0123
0124
...
etc etc.

我们非常感谢您的协助,如果我的问题的任何部分不清楚,请随时澄清.谢谢!

Any assistance is appreciated, and if any part of my question was unclear, feel free to clarify. Thanks!

如何生成所有数字列表的排列?不能解决我的问题,因为从"120398""120435"的增量是序列中的下一个合法"值.

How do I generate all permutations of a list of numbers? does not solve my question as the increment from "120398" to "120435" as the next "legal" value in the sequence.

更新了问题以包括所需的输出

EDIT 2: Updated question to include desired output

推荐答案

下面有三种变体算法.修改变体3以适应您的要求.

There are three variant algorithms below. Adapt variant 3 to suit your requirements.

这是一种实现方法.它实现了的一个较小变体,将10位数字的表初始化为0;扫描数字,为遇到的每个数字递增计数,然后检查是否有任何数字计数大于我在注释中建议的1 算法.一旦发现重复的数字,测试函数就会返回.

This is one way to do it. It implements a minor variant of the initialize a table of 10 digit counts to 0; scan the digits, increment the count for each digit encountered, then check whether any of the digit counts is more than 1 algorithm I suggested in a comment. The test function returns as soon as a duplicate digit is spotted.

#include <stdio.h>
#include <stdbool.h>

enum { MAX_ITERATION = 1000000 };

static bool duplicate_digits_1(int value)
{
    char buffer[12];
    snprintf(buffer, sizeof(buffer), "%d", value);
    char digits[10] = { 0 };
    char *ptr = buffer;
    char c;
    while ((c = *ptr++) != '\0')
    {
        if (++digits[c - '0'] > 1)
            return true;
    }
    return false;
}

int main(void)
{
    int count = 0;
    for (int i = 0; i < MAX_ITERATION; i++)
    {
        if (!duplicate_digits_1(i))
        {
            count += printf(" %d", i);
            if (count > 72)
            {
                putchar('\n');
                count = 0;
            }
        }
    }
    putchar('\n');
    return 0;
}

运行时,它会产生0到1,000,000之间的168,571个值,开始于:

When run, it produces 168,571 values between 0 and 1,000,000, starting:

 0 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 23 24 25 26 27 28 29
 30 31 32 34 35 36 37 38 39 40 41 42 43 45 46 47 48 49 50 51 52 53 54 56 57
 58 59 60 61 62 63 64 65 67 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84
 85 86 87 89 90 91 92 93 94 95 96 97 98 102 103 104 105 106 107 108 109 120
 123 124 125 126 127 128 129 130 132 134 135 136 137 138 139 140 142 143 145
 146 147 148 149 150 152 153 154 156 157 158 159 160 162 163 164 165 167 168
 169 170 172 173 174 175 176 178 179 180 182 183 184 185 186 187 189 190 192
 193 194 195 196 197 198 201 203 204 205 206 207 208 209 210 213 214 215 216
 217 218 219 230 231 234 235 236 237 238 239 240 241 243 245 246 247 248 249
 250 251 253 254 256 257 258 259 260 261 263 264 265 267 268 269 270 271 273
…
 987340 987341 987342 987345 987346 987350 987351 987352 987354 987356 987360
 987361 987362 987364 987365 987401 987402 987403 987405 987406 987410 987412
 987413 987415 987416 987420 987421 987423 987425 987426 987430 987431 987432
 987435 987436 987450 987451 987452 987453 987456 987460 987461 987462 987463
 987465 987501 987502 987503 987504 987506 987510 987512 987513 987514 987516
 987520 987521 987523 987524 987526 987530 987531 987532 987534 987536 987540
 987541 987542 987543 987546 987560 987561 987562 987563 987564 987601 987602
 987603 987604 987605 987610 987612 987613 987614 987615 987620 987621 987623
 987624 987625 987630 987631 987632 987634 987635 987640 987641 987642 987643
 987645 987650 987651 987652 987653 987654

在确定这是无效"之前,先对其进行测量.您是否真的经常进行锻炼以至于表现是一个真正的问题?

Before you decide this is 'not efficient', measure it. Are you really exercising it often enough that the performance is a real problem?

创建替代版本,我在注释中建议:迭代使用strchr(),检查第一个数字是否出现在尾部,如果不是,第二个数字是否出现在尾部,依此类推在第一个答案的框架下非常容易实现:

Creating the alternative version I suggested in the comments: use strchr() iteratively, checking whether the first digit appears in the tail, and if not whether the second digit appears in the tail, and so on is very easy to implement given the framework of the first answer:

static bool duplicate_digits_2(int value)
{
    char buffer[12];
    snprintf(buffer, sizeof(buffer), "%d", value);
    char *ptr = buffer;
    char c;
    while ((c = *ptr++) != '\0')
    {
        if (strchr(ptr, c) != NULL)
            return true;
    }
    return false;
}

比较时间后,我得到了这些结果(ng41使用duplicate_digits_1()ng43使用duplicate_digits_2().

When the times are compared I got these results (ng41 uses duplicate_digits_1() and ng43 uses duplicate_digits_2().

$ time ng41 > /dev/null
real    0m0.175s
user    0m0.169s
sys     0m0.002s
$ time ng43 > /dev/null
real    0m0.201s
user    0m0.193s
sys     0m0.003s
$

重复的计时通常显示出相似的结果,但是有时ng43的运行速度比ng41快-仅有一百万个数字的计时并不清楚(因此,YMMV –您的行驶里程可能会有所不同!).

Repeated timings generally showed similar results, but sometimes I got ng43 running faster than ng41 — the timing on just one set of one million numbers isn't clear cut (so YMMV — your mileage may vary!).

您还可以使用此技术,该技术类似于计数位数",但无需先转换为字符串(因此应该更快).

You could also use this technique, which is analogous to 'count digits' but without the conversion to string first (so it should be quicker).

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

enum { MAX_ITERATION = 1000000 };

static bool duplicate_digits_3(int value)
{
    char digits[10] = { 0 };
    while (value > 0)
    {
        if (++digits[value % 10] > 1)
            return true;
        value /= 10;
    }
    return false;
}

int main(void)
{
    int count = 0;
    const char *pad = "";
    for (int i = 0; i < MAX_ITERATION; i++)
    {
        if (!duplicate_digits_3(i))
        {
            count += printf("%s%d", pad, i);
            pad = " ";
            if (count > 72)
            {
                putchar('\n');
                count = 0;
                pad = "";
            }
        }
    }
    putchar('\n');
    return 0;
}

因为它避免了转换为字符串,所以速度要快得多.我从3次跑步中得到的最慢计时是:

Because it avoids conversions to strings, it is much faster. The slowest timing I got from a series of 3 runs was:

real    0m0.063s
user    0m0.060s
sys     0m0.001s

大约是其他两个中任何一个的三倍.

which is roughly three times as fast as either of the other two.

我还将MAX_ITERATION的值更改为10,000,000并运行了计时.当然,还有更多被拒绝的输出.

I also changed the value of MAX_ITERATION to 10,000,000 and ran timing. There are many more rejected outputs, of course.

$ time ng41 >/dev/null

real    0m1.721s
user    0m1.707s
sys     0m0.006s
$ time ng43 >/dev/null

real    0m1.958s
user    0m1.942s
sys     0m0.008s
$ time ng47 >/dev/null

real    0m0.463s
user    0m0.454s
sys     0m0.004s
$ ng41 | wc
   69237  712891 5495951
$ ng43 | wc
   69237  712891 5495951
$ ng47 | wc
   69237  712891 5495951
$ cmp <(ng41) <(ng43)
$ cmp <(ng41) <(ng47)
$ cmp <(ng43) <(ng47)
$ 

这些时间更稳定;变体1(ng41)总是比变体2(ng43)快,但是变体3(ng47)两者的跳动都很大.

These timings were more stable; variant 1 (ng41) was always quicker than variant 2 (ng43), but variant 3 (ng47) beats both by a significant margin.

JFTR:测试是在旧的17英寸MacBook Pro上,带有GCC 6.2.0的macOS Sierra 10.12.1上进行的— 2011年初,带有16 GB 1333 MHz DDR3 RAM的2.3GHz Intel Core i7 —并不是内存存在问题该代码.程序编号是连续的两位数质数,以防万一.

JFTR: testing was done on macOS Sierra 10.12.1 with GCC 6.2.0 on an old 17" MacBook Pro — Early 2011, 2.3GHz Intel Core i7 with 16 GB 1333 MHz DDR3 RAM — not that memory is an issue with this code. The program numbers are consecutive 2-digit primes, in case you're wondering.

此代码生成所需的数字序列(尽管仅将其配置为最多运行100,000个-1,000,000的更改是微不足道的).以自虐的方式很有趣.

This code generates the sequence of numbers you want (though it is only configured to run up to 100,000 — the change for 1,000,000 is trivial). It's fun in a masochistic sort of way.

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

enum { MAX_ITERATIONS = 100000 };

/* lz = 1 or 0 - consider that the number has a leading zero or not */
static bool has_duplicate_digits(int value, int lz)
{
    assert(value >= 0 && value < MAX_ITERATIONS + 1);
    assert(lz == 0 || lz == 1);
    char digits[10] = { [0] = lz };
    while (value > 0)
    {
        if (++digits[value % 10] > 1)
            return true;
        value /= 10;
    }
    return false;
}

int main(void)
{
    int lz = 0;
    int p10 = 1;
    int log_p10 = 0;    /* log10(0) is -infinity - but 0 works better */
    int linelen = 0;
    const char *pad = "";

    /* The + 1 allows the cycle to reset for the leading zero pass */
    for (int i = 0; i < MAX_ITERATIONS + 1; i++)
    {
        if (i >= 10 * p10 && lz == 0)
        {
            /* Passed through range p10 .. (10*p10-1) once without leading zeros */
            /* Repeat, adding leading zeros this time */
            lz = 1;
            i = p10;
        }
        else if (i >= 10 * p10)
        {
            /* Passed through range p10 .. (10*p10-1) without and with leading zeros */
            /* Continue through next range, without leading zeros to start with */
            p10 *= 10;
            log_p10++;
            lz = 0;
        }

        if (!has_duplicate_digits(i, lz))
        {
            /* Adds a leading zero if lz == 1; otherwise, it doesn't */
            linelen += printf("%s%.*d", pad, log_p10 + lz + 1, i);
            pad = " ";
            if (linelen > 72)
            {
                putchar('\n');
                pad = "";
                linelen = 0;
            }
        }
    }
    putchar('\n');
    return 0;
}

样本输出(至100,000):

Sample output (to 100,000):

0 1 2 3 4 5 6 7 8 9 01 02 03 04 05 06 07 08 09 10 12 13 14 15 16 17 18 19
20 21 23 24 25 26 27 28 29 30 31 32 34 35 36 37 38 39 40 41 42 43 45 46 47
48 49 50 51 52 53 54 56 57 58 59 60 61 62 63 64 65 67 68 69 70 71 72 73 74
75 76 78 79 80 81 82 83 84 85 86 87 89 90 91 92 93 94 95 96 97 98 012 013
014 015 016 017 018 019 021 023 024 025 026 027 028 029 031 032 034 035 036
037 038 039 041 042 043 045 046 047 048 049 051 052 053 054 056 057 058 059
061 062 063 064 065 067 068 069 071 072 073 074 075 076 078 079 081 082 083
084 085 086 087 089 091 092 093 094 095 096 097 098 102 103 104 105 106 107
108 109 120 123 124 125 126 127 128 129 130 132 134 135 136 137 138 139 140
…
901 902 903 904 905 906 907 908 910 912 913 914 915 916 917 918 920 921 923
924 925 926 927 928 930 931 932 934 935 936 937 938 940 941 942 943 945 946
947 948 950 951 952 953 954 956 957 958 960 961 962 963 964 965 967 968 970
971 972 973 974 975 976 978 980 981 982 983 984 985 986 987 0123 0124 0125
0126 0127 0128 0129 0132 0134 0135 0136 0137 0138 0139 0142 0143 0145 0146
0147 0148 0149 0152 0153 0154 0156 0157 0158 0159 0162 0163 0164 0165 0167
…
0917 0918 0921 0923 0924 0925 0926 0927 0928 0931 0932 0934 0935 0936 0937
0938 0941 0942 0943 0945 0946 0947 0948 0951 0952 0953 0954 0956 0957 0958
0961 0962 0963 0964 0965 0967 0968 0971 0972 0973 0974 0975 0976 0978 0981
0982 0983 0984 0985 0986 0987 1023 1024 1025 1026 1027 1028 1029 1032 1034
1035 1036 1037 1038 1039 1042 1043 1045 1046 1047 1048 1049 1052 1053 1054
1056 1057 1058 1059 1062 1063 1064 1065 1067 1068 1069 1072 1073 1074 1075
…
9820 9821 9823 9824 9825 9826 9827 9830 9831 9832 9834 9835 9836 9837 9840
9841 9842 9843 9845 9846 9847 9850 9851 9852 9853 9854 9856 9857 9860 9861
9862 9863 9864 9865 9867 9870 9871 9872 9873 9874 9875 9876 01234 01235 01236
01237 01238 01239 01243 01245 01246 01247 01248 01249 01253 01254 01256 01257
01258 01259 01263 01264 01265 01267 01268 01269 01273 01274 01275 01276 01278
01279 01283 01284 01285 01286 01287 01289 01293 01294 01295 01296 01297 01298
…
09827 09831 09832 09834 09835 09836 09837 09841 09842 09843 09845 09846 09847
09851 09852 09853 09854 09856 09857 09861 09862 09863 09864 09865 09867 09871
09872 09873 09874 09875 09876 10234 10235 10236 10237 10238 10239 10243 10245
10246 10247 10248 10249 10253 10254 10256 10257 10258 10259 10263 10264 10265
…
98705 98706 98710 98712 98713 98714 98715 98716 98720 98721 98723 98724 98725
98726 98730 98731 98732 98734 98735 98736 98740 98741 98742 98743 98745 98746
98750 98751 98752 98753 98754 98756 98760 98761 98762 98763 98764 98765 012345
012346 012347 012348 012349 012354 012356 012357 012358 012359 012364 012365
012367 012368 012369 012374 012375 012376 012378 012379 012384 012385 012386
…
098653 098654 098657 098671 098672 098673 098674 098675 098712 098713 098714
098715 098716 098721 098723 098724 098725 098726 098731 098732 098734 098735
098736 098741 098742 098743 098745 098746 098751 098752 098753 098754 098756
098761 098762 098763 098764 098765

这篇关于产生独特的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆