zoj有关问题
zoj问题
求大神指导 为什么我的代码一直提示越界问题 我是在看不出来哪越界了 求大神指导 都弄一晚上了
题目链接:http://ac.jobdu.com/problem.php?pid=1006
------解决思路----------------------
判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
求大神指导 为什么我的代码一直提示越界问题 我是在看不出来哪越界了 求大神指导 都弄一晚上了
题目链接:http://ac.jobdu.com/problem.php?pid=1006
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using std ::cin; using std ::cout; using std ::endl; using std ::string; using std ::search;
using std ::equal; using std ::find; using std ::ifstream; using std ::ios_base;
typedef string ::iterator iter;
const string s_zoj = "zoj";
bool judge( string & );
int main( void )
{
//ifstream cin;
//cin.open( "textFile//in.txt" , ios_base ::in );
string s;
while( cin >> s )
{
// 先谈论 第一种情况
if( s == s_zoj ) cout << "Accepted" << endl;
else
{
iter first_z = find( s.begin() , s.end() , 'z' );
iter first_j = find( s.begin() , s.end() , 'j' );
if( first_z == s.end() || first_j == s.end() )
{
cout << "Wrong Answer" << endl;
continue;
}
int count_of_o = 0;
for( iter p = first_z ; p != first_j + 1 ; p ++ )
{
if( * p == 'o' ) count_of_o ++;
}
if ( count_of_o == 0 ) cout << "Wrong Answer" << endl;
else // 第二中情形可以归入第三种情形 第三种情形
{
// 依照网上的说法 z o j 第一个z之前的 o的个数 乘以 z与o之间o的个数 就是j之后 o的个数
// 并且 这三部分之间必须全部是o
int flag = 1;
int cnt_left = 0 , cnt_mid = 0 , cnt_right = 0;;
// 计算第一部分的o 并同时判断
for( iter p = s.begin() ; p != first_z ; p ++ )
{
if( * p != 'o' )
{
flag = 0;
break;
}
cnt_left ++;
}
// 计算第二部分的o 同时并判断
if( flag )
{
for( iter p = first_z + 1 ; p != first_j ; p ++ )
{
if( * p != 'o' )
{
flag = 0;
break;
}
cnt_mid ++;
}
if( flag )
{
for( iter p = first_j + 1 ; p != s.end() ; p ++ )
{
if( * p != 'o' )
{
flag = 0;
break;
}
cnt_right ++;
}
if( flag )
{
if( cnt_left * cnt_mid == cnt_right ) cout << "Accepted" << endl;
else
cout << "Wrong Answer" << endl;
}
else
cout << "Wrong Answer" << endl;
}
else
cout << "Wrong Answer" << endl;
}
else
cout << "Wrong Answer" << endl;
}
}
}
return 0;
}
------解决思路----------------------
判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}