2017-2-18 POJ6377

http://ica.openjudge.cn/struct/1/
第一次用string类,踩的坑挺多,代码改了一晚上还是吗没有AC。
主要两点:
1.vector和string类里面的size和length函数返回值是size_type类型,是unsigned类型。因此s1.length()>s2.length()与s1.length()-s2.length()>0不等价,因为第二个式子为unsignded类型,恒为真。除此之外int j=s1.length()-s2.length()可以得到正确结果,因为强制转换成int;
2.string中用data成员函数可以转换成c中的字符数组形式的字符串。
3.bool类型除了0之外的值都是视作1,而非正数为1负数为0,重载>等比较运算符时尤其注意这一点。
4.第一次用了VS的单步调试。以及VS2013变得很智能,各种方便。鼠标放在某一个数据名上面自动显示数据类型,如果早点知道就不会因为unsigned类型花费了一晚上。

说到底还是不知道为什么WA啊,真的真的花了整整一个晚上,不过学到的东西也挺多。为了下学期做准备。

代码附上,纪念我的第一篇博客:

#include<iostream>
#include<string>
using namespace std;
typedef struct{
    string name;
    int m;
    int d;
    int sort_temp;
}Stu;
int operator-(string s1, string s2){
    for (int i = 0; i < s1.size() + 1; i++){
        if (s1.data()[i] == s2.data()[i])
            i++;
        else
        {
            return s1.data()[i] - s2.data()[i];
        }
        return 0;
    }
}
bool operator>(string s1, string s2){
    if (s1.length() == s2.length()){
        if (s1 - s2 > 0)return true;
        else return false;
    }
    else if (s1.length() >s2.length())return true;
    /*else if ((s1.length() - s2.length()) > 0)return true;
    */
    else return false;
}
bool operator>(Stu s1, Stu s2){
    if (s1.sort_temp > s2.sort_temp)
        return true;
    else if (s1.sort_temp == s2.sort_temp)
    {
        if (s1.name > s2.name)
            return true;
    }
    return false;
}
int main(){
    int n;
    cin >> n;
    if (n <= 0){
        cout << "None" << endl;
        system("pause");
        return 0;
    }
    /*string s1("Bill"), s2("Callf");
    int j = s1.length() - s2.length();
    cout << j;*/
Stu *s=new Stu[n];
for (int i = 0; i < n; i++){
    cin >> s[i].name >> s[i].m >> s[i].d;
    s[i].sort_temp = s[i].m * 100 + s[i].d;
}
for (int i = 0; i < n; i++){
    for (int j = 0; j < n - 1-i; j++){
        if (s[j]>s[j + 1])
        {
            Stu temp = s[j];
            s[j] = s[j + 1];
            s[j + 1] = temp;
        }
    }
}
int i = 0;
/*for (int i = 0; i < n; i++){
    cout << s[i].name << " " << s[i].sort_temp<<endl;
}*/
bool flag = false;
while(i<n){

    if (s[i].sort_temp == s[i + 1].sort_temp){
        flag = true;
        cout << s[i].m << " " << s[i].d << " " << s[i].name;
        while (s[i].sort_temp == s[i + 1].sort_temp){
            i++;
            cout << " " << s[i].name;
        }
        cout << endl;
    }
    i++;
    }
if (flag == false)cout << "None"<<endl;
system("pause");
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值