zzuli2324输入一年中两个日期,计算这两个日期相差多少天。输入输入四个整数m1,d1,m2,d2,用空格隔开,分别表示两个日期的月份和这个月的第几天。

#include<stdio.h>

#include<stdlib.h>

int main()

{q

 int i,m1,d1,m2,d2,day=0,t;

 int m[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};//用数组列出每个月天数,2月用29                                 //   是因为输入2,29会计算日期,其                                   // 他情况会输出erroor。不用管

  scanf("%d%d%d%d",&m1,&d1,&m2,&d2);     //    

  if(m1>m2||(m1==m2&&d1>d2)){       //令小日期在前

     t=m1,m1=m2,m2=t;

     t=d1,d1=d2,d2=t;

     }

     if((m1==2||m1==1)&& d1<29&& m2>2)   //计算日期中经过二月末,即无法确定日                                       的情况

    printf("error");

    else if(m1==1&&(d1==29||d1==30||d1==31)&&m2>2) //一月29,30,31的情况因为

      printf("error");             //f((m1==2||m1==1)&& d1<29&& m2>2)                                   //中不含d1等于29,30,31的情况

  else{

 for(i=m1;i<m2;i++)      //计算

    day += m[i];

   day += d2-d1;

   printf("%d",day);

  }

       return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的问题,需要考虑到游戏的界面设计、游戏逻辑、用户输入、游戏难度等多个方面。以下是一个简单的贪吃蛇小游戏设计思路: 1. 游戏界面设计 可以使用C语言的图形库(如graphics.h)来绘制游戏界面,也可以使用控制台输出字符来实现。游戏界面包括蛇、食物、障碍物等元素,可以使用不同的字符或颜色来表示。 2. 游戏逻辑 贪吃蛇游戏的核心逻辑是蛇的移动和食物的生成。蛇可以通过用户输入(如上下左右箭头键)来移动,每次移动一个格子。当蛇头碰到食物时,蛇身变长,并生成新的食物。当蛇头碰到障碍物或蛇身时,游戏结束。 3. 用户输入 可以使用C语言的getch()函数获取用户输入的键盘按键。根据用户输入来改变蛇的移动方向。 4. 游戏难度 可以通过控制食物的生成速度和障碍物的数量来控制游戏难度。随着游戏的进行,食物生成速度可以逐渐加快,障碍物数量可以逐渐增加。 下面是一个简单的C语言贪吃蛇小游戏代码示例,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #define WIDTH 40 #define HEIGHT 20 struct point { int x; int y; }; struct snake { struct point body[100]; int length; char direction; }; struct food { struct point pos; }; struct obstacle { struct point pos; }; struct snake init_snake(struct snake s, int x, int y, int length) { s.length = length; s.direction = 'r'; // 初始方向为向右 for (int i = 0; i < length; i++) { s.body[i].x = x - i; s.body[i].y = y; } return s; } struct food init_food() { struct food f; f.pos.x = rand() % WIDTH; f.pos.y = rand() % HEIGHT; return f; } struct obstacle init_obstacle() { struct obstacle o; o.pos.x = rand() % WIDTH; o.pos.y = rand() % HEIGHT; return o; } void draw(struct snake s, struct food f, struct obstacle o) { system("cls"); // 清屏 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) { printf("#"); // 绘制边框 } else if (i == s.body[0].y && j == s.body[0].x) { printf("O"); // 绘制蛇头 } else if (i == f.pos.y && j == f.pos.x) { printf("*"); // 绘制食物 } else if (i == o.pos.y && j == o.pos.x) { printf("@"); // 绘制障碍物 } else { int flag = 0; for (int k = 1; k < s.length; k++) { if (i == s.body[k].y && j == s.body[k].x) { printf("o"); // 绘制蛇身 flag = 1; break; } } if (!flag) { printf(" "); // 绘制空格 } } } printf("\n"); } } struct snake move(struct snake s) { for (int i = s.length - 1; i > 0; i--) { s.body[i] = s.body[i - 1]; // 蛇身跟随蛇头移动 } switch (s.direction) { case 'u': s.body[0].y--; break; case 'd': s.body[0].y++; break; case 'l': s.body[0].x--; break; case 'r': s.body[0].x++; break; } return s; } int collision(struct snake s, struct food f, struct obstacle o) { if (s.body[0].x == 0 || s.body[0].x == WIDTH - 1 || s.body[0].y == 0 || s.body[0].y == HEIGHT - 1) { return 1; // 撞墙 } for (int i = 1; i < s.length; i++) { if (s.body[0].x == s.body[i].x && s.body[0].y == s.body[i].y) { return 1; // 撞自己 } } if (s.body[0].x == f.pos.x && s.body[0].y == f.pos.y) { return 2; // 吃到食物 } if (s.body[0].x == o.pos.x && s.body[0].y == o.pos.y) { return 3; // 撞到障碍物 } return 0; // 未发生碰撞 } int main() { srand(time(NULL)); // 随机数种子 struct snake s; s = init_snake(s, WIDTH / 2, HEIGHT / 2, 4); struct food f; f = init_food(); struct obstacle o; o = init_obstacle(); int score = 0; while (1) { draw(s, f, o); if (kbhit()) { // 检测键盘输入 char c = getch(); switch (c) { case 'w': case 'W': if (s.direction != 'd') { s.direction = 'u'; } break; case 's': case 'S': if (s.direction != 'u') { s.direction = 'd'; } break; case 'a': case 'A': if (s.direction != 'r') { s.direction = 'l'; } break; case 'd': case 'D': if (s.direction != 'l') { s.direction = 'r'; } break; } } s = move(s); int result = collision(s, f, o); if (result == 1) { printf("Game over! Your score is %d.\n", score); break; } else if (result == 2) { s.length++; f = init_food(); score++; } else if (result == 3) { printf("Game over! You hit the obstacle.\n"); break; } Sleep(100); // 控制游戏速度 } return 0; } ``` 注意:以上代码仅供参考,可能存在一些问题和不完善的地方,需要根据实际情况进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值