C,217个 189字节
单机版:
int a(char*b){int c=strlen(b);for(int i=0;i<c/2;i++)if(b[i]!=b[c-i-1])return 0;}int main(int e,char **f){int b,c;char d[9];b=atoi(f[1]);c=b;while(1){sprintf(d,"%d",c);if(a(d)&&(c/b)*b==c)return printf("%d",c);c++;}}
调用函数版本:
int s(char*a){int b=strlen(a);for(int i=0;i<b/2;i++)if(a[i]!=a[b-i-1])return 0;}int f(int a){int b;char c[9];b=a;while(1){sprintf(c,"%d",b);if(s(c)&&(b/a)*a==b)return printf("%d",b);b++;}}
松散
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int check_palindrome(char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1])
return 0;
}
return 1;
}
int main(int argc, char **argv) {
int number;
int pal;
char string[15];
number = atoi(argv[1]);
pal = number;
while (1) {
sprintf(string, "%d", pal);
if (check_palindrome(string) && (pal / number) * number == pal)
{
printf("%d\n", pal);
return 1;
}
pal++;
}
return 0;
}
调用函数
int s(char *a) {
int b = strlen(a);
for (int i = 0; i < b / 2; i++) {
if (a[i] != a[b - i - 1])
return 0;
}
return 1; //We can remove it, it leads to a undefined behaviour but it works
}
int f(int a) {
int b;
char c[9];
b = a;
while (1) {
sprintf(c, "%d", b);
if (s(c) && (b / a) * a == b)
{
printf("%d\n", b); //no need for the \n
return 1; //just return whatever printf returns, who cares anyway ?
}
b++;
}
return 0; //no need for that
}
为了历史性,我包括了独立版本。
这是我的第一个代码高尔夫,欢迎任何评论!