Add solution for problem-59 and a C solution for problem 40
This commit is contained in:
65
problem-40.c
Normal file
65
problem-40.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
|
||||
#define MAX_NUM 1000000
|
||||
|
||||
char* LETTERS = "0123456789";
|
||||
|
||||
|
||||
int digits(int* res, int num)
|
||||
{
|
||||
int len = 0;
|
||||
while (num > 0) {
|
||||
len++;
|
||||
res = (int*)realloc(res, len * sizeof(int));
|
||||
res[len-1] = num % 10;
|
||||
num /= 10;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char* str;
|
||||
int* digs;
|
||||
int nums, j, result;
|
||||
int digit = 0, i = 0, num = 1;
|
||||
|
||||
str = malloc(MAX_NUM * sizeof(int));
|
||||
digs = malloc(0 * sizeof(int));
|
||||
nums = digits(digs, num);
|
||||
|
||||
while (digit < MAX_NUM) {
|
||||
str[digit] = digs[nums-1-i];
|
||||
digit++;
|
||||
i++;
|
||||
if (i == nums) {
|
||||
/* refill digs and reset i */
|
||||
num++;
|
||||
nums = digits(digs, num);
|
||||
i = 0;
|
||||
/* DEBUG
|
||||
printf("num = %d str = ", num);
|
||||
for (j=0; j<digit; j++)
|
||||
printf("%d", str[j]);
|
||||
printf("\n");
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
result = str[0] *
|
||||
str[9] *
|
||||
str[99] *
|
||||
str[999] *
|
||||
str[9999] *
|
||||
str[99999] *
|
||||
str[999999];
|
||||
|
||||
printf("The result is %d\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user