Section 1.2__PROB Dual Palindromes_Error已解决

  > Run 1: Execution error: Your program had this runtime error:
        Illegal file open (/dev/tty). The program ran for 0.000 CPU
        seconds before the error. It used 2320 KB of memory.

/*
ID: nenusb1
LANG: C
TASK: dualpal
*/
#include <stdio.h>
#include <string.h>


int ifPal(char numStr[12]){
     int len = strlen(numStr);
     int f,t;
     f=0,t=len-1;
     while(f>=0 && t<len && f<=t){
        if(numStr[f]==numStr[t]){f++;t--;}
        else break;
     }
     if(f>t) return 1;
     else return 0;
}
//itoa
void myItoa(int value, char str[12], int base){
     int i=0;     
     while(pow(base,i)<=value){
             i++;
     }
     int len = --i;
     str[len+1] = '\0';     
     char c[1];      
     while(value>=0 && i>=0){        
        double dp = pow(base,i);//如果是用int p = ((int)pow(base,i)),pow(5,2)=25.0000会变成24! 
        int p = (int)dp;
        //printf("value=%d p=%d  ",value,p);        
        
        int div = value/p;  //as base<=10, so intValue must be less than 10  
        value = value%p;
        //printf("div=%d mod=%d\n",div,value);        
          
        //convert single int to char
        char c[1];
        sprintf(c,"%d",div);
        str[len-i] = c[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++){
       
       char numToString[12];
       int base;
       int palCount = 0;
       for(base = 2; base<=10; base++){
         myItoa(num,numToString,base);
         
         if(ifPal(numToString)){ palCount++;}
         if(palCount == 2) {printf("%d\n", num); count++; break;}
       }
    }
    
    return 0;
}



转载于:https://my.oschina.net/kaneiqi/blog/192724

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值