Digits Are Not Just Characters
题意:
比较大小,如果比目标字符串大输出“+”,相等也输出“+”,小则输出“-”: 比较规则: 字母大于数字 两个字母比较按照ASCII码 当被解释为十进制数时,两个数字项按其值排序。
Number items come before letter items
.
Two letter items are ordered by their ASCII codes
.
Two number items are ordered by their values when interpreted as decimal numbers
.
题解:
如果用默认比较,X52Y 大于X222,因为计算机会先比较X,X一样再比较5和2,5>2,所以前者大于后者,但实际上,按照题目规则,X比完后,再比较52和222,也就是一串数字要当做一个整体来处理, 而且数字遇上字母,字母大于数字,也就是X52Y大于32 我是这么想的,将字母和数字都转化成对应的数字, 将连续的数字转化成十进制形式,然后存在数组里;字母也存在数组里,这样之后直接比较就可以 但是如何处理数字和字母的比较呢?数字“10000”转化后大于“a”,我们可以在字母转化时加入一个绝对大值,使得数字远小于字母,但是字母之间可以比较 file[k1]=maxn+a[i] maxn=1e10 详细看代码把
代码:
#include <bits/stdc++.h>
using namespace std
;
typedef long long ll
;
const ll maxn
=1e10;
const int maxx
=130;
ll file
[maxx
];
ll file2
[maxx
];
int n
;
int main()
{
ios
::sync_with_stdio(false);
cin
>>n
;
string a
;
cin
>>a
;
int k1
=1;
for(int i
=0; i
<a
.length(); i
++)
{
if(a
[i
]>'9') file
[k1
]=maxn
+a
[i
],k1
++;
else
{
int j
=i
;
file
[k1
]=(a
[j
]-'0');
for(j
=i
+1; a
[j
]<='9'&&j
<a
.length(); j
++)
file
[k1
]=file
[k1
]*10+(a
[j
]-'0');
k1
++;
i
=j
-1;
}
}
while(n
--)
{
int k2
=1;
int f
=1;
memset(file2
,0,sizeof(file2
));
string b
;
cin
>>b
;
for(int i
=0; i
<b
.length(); i
++)
{
if(b
[i
]>'9') file2
[k2
]=maxn
+b
[i
],k2
++;
else
{
int j
=i
;
file2
[k2
]=(b
[j
]-'0');
for(j
=i
+1; b
[j
]<='9'&&j
<b
.length(); j
++)
{
file2
[k2
]=file2
[k2
]*10+(b
[j
]-'0');
}
k2
++;
i
=j
-1;
}
}
for(int i
=1;i
<k1
&&i
<k2
;i
++)
{
if(file
[i
]<file2
[i
])
{ f
=0;
cout
<<"+"<<endl
;
break;
}
if(file
[i
]>file2
[i
]) {
f
=0;
cout
<<"-"<<endl
;
break;
}
if(file
[i
]==file2
[i
]) continue;
}
if(f
==1&&k1
==k2
) cout
<<"+"<<endl
;
if(f
==1&&k1
>k2
) cout
<<"-"<<endl
;
if(f
==1&&k1
<k2
) cout
<<"+"<<endl
;
}
return 0;
}