代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long ansfqy = __LONG_MAX__;
int ansnum = 10;
void justdfs(long n, long frequency);
void exceeddfs(char *n, long frequency);
void justdfs(long n, long frequency) {
if (ansfqy < frequency) {
return;
}
if (n < 10 && (n < ansnum || frequency < ansfqy)) {
ansnum = n;
ansfqy = frequency;
return;
}
long temp = 10;
while (n / temp > 0) {
long nleft = n / temp;
long nright = n % temp;
temp *= 10;
justdfs(nleft + nright, frequency + 1);
}
}
void exceeddfs(char *n, long frequency) {
if (ansfqy < frequency) {
return;
}
for (int i = 1; i < strlen(n); i++) {
char BIleft[1025], BIright[1025];
strncpy(BIleft, n, i);
BIleft[i] = '\0';
strncpy(BIright, n + i, strlen(n) - i);
BIright[strlen(n) - i] = '\0';
long sum = atol(BIleft) + atol(BIright);
char temp[1025];
sprintf(temp, "%ld", sum);
if (strlen(temp) < 19) {
justdfs(sum, frequency + 1);
} else {
exceeddfs(temp, frequency + 1);
}
}
}
int main() {
char n[1025];
scanf("%s", n);
if (strlen(n) < 19) {
justdfs(atol(n), 0);
} else {
exceeddfs(n, 0);
}
printf("%ld %d\n", ansfqy, ansnum);
return 0;
}