本题思路简单,先构造出1000以内的英文数字表示放在string数组d000中,然后把10^9内的数字分成3段各1000以内的串s3,s2,s1,然后取得对应的d000下标p3,p2,p1,用p3*1000000+p2*1000+p1得到结果。其中把10^9内的数字分成3段的函数split用substr得到错误“Restricted Function”。郁闷了好久!
终于知道问题所在!Zju 2971 Give Me the Number不再困惑了。原来是用substr时要用注意起始位置参数要小于串长,长度参数的值必须>0。注意类似于one thousand的测试数据。下面是我的分隔函数。
void
split(
string
t,
string
&
s3,
string
&
s2,
string
&
s1)
... {
int i=t.find(" million");
if (i!=-1)
...{
s3=t.substr(0,i);
if (i+9<t.size()) t=t.substr(i+9);
}
int j=t.find(" thousand");
if (j!=-1)
...{
s2=t.substr(0,j);
if (j+10<t.size()) t=t.substr(j+10);
}
s1=t;
}
... {
int i=t.find(" million");
if (i!=-1)
...{
s3=t.substr(0,i);
if (i+9<t.size()) t=t.substr(i+9);
}
int j=t.find(" thousand");
if (j!=-1)
...{
s2=t.substr(0,j);
if (j+10<t.size()) t=t.substr(j+10);
}
s1=t;
}