USACO Dual Palindrome

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
/*
ID: nenusb1
LANG: C
TASK: dualpal
*/
#include <stdio.h>
#include <string.h>

char numStr[12];
int ifPal(){
     int len = strlen(numStr);
     int i;
     for(i=0; i<len; i++){
           if(numStr[i] != numStr[len-1-i]) return 0;//use break is stupid!
     }
     return 1;
}
//itoa
void myItoa(int value, int base){
     int i=0;     
     while(pow(base,i)<=value){
             i++;
     }
     int len = --i;
     numStr[len+1] = '\0';     
     char c[1];      
     while(value>=0 && i>=0){    
        int tmp = value % base;    
        value /= base;                  
        //convert single int to char
        numStr[len-i] = tmp+'0';       
        i--;
     }   
} 

int main(){
    freopen("dualpal.in","r",stdin);
    freopen("dualpal.out","w",stdout);
    
    int n, s;
    scanf("%d %d",&n,&s);
    
    int num, count;
    count = 0;
    for(num = s+1;count<n;num++){
       memset(numStr,'0',sizeof(numStr));

       int base;
       int palCount = 0;
       for(base = 2; base<=10; base++){
         myItoa(num,base);
         
         if(ifPal(numStr)){ palCount++;}
         if(palCount == 2) {printf("%d\n", num); count++; break;}
       }
       
    }
    
    return 0;
}

Reference resources

The original

char numStr[12]

Into a global variable, Execution before error is solved, don't konw why

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Xavier at January 22, 2014 - 12:33 PM