mode-c++

/*感谢机房JYW的友情馈赠*/
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector>
#include <set>
#include <queue> #define Orz __attribute__((optimize("-O2"))) #define lx 2000 using namespace std;  vector<int>::iterator it;  vector<int> a[100010];
struct edge{ int u,v,next; }eg[maxm]; int f[5][5]={{0,3,2,3,2},{3,2,1,2,3},{2,1,4,3,2},{3,2,3,2,3},{2,3,2,3,4}}; int mm(int a,int b, int &c){ return 0; } int main(){ freopen("stdin.in","r",stdin); freopen("stdout.out","w",stdout); //文件输入输出 int cas,n; scanf("%d",&cas); while (cas--){ } //处理多组数据 char vac[lx][lx]; scanf("%d",&n); scanf("%c",&vac[0][0]); for (int i=1;i<=n;i++) scanf("%s",vac[i] + 1);//读入字符串 int k[lx]; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",k+i); sort(k+1,k+n+1); //从1到n默认从小到大排序 fclose(stdin); fclose(stdout); exit(0); //退出整个程序,fclose可不写 int a=0,b; b=a++; printf("a=%d b=%d\n",a,b); b=++a; printf("a=%d b=%d\n",a,b); //前者是先赋值后加,后者先加后赋值 int p; scanf("%d",&p); switch (p){ case 0: case 1: case 2:printf("Orz Jry\n"); break; case 3:printf("Orz Jc\n"); break; case 4: case 5:printf("Orz Dzy\n"); break; } //相当于case,但是单独一个case:右边一定要加break,不然会执行其他case   int n;   scanf("%d",&n);   for (int i=1;i<=n;i++)   {     int x;     scanf("%d",&x);     a[0].push_back(x);   }   for (it=a[0].begin();it!=a[0].end();it++)   cout<<*it<<" "; return 0; queue<int> Q; //定义队列Q,定义时会初始化的 while(!Q.empty()) Q.pop(); //清空队列 Q.push(1); //1入队 Q.pop(); //队首元素出队 int v=Q.front(); //赋值Q的队首元素给v int s=Q.size(); //返回队列大小 set<int> S; S.insert(3); //插入3,元素不会重复 S.insert(9); S.insert(10); S.erase(S.find(10)); //删除10 for(set<int>::iterator i=S.begin();i!=S.end();++i) printf("%d ",*i); //从小到大输出 printf("\n"); }

 

转载于:https://www.cnblogs.com/rpSebastian/p/4178874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Dev-C++编写贪吃蛇的基本步骤: 1. 创建一个新的C++项目,并命名为“贪吃蛇”。 2. 在项目中创建一个新的C++源文件,并将其命名为“main.cpp”。 3. 在“main.cpp”文件中,包含必要的头文件和命名空间: ``` #include <iostream> #include <conio.h> #include <windows.h> using namespace std; ``` 4. 定义贪吃蛇和食物的结构体: ``` struct Snake{ int x, y; }; struct Food{ int x, y; }; ``` 5. 定义全局变量和常量: ``` const int mapWidth = 20; const int mapHeight = 20; const int snakeMaxLen = 100; const int interval = 100; int direction = 0; int score = 0; int snakeLen = 3; Snake snake[snakeMaxLen]; Food food; ``` 6. 初始化游戏窗口和贪吃蛇的初始位置: ``` void init(){ system("mode con cols=40 lines=25");//设置窗口大小 srand((unsigned)time(NULL));//初始化随机数种子 snake[0].x = 10; snake[0].y = 10; snake[1].x = 9; snake[1].y = 10; snake[2].x = 8; snake[2].y = 10; food.x = rand() % mapWidth; food.y = rand() % mapHeight; } ``` 7. 定义绘制函数,用于绘制游戏界面: ``` void draw(){ system("cls");//清屏 for(int i = 0; i < mapHeight; i++){ for(int j = 0; j < mapWidth; j++){ if(i == 0 || i == mapHeight - 1 || j == 0 || j == mapWidth - 1){ cout << "#";//绘制边框 }else if(i == snake[0].y && j == snake[0].x){ cout << "H";//绘制蛇头 }else if(i == food.y && j == food.x){ cout << "*";//绘制食物 }else{ bool isBody = false; for(int k = 1; k < snakeLen; k++){ if(i == snake[k].y && j == snake[k].x){ cout << "o";//绘制蛇身 isBody = true; break; } } if(!isBody){ cout << " ";//绘制空格 } } } cout << endl; } cout << "Score: " << score << endl;//显示得分 } ``` 8. 定义移动函数,用于控制贪吃蛇的移动: ``` void move(){ for(int i = snakeLen - 1; i > 0; i--){ snake[i].x = snake[i - 1].x; snake[i].y = snake[i - 1].y; } switch(direction){ case 0://向上移动 snake[0].y--; break; case 1://向下移动 snake[0].y++; break; case 2://向左移动 snake[0].x--; break; case 3://向右移动 snake[0].x++; break; } } ``` 9. 定义碰撞检测函数: ``` bool check(){ if(snake[0].x == 0 || snake[0].x == mapWidth - 1 || snake[0].y == 0 || snake[0].y == mapHeight - 1){ return true;//撞到边界 } for(int i = 1; i < snakeLen; i++){ if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){ return true;//撞到自己 } } if(snake[0].x == food.x && snake[0].y == food.y){ score += 10;//吃到食物 snakeLen++; food.x = rand() % mapWidth; food.y = rand() % mapHeight; } return false; } ``` 10. 在主函数中,循环执行绘制、移动和碰撞检测等操作: ``` int main(){ init(); while(true){ if(_kbhit()){ char ch = _getch(); switch(ch){ case 'w'://向上移动 if(direction != 1){ direction = 0; } break; case 's'://向下移动 if(direction != 0){ direction = 1; } break; case 'a'://向左移动 if(direction != 3){ direction = 2; } break; case 'd'://向右移动 if(direction != 2){ direction = 3; } break; } } move(); if(check()){ cout << "Game Over!" << endl; break; } draw(); Sleep(interval);//延迟 } return 0; } ``` 11. 编译并运行程序,即可开始游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值