1 /** 2 ID: njuwz151 3 TASK: palsquare 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 10 using namespace std; 11 12 char baseTable[] = {‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘}; 13 14 char* reverse(const char* src, int len); 15 16 char* base10ToBaseN(int number, int base); 17 18 int main() { 19 freopen("palsquare.in", "r", stdin); 20 freopen("palsquare.out", "w", stdout); 21 int base; 22 cin >> base; 23 char* palNumber; 24 char* palSquare; 25 char* reverseSquare; 26 for(int i = 1; i <= 300; i++) { 27 if(!palNumber) { 28 delete palNumber; 29 palNumber = NULL; 30 } 31 palNumber = base10ToBaseN(i, base); 32 if(!palSquare) { 33 delete palSquare; 34 palSquare = NULL; 35 } 36 palSquare = base10ToBaseN(i * i, base); 37 if(!reverseSquare) { 38 delete palSquare; 39 palSquare = NULL; 40 } 41 reverseSquare = reverse(palSquare, strlen(palSquare)); 42 for(int j = 0; j < strlen(palSquare); j++) { 43 if(palSquare[j] != reverseSquare[j]) { 44 break; 45 } 46 if(j == strlen(palSquare) - 1) { 47 cout << palNumber << " " << palSquare << endl; 48 } 49 } 50 } 51 return 0; 52 } 53 54 char* reverse(const char* src, int len) { 55 char* result = new char[len+1]; 56 for(int i = 0; i < len; i++) { 57 result[i] = src[len - 1 - i]; 58 } 59 result[len] = 0; 60 return result; 61 } 62 63 char* base10ToBaseN(int number, int base) { 64 char* result = new char[10]; 65 int len = 0; 66 while(number != 0) { 67 result[len] = baseTable[number % base]; 68 number /= base; 69 len++; 70 } 71 result[len] = 0; 72 return reverse(result, strlen(result)); 73 }
这是原来的代码,在本机跑的时候没有问题,但是在评测的时候报错了。错误信息如下:
program:_malloc.c:2392:_sysmalloc:_Assertion_`(old_top_==_initial_top_(av)_&&_old_size_==_0)_||_((unsigned_long)_(old_size)_>=_MINSIZE_&&_prev_inuse_(old_top)_&&_((unsigned_long)_old_end_&_(pagesize_-_1))_==_0)‘_failed. -------------------
在查阅资料之后,应该是说我的代码越界使用了内存,于是我重新写了代码。
1 /** 2 ID: njuwz151 3 TASK: palsquare 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <string> 10 11 using namespace std; 12 13 string baseTable = "0123456789ABCDEFGHIJK"; 14 15 string reverse(string src); 16 17 string base10ToBaseN(int number, int base); 18 19 int main() { 20 freopen("palsquare.in", "r", stdin); 21 freopen("palsquare.out", "w", stdout); 22 int base; 23 cin >> base; 24 for(int i = 1; i <= 300; i++) { 25 string palNumber = base10ToBaseN(i, base); 26 string palSquare = base10ToBaseN(i * i, base); 27 string reverseSquare = reverse(palSquare); 28 for(int j = 0; j < palSquare.size(); j++) { 29 if(palSquare[j] != reverseSquare[j]) { 30 break; 31 } 32 if(j == palSquare.size() - 1) { 33 cout << palNumber << " " << palSquare << endl; 34 } 35 } 36 } 37 38 return 0; 39 } 40 41 string reverse(string src) { 42 int len = src.size(); 43 string result; 44 for(int i = len - 1; i > -1; i--) { 45 result += src[i]; 46 } 47 return result; 48 } 49 50 string base10ToBaseN(int number, int base) { 51 string result; 52 while(number > 0) { 53 result = baseTable[number % base] + result; 54 number /= base; 55 } 56 return result; 57 }
使用了string 来替代数组,说起来我也不是专业写C/C++的,之前char数组和string混合使用出错了,就单用char数组解决问题了,但是这次没能用好char数组,说明我对内存管理还是有一定的认知缺乏。
原文:http://www.cnblogs.com/NJUWZ/p/6953289.html