如何调试在OJ中的代码
Posted 去做点事情
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何调试在OJ中的代码相关的知识,希望对你有一定的参考价值。
在OJ上的原始程序:
class Solution { public: void replaceSpace(char *str,int length) {if(str == NULL || length<=0) return; int length_origin = 0; int blank = 0; while(*str != ‘\0‘){ if(*str == ‘ ‘){ blank++; length_origin++; str++; } else{ length_origin++; str++; continue; } }int length_new = length_origin+2*blank;if(length_new > length) return; else{cout<<‘b‘<<endl; char *str_new = str+2*blank;while(str_new != str){ if(*str == ‘ ‘){ *str_new = ‘0‘; *(str_new-1) = ‘2‘; *(str_new-2) = ‘%‘; str_new = str_new -3; str--; } else{ *str_new =*str; str_new--; str--; } } } } };
加入其他部分后的调试程序:
#include<iostream> 标准输入输出流 using namespace std; class Solution { public: void replaceSpace(char *str,int length) { cout<<‘a‘<<endl; if(str == NULL || length<=0) return; int length_origin = 0; int blank = 0; while(*str != ‘\0‘){ if(*str == ‘ ‘){ blank++; length_origin++; str++; } else{ length_origin++; str++; continue; } }int length_new = length_origin+2*blank;if(length_new > length) return; else{char *str_new = str+2*blank;while(str_new != str){ if(*str == ‘ ‘){ *str_new = ‘0‘; *(str_new-1) = ‘2‘; *(str_new-2) = ‘%‘; str_new = str_new -3; str--; } else{ *str_new =*str; str_new--; str--; } } } } }; int main(){ c++运行main函数 char str[] = "we are happy"; Solution replace; 类Solution实例化了一个replce对象 replace.replaceSpace(str,50); cout<<str<<endl; return 0; }
以上是关于如何调试在OJ中的代码的主要内容,如果未能解决你的问题,请参考以下文章