本地机器上测试的结果与Full Answer一致,但提交运行就:
> Run 8: 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 2052 KB of memory. ------ Data for Run 8 [length=2 bytes] ------ 3 ----------------------------
代码:
/* ID: nenusb1 LANG: C TASK: palsquare */ #include <stdio.h> #include <string.h> #include <stdlib.h> char str[10]; char s[10]; int len; void myItoa(int num, int base){ memset(str,'0',sizeof(str)); int i=0; while(num>0){ int mod = num%base; num /= base; if(mod<10) {str[i] = mod + '0';} else {str[i] = mod-10 + 'A';} i++; } len = i; } int isPal(){ int i; for(i=0; i<len; i++){ if(str[i] != str[len-1-i]) return 0; } return 1; } void reverse(){ int i; for(i=0; i<len/2; i++){ char t = str[i]; str[i] = str[len-1-i]; str[len-1-i] = t; } } int main(){ freopen("palsquare.in","r",stdin); freopen("palsquare.out","w",stdout); int b; scanf("%d",&b); int n; for(n=1; n<=300; n++){ double dsquare = pow(n,2); int square = dsquare; myItoa(square,b); if(isPal()){ //printf("n=%d square=%d---",n,square); str[len]='\0'; memset(s,'0',sizeof(s)); strcpy(s,str); myItoa(n,b); reverse(); str[len]='\0'; printf("%s %s\n",str,s); } } return 0; }
经过曾哥帮忙,发现上述在Dev C++上运行正常的代码在VS2010上需改动两点:
1st,增加头文件
#include <math.h> //包含pow函数的头文件
2nd,将
double dsquare = pow(n,2);
改成:
double dsquare = pow((double)n,2);
改后,vs上跑通了,USACO上也通过了(是2nd导致了Execution Error,but don't konw why)
正确代码:
/* ID: nenusb1 LANG: C TASK: palsquare */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> char str[20]; char s[20]; int len; void myItoa(int num, int base){ memset(str,'0',sizeof(str)); int i=0; while(num>0){ int mod = num%base; num /= base; if(mod<10) {str[i] = mod + '0';} else {str[i] = mod-10 + 'A';} i++; } len = i; } int isPal(){ int i; for(i=0; i<len; i++){ if(str[i] != str[len-1-i]) return 0; } return 1; } void reverse(){ int i; for(i=0; i<len/2; i++){ char t = str[i]; str[i] = str[len-1-i]; str[len-1-i] = t; } } int main(){ freopen("palsquare.in","r",stdin); freopen("palsquare.out","w",stdout); int b; scanf("%d",&b); int n; for(n=1; n<=300; n++){ double dsquare = pow((double)n,2); int square = dsquare; myItoa(square,b); if(isPal()){ //printf("n=%d square=%d---",n,square); str[len]='\0'; memset(s,'0',sizeof(s)); strcpy(s,str); myItoa(n,b); reverse(); str[len]='\0'; printf("%s %s\n",str,s); } } return 0; }