示例#1
0
文件: eu036.c 项目: pbevin/euler
/*
 * Sum of all numbers < 1,000,000 that are palindromic
 * in base 10 and base 2.
 */
void eu036(char *ans) {
  char buf[30];
  int t = 0;

  for (int i = 1; i < 1000000; i++) {
    sprintf(buf, "%d", i);
    if (ispalindrome(buf)) {
      base2(i, buf);
      if (ispalindrome(buf)) {
        t += i;
      }
    }
  }

  sprintf(ans, "%d", t);
}
示例#2
0
文件: eul004.c 项目: gidj/cEuler
int unsigned long
largestPalindrome(int unsigned long a, int unsigned long b, int unsigned long palin) {
  if (a < 1) {
    return palin;
  }

  if (b < 1) {
    return largestPalindrome(a-1, a-2, palin);
  }

  int unsigned long temp = a*b;

  if (temp < palin) {
    if ((a-1)*(a-1) < palin) {
      return palin;
    } else { 
      return largestPalindrome(a-1, a-2, palin);
    }
  }

  if (ispalindrome(temp)) {
    if (temp > palin) {
      palin = temp;
      printf("New palindrome! %lu\n", palin);

      if ((a-1)*(a-2) < palin) {
        return palin;
      } else {
        return largestPalindrome(a-1, a-2, palin);
      }
    }
  }
  
  return largestPalindrome(a, b-1, palin);
}
示例#3
0
main()
{
	int i=100,j,k,l=0,ll,lll;
	int digits[6];
	int max=0;

	while (i<=999)
	{
		j=100;
		while (j<=999)
		{
			k=i*j;
/*			printf("k=%d\n",k); */
			l=0;
			while (l<6)
			{
				ll=pow(10,l);
				lll=pow(10,l+1);
				digits[l] = (k%lll)/ll;
				l++;
			}
			if (ispalindrome(digits))
			{
				if(k>max)
				{
					printf("%d is the greatest palindrome\n",k);
					max=k;
				}
			}
/*			l=0; while(l<6) { printf("digits[%d]=%d\n",l,digits[l]); l++; } */
			j++;
		}
		i++;
	}
}
示例#4
0
int PE4main(pe_data_t *pedata)
{
    int largest_palindrome = 0, temp = 0;
    int x, y, total;

    for (x = 100; x <=999; x++)
        {
            for (y = 100; y <= 999; y++)
                {
                    total = x * y;
                    if (ispalindrome(total))
                        {
                            temp = total;
                            if (temp > largest_palindrome)
                                largest_palindrome = total;
                        }

                }
        }

    if (pedata->verbosity > 0) printf("Largest palindrome is %i\n", largest_palindrome);
    sprintf(pedata->result,"%i",largest_palindrome);

    return 0;
}
 void dfs(string &s, vector<vector<string> > &result, vector<string> &path, int from, int to) {
     if (to == s.size()) {
         if (ispalindrome(s, from, to)) {
             path.push_back(s.substr(from, to - from));
             result.push_back(path);
             path.pop_back();
         }
         return;
     }
     
     // don't cut
     dfs(s, result, path, from, to+1);
     // cut
     if (ispalindrome(s, from, to)) {
         path.push_back(s.substr(from, to - from));
         dfs(s, result, path, to, to+1);
         path.pop_back();
     }
 }
示例#6
0
int main()
{
    char str[] = "abciccba";
    if (ispalindrome(str))
        printf("%s is palindrome\n", str);
    else
        printf("%s is not palindrome\n", str);

    return 0;
}
示例#7
0
void testpalindrome()
{
	struct node* node = buildlistattailbyref(3);
	printlist(node, "node");
	node->data = node->next->next->data;
	printlist(node, "palindrome");

	printf("\nispalindrome recur = %s\n", ispalindromerecur(&node, node) ? "true" : "false");
	printf("\nispalindrome = %s\n", ispalindrome(node) ? "true" : "false");

}
示例#8
0
char *strmakepalindrome (char *s)
{
    if (!ispalindrome(s))
    {
        char rev [1024];
        strcpy (rev, s);
        strrev (rev);
        strcat (s, rev);
    }
    return s;
}
 void help(vector<vector<string>> &ret, vector<string> &one, string &s, int start){
     if(start==s.size()){
         ret.push_back(one);
         return;
     }
     //for loop counts all substr with same start pos, with increasing size
     //every recursive increase start by one
     for(int i=start;i<s.size();i++){
         string cur = s.substr(start,i-start+1);
         if(ispalindrome(cur)){
             one.push_back(cur);
             help(ret,one,s,i+1);
             one.pop_back();
         }
     }
 }
示例#10
0
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
	int n;
	scanf("%d",&n);

	int i;
	for(i=2;i<=n;i++)
	{
		int n_base=number(n,i);
		if(ispalindrome(n_base))
			break;
	}
	printf("%d\n",i);
	}
	return 0;
}
示例#11
0
int main()
{
	int t;
	scanf("%d",&t);
	
	while(t--)
	{
		long long int num;
		scanf("%lld",&num);
		int count=0;
		
		while(!ispalindrome(num))
		{
			num=num+reverse(num);
			count++;
		}
		printf("%d %lld\n",count,num);
	}
	return 0;
}
 //@s: the given string
 //@idx: the index we has manipulated
 //@ans: each solution
 //@l: how many string in ans
 //@ret: final answer 
 void work(const string &s, int idx, vector<string> &ans, int l, vector<vector<string> > &ret)
 {
     //the partition is over
     if(idx >= s.length())
         {
             vector<string> tt(ans.begin(), ans.begin()+l);
             ret.push_back(tt);
             return;
         }
     //test all possiblility
     for(int i=idx; i<s.length(); ++i)
         {
             string t = s.substr(idx, i-idx+1);
             if(ispalindrome(t))//t is a palindrome string
             {
                 ans[l] = t;
                 work(s, i+1, ans, l+1, ret);
             }
         }
 }
示例#13
0
文件: euler4.c 项目: umenthum/euler
int main()
{
    int i,j;
    struct llist* curr = malloc(sizeof(llist));
    struct llist* holder = curr;
    for(i=0;i<900;i++){
        for(j=0;j<900;j++){
            if(ispalindrome((i+100)*(j+100))==1){
                printf("%d\n",(i+100)*(j+100));
                curr->val=(i+100)*(j+100);
                curr->next=malloc(sizeof(llist));
                curr=curr->next;
            }
        }
    }
    i=0;
    while(holder!=curr){
        i=holder->val>i?holder->val:i;
        holder=holder->next;
    }
    printf("%d\n",i);
    return 0;
}