Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:我的代码,为什么总是wrong answer呢?

Posted by qdb at 2008-07-17 16:42:54 on Problem 2121
In Reply To:我的代码,为什么总是wrong answer呢? Posted by:tq010or at 2008-06-02 15:41:24
#include <iostream>
using namespace std;
char str[28][20]=
{
 "zero","one","two","three",
 "four", "five", "six", "seven", "eight", 
 "nine", "ten", "eleven", "twelve", "thirteen", 
 "fourteen", "fifteen", "sixteen", "seventeen", 
 "eighteen", "nineteen", "twenty", "thirty", 
 "forty", "fifty", "sixty", "seventy", "eighty", 
 "ninety"
};

int value[28]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};



void fun(char s[],int & size)
{
	size=0;
	int i=0;
	int j=0;
	j++;
	while(s[i]!='\0')
	{
		if(s[i]==' '){s[i]='\0';j++;}
		i++;
	}
	size=j;
}


int fun1(char s[])
{
  int i;
  for(i=0;i<28;i++)
  {
	  if(strcmp(s,str[i])==0)break;
  }
  return value[i];
}

int fun2(char s[],char d[],int & size)
{
 char buffer[20];
 int j=0;
 bool a=false;
 for(int i=0;i<size;i++)
 {
  strcpy(buffer,&d[j]);
  j+=strlen(buffer);
  j++;
  if(strcmp(s,buffer)==0){size=size-i-1;a=true;break;};
 }
 if(a) return j;
 return -1;
}

int fun4(char s[],int size)
{
	int baiwan=0;
	int qian=0;
	int bai=0;
	int ge=0;

	int base=0;

	int size2=size;
	int pos=fun2("million",&s[base],size);
	if(pos!=-1)
	{
		size2=size2-size-1;
		baiwan=fun4(&s[base],size2)*1000000;
		base=base+pos;
	}

	size2=size;
	pos=fun2("thousand",&s[base],size);
	if(pos!=-1)
	{
		size2=size2-size-1;
		qian=fun4(&s[base],size2)*1000;
		base=base+pos;
	}

	size2=size;
	pos=fun2("hundred",&s[base],size);
	if(pos!=-1)
	{  
		size2=size2-size-1;
		bai=fun4(&s[base],size2)*100;
		base=base+pos;
	}
    
	char buffer[20];
	for(int i=0;i<size;i++)
	{
		strcpy(buffer,&s[base]);
		ge+=fun1(buffer);
		base+=strlen(buffer);
		base++;
	}

	return baiwan+qian+bai+ge;
}


int main()
{
   // freopen("in.txt","r",stdin);
	char buffer[400];
	int size;
	char * p=NULL;

	while(1)
	{
	//
	cin.getline(buffer,400);
	//if(cin.eof())break;
	if(strcmp(buffer,"")==0)break;
	fun(buffer,size);//farmat
	bool a=false;
	p=buffer;
	if(strcmp(buffer,"negative")==0){a=true;p=buffer+strlen(buffer)+1;size--;}
	int result=fun4(p,size);
	if(a)result=-result;
	cout<<result<<endl;
	}

	return 0;
}

/*
刚出炉的
我也刚刚编译通过
心情好  送你去研究吧

采用递推的思路  简单好理解

注意它的输入文件末尾以空行结束 也就是以空串结束
就这点不知道浪费了多少人的宝贵年华  TMD
*/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator