返回信息流bupt OJ 258. 测试了很多组,没有问题,提交运行时错误[ema1]
求大神指教。。代码如下:
#include<stdio.h>
#include<vector>
#include<string.h>
#include<string>
using namespace std;
struct Birthday
{
string year;
string month;
string day;
};
struct student
{
string Name;
string Sex;
struct Birthday Birth;
};
int main()
{
int T;
scanf( "%d", &T );
while( T-- )
{
int N, M;
scanf( "%d %d", &N, &M );
vector< student > StudentVector;
student tmpStudent;
int n = N;
while( n-- )
{
char tmpName[ 35 ];
char tmpSex[ 10 ];
char birth[ 20 ];
scanf( "%s %s %s", tmpName, tmpSex, birth );
tmpStudent.Name = tmpName;
tmpStudent.Sex = tmpSex;
string tmpString = birth;
tmpStudent.Birth.year = tmpString.substr( 0, 4 );
tmpStudent.Birth.month = tmpString.substr( 5, 2 );
tmpStudent.Birth.day = tmpString.substr( 8, 2 );
StudentVector.push_back( tmpStudent );
}
while( M-- )
{
char s;
string nameLimit, sexLimit, birthLimit, tmpString;
bool isName = false, isSex = false, isBirth = false;
fflush( stdin );
while( ( s = getchar() ) != '\n' )
{
if( s == ' ' )
{
if( isName )
nameLimit = tmpString;
else if( isSex )
sexLimit = tmpString;
else if( isBirth )
birthLimit = tmpString;
isName = isSex = isBirth = false;
tmpString = "";
continue;
}
else if( s == '=' )
{
if( tmpString.compare( "Name" ) == 0 )
{
isName = true;
}
else if( tmpString.compare( "Sex" ) == 0 )
{
isSex = true;
}
else if( tmpString.compare( "Birthday" ) == 0 )
{
isBirth = true;
}
tmpString = "";
continue;
}
else if( s == '\'' )
{
continue;
}
tmpString += s;
}
if( isName )
nameLimit = tmpString;
else if( isSex )
sexLimit = tmpString;
else if( isBirth )
birthLimit = tmpString;
string yearLimit, monthLimit, dayLimit;
int birthLen = birthLimit.length();
tmpString = "";
int birthPos = 0;
if( birthLen > 0 )
{
for( int i = 0; i < birthLen; i++ )
{
if( birthLimit[ i ] == '\/' )
{
birthPos++;
if( birthPos == 1 && tmpString.compare( "*" ) )
{
yearLimit = tmpString;
}
else if( birthPos == 2 && tmpString.compare( "*" ) )
{
monthLimit = tmpString;
}
tmpString = "";
continue;
}
tmpString += birthLimit[ i ];
}
if( tmpString.compare( "*" ) )
{
dayLimit = tmpString;
}
}
bool existElem = false;
for( int i = 0; i < N; i++ )
{
if( ( ! StudentVector[ i ].Name.compare( nameLimit ) || nameLimit.empty() ) &&
( ! StudentVector[ i ].Sex.compare( sexLimit ) || sexLimit.empty() ) &&
( ! StudentVector[ i ].Birth.year.compare( yearLimit ) || yearLimit.empty() ) &&
( ! StudentVector[ i ].Birth.month.compare( monthLimit ) || monthLimit.empty() ) &&
( ! StudentVector[ i ].Birth.day.compare( dayLimit ) || dayLimit.empty() ) )
{
for( int j = 0; j < StudentVector[ i ].Name.length(); j++ )
{
printf( "%c", StudentVector[ i ].Name[ j ] );
}
existElem = true;
printf( "\n" );
}
}
if( ! existElem )
printf( "NULL\n" );
}
}
return 0;
}
这是一条镜像帖。来源:北邮人论坛 / acm-icpc / #88937同步于 2016/2/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ACM_ICPC机器人发帖
bupt oj 258.运行时错误。。求助
yyss1230
2016/2/28镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
好长,不想看。给一个我AC的代码供你参考。刚看了一下统计结果,194ms,排第一,哈哈
/*
USER_ID: test#yingzinanfei
PROBLEM: 258
SUBMISSION_TIME: 2015-03-27 10:37:08
*/
#include<stdio.h>
#include<string.h>
struct student{
char name[50];
char sex[10];
int year;
int month;
int day;
}stu[501];
student match;
void make(char s[]){
char first = s[0];
switch(first){
case 'N':{
int i;
for(i = 6; s[i] != '\''; i++){
match.name[i - 6] = s[i];
}
match.name[i - 6] = 0;
break;
}
case 'S':{
int i;
for(i = 5; s[i] != '\''; i++){
match.sex[i - 5] = s[i];
}
match.sex[i - 5] = 0;
break;
}
case 'B':{
int diff = 0;
if(s[10] == '*'){
diff -= 3;
}
else {
int y = 0;
for(int j = 10; j <= 13; j++){
y = y * 10 + s[j] - '0';
}
match.year = y;
}
if(s[15 + diff] == '*'){
diff -= 1;
}
else {
match.month = (s[15 + diff] - '0') * 10 + (s[16 + diff] - '0');
}
if(s[18 + diff] != '*'){
match.day = (s[18 + diff] - '0') * 10 + (s[19 + diff] - '0');
}
}
}
}
int main(){
int t;
while(scanf("%d", &t) != EOF){
for(int a = 0; a < t; a++){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i<= n; i++){
scanf("%s%s%d/%d/%d", stu[i].name, stu[i].sex, &stu[i].year, &stu[i].month, &stu[i].day);
}
for(int i = 1; i <= m; i++){
match.name[0] = match.sex[0] = 0;
match.year = match.month = match.day = -1;
char s[100];
getchar();
scanf("%[^\n]", s);
char inst[4][100];
int idx = 0;
int which = 1;
int j;
for(j = 0; s[j] != 0; j++){
if(s[j] == ' '){
inst[which][j - idx] = 0;
idx = j + 1;
which ++;
}
else {
inst[which][j - idx] = s[j];
}
}
inst[which][j - idx] = 0;
for(int k = 1; k <= which; k++){
make(inst[k]);
}
bool flag = false;
for(int k = 1; k <= n; k++){
if((match.name[0] == 0 || strcmp(stu[k].name, match.name) == 0) &&
(match.sex[0] == 0 || strcmp(stu[k].sex, match.sex) == 0) &&
(match.year == -1 || stu[k].year == match.year) &&
(match.month == -1 || stu[k].month == match.month) &&
(match.day == -1 || stu[k].day == match.day)) {
printf("%s\n", stu[k].name);
flag = true;
}
}
if(flag == false) printf("NULL\n");
}
}
}
return 0;
}
【 在 yyss1230 的大作中提到: 】
: bupt OJ 258. 测试了很多组,没有问题,提交运行时错误
: 求大神指教。。代码如下:
: #include<stdio.h>
: ...................
这种处理方式不错,多谢啦~
OJ没法调试,虐心。。
【 在 chenxiansf 的大作中提到: 】
: 好长,不想看。给一个我AC的代码供你参考。刚看了一下统计结果,194ms,排第一,哈哈
: [code=c]
: /*
: ...................
虽然不知道你的运行时错误时什么,但是我知道你的代码在本地运行就不会正确!struct是C风格的数据类型,其特征是为一个特定的struct结构分配确定的内存空间!!!而string类型是C++动态长度类型,其大小是不确定的,你如果将其包含在struc中,编译会通过,但是运行时就会错误!如果要使用struct类,那么就不要在其中使用vector和string等C++动态长度类型。
原来是这么回事。。谢谢你的回复~
【 在 liuyaqiu 的大作中提到: 】
: 虽然不知道你的运行时错误时什么,但是我知道你的代码在本地运行就不会正确!struct是C风格的数据类型,其特征是为一个特定的struct结构分配确定的内存空间!!!而string类型是C++动态长度类型,其大小是不确定的,你如果将其包含在struc中,编译会通过,但是运行时就会错误!如果要使用struct类,那么就不要在其中使用vector和string等C++动态长度类型。