1028 人口普查
搞了很长时间,写了一堆臭代码,测试点五一直显示段错误,就是16分,代码如下:
#include
#include
#include
#include<bits/stdc++.h>
#include
using namespace std;
#include
struct day
{
int year;
int month;
int day;
};
struct ddd
{
string name;
string born;
};
int count(int year, int month, int day)
{
int sum = 0;
int months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
for (int i = 0; i < year; ++i)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
sum += 366;
}
else
sum += 365;
}
for (int i = 0; i < month - 1; ++i)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
months[1] = 28;
sum += months[i];
}
else
{
months[1] = 29;
sum += months[i];
}
}
sum += day;
return sum;
}
int main()
{
int N;
int ss = 0;
string name, borndate;
int da, month, year;
int* a = new int(100000);
ddd *p=new ddd[98888];
cin >> N;
for (int i = 0; i < N; ++i)
{
cin >> name >> borndate;
p[i].born = borndate;
p[i].name = name;
}
int max = 0,min=10000000;
day d[10000];
int j,k;
for (int i = 0; i < N; ++i)
{
year =stoi( p[i].born.substr(0, 4));
month = stoi(p[i].born.substr(5, 2));
da = stoi(p[i].born.substr(8, 2));
int days = count(year, month, da);
int chazhi = count(2014, 9, 6) - count(1814, 9, 6);
if (count(2014, 9, 6) - days <= chazhi && count(2014, 9, 6) >= days)
{
if (count(2014, 9, 6) - days > max)
{
max = count(2014, 9, 6) - days;
j= i;
}
if (count(2014, 9, 6) - days < min)
{
min = count(2014, 9, 6) - days;
k = i;
}
d[i].day = da;
d[i].month = month;
d[i].year = year;
++ss;
}
}
if (ss == 0)
cout << ss;
else
cout << ss<<" "<<p[j].name<<" "<<p[k].name << endl;
delete[]a;
delete [] p;
system("pause");
return 0;
}
优秀代码如下,一起学习
#include
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
string name1, name2;
string maxbirth = “1814/09/06”, minbirth = “2014/09/06”;
while (n–) {
string s, birth;
cin >> s >> birth;
if (birth >= “1814/09/06” && birth <= “2014/09/06”) {
count++;
if (birth > maxbirth) {//找出最年轻的人
maxbirth = birth;
name1 = s;
}
if (birth < minbirth) {//找出最年长的人
minbirth = birth;
name2 = s;
}
}
}
if (!count) cout << count;
else cout << count << " " << name2 << " " << name1;
return 0;
}
没有想到这个思路的原因之一是并不知道字符串还可以这样比较傲,神奇