- #import <Foundation/Foundation.h>
- #define baseYear 2006 // 定义基础年,可以方便修改
- int nomailDay[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
- int BissextileDay[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
- bool isBissextile(int year);
- int totalDaysInYear(int year,int month,int day);
- void findFriends(int days);
- // 主函数
- int main(int argc, const char * argv[]) {
- int year = 0;
- int month = 0;
- int day = 0;
-
- while (year < baseYear || month < 1 || month >12 || day < 1) {
- printf("请输入年-月-日,如:2015-4-22\n");
- scanf("%d-%d-%d",&year,&month,&day);
-
- if ((isBissextile(year) && day > BissextileDay[month-1]) || (!isBissextile(year) && day >nomailDay[month-1])) {
- year = month = day = 0;
- }
- }
- printf("这天为%d年%d月%d日。\n",year,month,day);
- findFriends(totalDaysInYear(year, month, day));
-
- return 0;
- }
- bool isBissextile(int year) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
- return true;
- }
- else {
- return false;
- }
- }
- int totalDaysInYear(int year,int month,int day){
- int totalDays = 0;
-
- if (isBissextile(year)) {
- for (int i = 0; i < month - 1; i++) {
- totalDays += BissextileDay[i];
- }
- }
- else {
- for (int i = 0; i < month - 1; i++) {
- totalDays += nomailDay[i];
- }
- }
- totalDays += day;
- for (int i = baseYear; i < year; i++) {
-
- if (isBissextile(i)) {
- totalDays += 366;
- }
- else {
- totalDays += 365;
- }
- }
- printf("这天是从2006年1月1日开始的第%d天。\n",totalDays);
- return totalDays;
- }
- void findFriends(int days){
- if (days % 6 == 0) {
- printf("小明这天同时结识美女和帅哥。\n");
- }
- else if (days % 2 == 0){
- printf("小明这天结识的是帅哥。\n");
- }
- else if (days % 3 == 0){
- printf("小明这天结识的是美女。\n");
- }
- else{
- printf("小明这天没有结识帅哥和美女。\n");
- }
- }
复制代码
放上我的代码,供楼主参考。
|