七八个星天外,两三点雨山前 (琐记)

《西江月·夜行黄沙道中》     【南宋】辛弃疾

明月别枝惊鹊,清风半夜鸣蝉。稻花香里说丰年,听取蛙声一片。
七八个星天外,两三点雨山前。旧时茅店社林边,路转溪桥忽见。


1·memset( )函数

点击打开链接

  1. const关键字

常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。不管出现在任何上下文都是为这个目的而服务的。    

  1. const使用方法

  • 定义const对象

    const修饰符可以把对象转变成常数对象,意思就是说利用const进行修饰的变量的值在程序的任意位置将不能再被修改,就如同常数一样使用!任何修改该变量的尝试都会导致编译错误:

        

    注意:因为常量在定以后就不能被修改,所以定义时必须初始化:


3·  max min

#include<algorithm>
using namespace std;
 
 
  • 1
  • 2
  • 1
  • 2

4·typedef

typedef Long Long  LL;

点击打开链接



5.lower_bound()


函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置

举例如下:

一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标

pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。

pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。

pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。

所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~

返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置




6.strchr

头文件:#include <cstring> (c++)或#include <string.h> (c)

功能:查找字符串s中首次出现字符c的位置

说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的俄罗斯方块游戏的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #define WIDTH 10 #define HEIGHT 20 int board[HEIGHT][WIDTH]; int shape[7][4][4] = { {{0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0}}, {{0,0,0,0}, {0,2,2,0}, {0,2,2,0}, {0,0,0,0}}, {{0,0,0,0}, {0,0,3,3}, {0,3,3,0}, {0,0,0,0}}, {{0,0,0,0}, {0,4,4,0}, {4,4,0,0}, {0,0,0,0}}, {{0,0,0,0}, {5,5,0,0}, {0,5,5,0}, {0,0,0,0}}, {{0,0,0,0}, {0,6,0,0}, {6,6,6,0}, {0,0,0,0}}, {{0,0,0,0}, {0,7,0,0}, {0,7,7,0}, {0,0,7,0}} }; int pos_x, pos_y; int cur_shape, cur_angle; int check_block(int x, int y) { if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT || board[y][x]) return 0; return 1; } void rotate_shape(int angle) { int tmp[4][4]; int i, j; for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) tmp[i][j] = shape[cur_shape][i][j]; while (angle--) { for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) shape[cur_shape][i][j] = tmp[3-j][i]; } } void new_block() { cur_shape = rand() % 7; cur_angle = rand() % 4; pos_x = WIDTH / 2 - 2; pos_y = 0; if (!check_block(pos_x, pos_y)) { printf("Game Over!\n"); exit(0); } } void draw_board() { int i, j; system("cls"); printf("Score: 0\n"); for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { if (board[i][j]) printf("#"); else if (check_block(j, i+1)) printf("."); else printf(" "); } printf("\n"); } } void merge_block() { int i, j; for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) if (shape[cur_shape][i][j]) board[pos_y+i][pos_x+j] = shape[cur_shape][i][j]; } void check_line() { int i, j, k; for (i = HEIGHT-1; i >= 0; i--) { for (j = 0; j < WIDTH; j++) { if (!board[i][j]) break; } if (j == WIDTH) { for (k = i; k > 0; k--) for (j = 0; j < WIDTH; j++) board[k][j] = board[k-1][j]; for (j = 0; j < WIDTH; j++) board[0][j] = 0; i++; } } } int main() { int i, j, ch; srand((unsigned)time(NULL)); new_block(); while (1) { draw_board(); if (_kbhit()) { ch = _getch(); switch (ch) { case 'a': if (check_block(pos_x-1, pos_y)) pos_x--; break; case 'd': if (check_block(pos_x+1, pos_y)) pos_x++; break; case 's': if (check_block(pos_x, pos_y+1)) pos_y++; break; case 'w': rotate_shape(1); if (!check_block(pos_x, pos_y)) rotate_shape(3); break; case 'q': rotate_shape(3); if (!check_block(pos_x, pos_y)) rotate_shape(1); break; case ' ': while (check_block(pos_x, pos_y+1)) pos_y++; merge_block(); new_block(); check_line(); break; case 'r': for (i = 0; i < HEIGHT; i++) for (j = 0; j < WIDTH; j++) board[i][j] = 0; new_block(); break; case 'x': exit(0); } } if (!check_block(pos_x, pos_y+1)) { merge_block(); new_block(); check_line(); } else { pos_y++; } } return 0; } ``` 这个代码使用了Windows下的conio库,可以通过Visual Studio等IDE来编译运行。游戏操作:a/d键左右移动,s键加速下落,w键顺时针旋转,q键逆时针旋转,空格键直接落下,r键重新开始,x键退出游戏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值