KY108 Day of Week
描述
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2005, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
思路
用map来存储月份的对应关系
代码
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<string>
using namespace std;
int daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool IsLeapYear(int x) {
return (x % 4 == 0 && x % 100 != 0) || (x % 400 == 0);
}
int main() {
int year, MonthNumber, day;
char str[100];
string month;
bool isBefore;
map<string, int> monthName = {
{"January", 1},
{"February", 2},
{"March", 3},
{"April", 4},
{"May", 5},
{"June", 6},
{"July", 7},
{"August", 8},
{"September", 9},
{"October", 10},
{"November", 11},
{"December", 12},
};
string weekday[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
while ((scanf("%d%s%d", &day, str, &year)) != EOF) {
month = str;
MonthNumber = monthName[month];
if (year < 2023
|| 2023 == year && MonthNumber < 3
|| 2023 == year && MonthNumber == 3 && day < 3) {
isBefore = true;
} else
isBefore = false;
int beginYear, beginMonth, beginDay, endYear, endMonth, endDay;
if (isBefore) {
beginYear = year;
beginMonth = MonthNumber;
beginDay = day;
endYear = 2023;
endMonth = 3;
endDay = 2;
} else {
beginYear = 2023;
beginMonth = 3;
beginDay = 2;
endYear = year;
endMonth = MonthNumber;
endDay = day;
}
int totalDay = 0;
while (true) {
if (beginYear == endYear && beginMonth == endMonth && beginDay == endDay)
break;
totalDay++;
bool isLeap = IsLeapYear(beginYear);
beginDay++;
if (beginDay > daytab[isLeap][beginMonth]) {
beginDay = 1;
beginMonth++;
if (beginMonth > 12) {
beginMonth = 1;
beginYear++;
}
}
}
if (isBefore) {
printf("%s\n", weekday[(11 - totalDay % 7) % 7].c_str());
} else {
printf("%s\n", weekday[(totalDay + 4) % 7].c_str());
}
}
return 0;
}
问题
C6064:缺少“scanf_s”的整型参数
scanf_s中输入char类型需要在后面输入参数
KY9 成绩排序
描述
用一维数组存储学号和成绩,然后,按成绩排序输出。
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。 接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。 如果学生的成绩相同,则按照学号的大小进行从小到大排序。
思路
用struct建立学生结构体数组
对结构体进行排序
用sort函数进行排序
bool一个自定义排序规则
代码
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Student {
int num;
int score;
};
bool MyCompare(Student lhs, Student rhs) {
if (lhs.score < rhs.score)
return true;
else if (lhs.score == rhs.score && lhs.num < rhs.num)
return true;
else{
return false;
}
}
int main() {
Student arr[100];
int n;
scanf("%d", &n);
for (size_t i = 0; i < n; i++)
{
scanf("%d%d", &arr[i].num, &arr[i].score);
}
sort(arr, arr + n, MyCompare);
for (size_t i = 0; i < n; i++) {
printf("%d %d\n", arr[i].num, arr[i].score);
}
return 0;
}