C89,381个 226 195 170 169字节
1分索引(与挑战中的回答完全相同)。
假定4字节(32位)int
(大多数现代体系结构)。
我真的相信这不可能再短了。
x,c,*b,m,t,i,a;g(n){for(b=malloc(0);c<n;b[c-1]=x++,t=1){char s[9];for(i=m=0;i<sprintf(s,"%d",x);m+=a,t*=a)a=s[i++]-48;b=m*t?x%m+x%t?b:realloc(b,4*++c):b;}return b[c-1];}
函数会int g (int)
在每次调用时泄漏内存并访问未初始化的内存,但不会进行段错误并返回正确的数字。
完整程序,以./prog $(seq 1 10)
ungolfed(kinda)的形式在一元(10个)中输入:
x, c, * b, m, t, i, a;
g(n) {
for (b = malloc(0); c < n; b[c - 1] = x++, t = 1) {
char s[9];
i = m = 0;
for (; i < sprintf(s, "%d", x); m += a, t *= a) a = s[i++] - 48;
b = m * t ? x % m + x % t ? b : realloc(b, 4 * ++c) : b;
}
return b[c - 1];
}
main (j) {
printf("%d\n", g(--j));
}
旧答案:
C99,381字节
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#define U uint64_t
#define S size_t
S f(S n){U x=0;S c=1,l;U*b=malloc(sizeof(U));while(c<=n){char s[21];snprintf(s,20,"%"PRIu64,x);U m=0,t=1;l=strnlen(s,21);for(S i=0;i<l;i++){U a=(U)s[i]-48;m+=a,t*=a;}if(m*t?(!(x%m))&&(!(x%t)):0){b=realloc(b,sizeof(U)*++c);b[c-1]=x;}++x;}U o=b[n];free(b);return o;}
这可能打得更多。
完整程序:
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
bool qualifies (const uint64_t);
size_t f (const size_t);
int main(const int argc, const char* const * const argv) {
(void) argc;
size_t arg = strtoull(argv[1], NULL, 10);
uint64_t a = f(arg);
printf("a: %" PRIu64 "\n", a);
return 0;
}
bool qualifies (const uint64_t num) {
char s[21];
snprintf(s, 20, "%" PRIu64 "", num);
uint64_t sum = 0,
mult = 1;
size_t len = strnlen(s, 400);
for (size_t i = 0; i < len; i++) {
uint64_t a = (uint64_t) s[i] - 48;
sum += a, mult *= a;
}
//printf("sum: %" PRIu64 "\nmult: %" PRIu64 "\n", sum, mult);
return sum * mult ? (! (num % sum)) && (! (num % mult)) : false;
}
size_t f (const size_t n) {
uint64_t x = 0;
size_t s_len = 1;
uint64_t* nums = malloc(sizeof (uint64_t) * s_len);
while (s_len <= n) {
if (qualifies(x)) {
++s_len;
//printf("len: %zu\n", s_len);
nums = realloc(nums, sizeof (uint64_t) * s_len);
nums[s_len - 1] = x;
}
++x;
}
uint64_t o = nums[n];
free(nums);
return o;
}