2017-2-18 POJ6377

来源:互联网 发布:dct算法进行图像压缩 编辑:程序博客网 时间:2024/06/05 06:42

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
原创粉丝点击