输入输出示例
输入:
9 October 2001
14 October 2001
输出:
Tuesday
Sunday
【原题链接】
字符串处理
C风格的字符串
- 字符数组,以’\0‘结尾
- 建议在输入输出语句中使用
C++风格的字符串
#include <string>
using namespace std;
- 初始化:string str1 = str;//world
- 连接: str1 + “hello” //即"worldhello"
- 字符:str1[0]//即’w’
- 长度:str1.length();
- 判断相符:str1 == “world”
- 比较字典顺序:str1 > “abandon”
- 从C++风格到C风格:str1.c_str();
字符串到数字的对应:map映射
#include <map>
using namespace std;
map<string,string> myMap = {
//<键key的类型,值value的类型>
{"Caixukun","ikun"},
{"Wuyifan","meigeni"}
};
char str[100];
scanf("%s",str);
string name = str;
printf("%s的粉丝被称为%s\n",name.c_str(),myMap[name].c_str());
星期的计算
根据今天是星期几,计算要求日期距离今天的距离,然后计算其星期数即可
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int main() {
map<string,int> month2int = {
{"January",1},
{"February",2},
{"March",3},
{"April",4},
{"May",5},
{"June",6},
{"July",7},
{"August",8},
{"September",9},
{"October",10},
{"November",11},
{"December",12}
};
int month_Day[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string int2Weekday[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int year, mon, day;
char str[100];
string month;
bool isBefore;//在过去还是在未来
while(scanf("%d%s%d",&day,str,&year)!=EOF){
month = str;//把字符串从C风格转换成C++风格
mon = month2int[month];
if (year < 2024||2024 == year && mon <3 || 2024 ==year &&mon==3&&day <10){
isBefore= true;
}else{
isBefore= false;
}
//从begin走到end
int beginYear, beginMon, beginDay, endYear, endMonth, endDay;
if (isBefore){
beginYear = year;
beginMon = mon;
beginDay = day;
endYear = 2024;
endMonth = 3;
endDay = 10;
} else{
beginYear = 2024;
beginMon = 3;
beginDay = 10;
endYear = year;
endMonth = mon;
endDay = day;
}
//2024年3月10日是星期日
int totalDay = 0;
while(true){
if (beginYear==endYear&&beginMon==endMonth&&beginDay==endDay){
break;
}
++totalDay;
//next day
bool isLeap = beginYear%400==0||beginYear%100!=0&&beginYear%4==0;
if(isLeap){
month_Day[2]=29;
} else{
month_Day[2]=28;
}
++beginDay;
if (beginDay>month_Day[beginMon]){
beginDay = 1;
++beginMon;
if (beginMon>12){
beginMon=1;
beginYear++;
}
}
}
if (isBefore){
printf("%s\n",int2Weekday[7-totalDay%7].c_str());
}else{
printf("%s\n",int2Weekday[(totalDay)%7].c_str());
}
}
return 0;
}