1 / 2
Oct 2011

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX_NUM_LEN 7
int strrev(char *s)
{
	unsigned int l, i;
    if(s == NULL) return 0;

for(i = 0, l = strlen(s); i <= ceil(l / 2); ++i, --l)
{
	char c = s[i];
	s[i] = s[l-1];
	s[l-1] = c;
}

return 1;
}
bool ispalin(const char *s_in)
{
	char *s, *s1, *s2;
	unsigned int l, l12;
	bool ret = false;
    if(s_in == NULL) return false;

l = strlen(s_in);
l12 = floor(l / 2);
s = malloc(sizeof(char) * (l + 1));
s1 = malloc(sizeof(char) * (l12 + 1));
s2 = malloc(sizeof(char) * (l12 + 1));

strcpy(s, s_in);
strncpy(s1, s, l12);
strrev(s);
strncpy(s2, s, l12);
s1[l12] = s2[l12] = '\0';

if(memcmp(s1, s2, l12) == 0) ret = true;

free(s);
free(s1);
free(s2);

return ret;
}
bool uint32_ispalin(uint32_t x)
{
	bool ret;
	char *s;
    s = malloc(sizeof(char) * (MAX_NUM_LEN + 1));
sprintf(s, "%u", x);
ret = ispalin(s);
free(s);

return ret;
}
int main(int argc, char **argv)
{
	unsigned int n, i;
	uint32_t *in;
    scanf("%u", &n);
in = malloc(sizeof(int) * n);
for(i = 0; i < n; ++i)
	scanf("%u", &in[i]);

for(i = 0; i < n; ++i)
	for(; ; ++in[i])
		if(uint32_ispalin(in[i]))
		{
			printf("%u\n", in[i]);
			break;
		}

free(in);

return EXIT_SUCCESS;
}

I really can't tell why does it report a wrong answer. I've tested it throughouly and it works perfectly.

EDIT: Wait, wait..

Is that a million digits? If that is so, I need to implement a number so big. Can someone confirm this?

  • created

    Oct '11
  • last reply

    Oct '11
  • 1

    reply

  • 105

    views

  • 2

    users

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 19 12d

Want to read more? Browse other topics in C and C++ or view latest topics.