比较大小 题解

题目在主页,如有出错请指出
1.

include <bits/stdc++.h>

using namespace std;

int main()
{
string s1, s2;
cin >> s1 >> s2;
int l1 = s1.size(), l2 = s2.size(), ans;
if (l1 > l2) ans = 1;
else if (l1 < l2) ans = -1;
else
{
if (s1 > s2) ans = 1;
else if (s1 < s2) ans = -1;
else ans = 0;
}
cout << ans << endl;
return 0;
}
2.

include <bits/stdc++.h>

using namespace std;

int main()
{
string a, b;
cin >> a >> b;
int len1 = a.size();
int len2 = b.size();
if (len1 > len2)
{
cout << 1 << endl;
return 0;
}
else if (len1 < len2)
{
cout << -1 << endl;
return 0;
}
else
{
for (int i = 1; i < len1; i++)
{
int t1 = a[i] - '0', t2 = b[i] - '0';
if (t1 > t2)
{
cout << 1 << endl;
return 0;
}
else if (t1 < t2)
{
cout << -1 << endl;
return 0;
}
}
}
cout << 0 << endl;
return 0;
}

posted @ 2025-05-10 10:56  嵇若凌  阅读(5)  评论(0)    收藏  举报