一部动画片共8集

核心提示I= 116I= 125I= 134I= 143I= 152I= 161I= 215I= 224I= 233I= 242I= 251I= 314I= 323I= 332I= 341I= 413I= 422I= 431I= 512I= 521

I= 116

I= 125

I= 134

I= 143

I= 152

I= 161

I= 215

I= 224

I= 233

I= 242

I= 251

I= 314

I= 323

I= 332

I= 341

I= 413

I= 422

I= 431

I= 512

I= 521

I= 611

ans = 21

21种。

用程序写,

#include <cstdio>

using namespace std;

int sum(int x)

{

int sums = 0, tmp;

while (x != 0)

{

tmp = x % 10;

if (tmp < 1)

return 0;

sums += tmp;

x /= 10;

}

return sums;

}

int main()

{

int cnt = 0;

for (int i = 100; i < 1000; i ++)

{

if (sum(i) == 8)

{

printf("I= %dn", i);

cnt ++;

}

}

printf("ans = %dn", cnt);

return 0;

}

#include?<stdio.h>

#include?<stdlib.h>

#include?<string.h>

#include?<time.h>

#if?defined(__GNUC__)

//下面是依赖?Linux?实现

#include?<unistd.h>

#define?sleep_ms(m)?

usleep(m?*?1000)

//向上移动光标函数?Linux

static?void?__curup(int?height)

{

int?i?=?-1;

while?(++i<height)

printf("033[1A");?//先回到上一行?

}

#else?

//?创建等待函数?1s?60?帧?相当于?16.7ms?=>?1帧,?我们取16ms

//?咱么的这屏幕?推荐?1s?25帧吧?40ms

//?这里创建等待函数?以毫秒为单位?,?需要依赖操作系统实现

#include?<Windows.h>

#define?sleep_ms(m)?

Sleep(m)

//向上移动光标

static?void?__curup(int?height)

{

COORD?cr?=?{0,0};

//?GetStdHandle(STD_OUTPUT_HANDLE)?获取屏幕对象,?设置光标

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),?cr);

}

#endif?

//?定义初始屏幕的宽高像素宏

#define?_INT_WIDTH(100)

#define?_INT_HEIGHT(50)

//?屏幕刷新帧的速率

#define?_INT_FRATE(40)

//?雪花飘落的速率,相对于?屏幕刷新帧?的倍数

#define?_INT_VSNOW(10)

#define?cerr(msg,...)?

fprintf(stderr,?"[%s:%s:%d]"?msg?"n",__FILE__,__func__,__LINE__,##__VA_ARGS__);

struct?screen?{

int?frate;?//?也可以用?unsigned?结构

int?width;

int?height;

char?*pix;

};

struct?screen*?screen_create(int?frate,?int?width,?int?height);

void?screen_destory(struct?screen**?pscr);

int?screen_draw_snow(struct?screen*?scr);

void?screen_flash_snow(struct?screen*?scr);

//?主函数,主业务在此运行

int?main(int?argc,?char?*argv[])

{

struct?screen*?scr?=?NULL;

//创建一个屏幕对象

scr?=?screen_create(_INT_FRATE,?_INT_WIDTH,?_INT_HEIGHT);

if?(NULL?==?scr)

exit(EXIT_FAILURE);

//绘制雪花动画

screen_flash_snow(scr);

//销毁这个屏幕对象

screen_destory(&scr);

return?0;

}

struct?screen*

screen_create(int?frate,?int?width,?int?height)

{

struct?screen?*scr?=?NULL;

if?(frate<0?||?width?<=?0?||?height?<=?0)?{

cerr("[WARNING]check?is?frate<0?||?width<=0?||?height<=0?err!");

return?NULL;

}

//后面是?为?scr->pix?分配的内存?width*height

scr?=?malloc(sizeof(struct?screen)?+?sizeof(char)*width*height);

if?(NULL?==?scr)?{

cerr("[FATALG]Out?of?memory!");

return?NULL;

}

scr->frate?=?frate;

scr->width?=?width;

scr->height?=?height;

//减少malloc次数,malloc消耗很大,内存泄露呀,内存碎片呀

scr->pix?=?((char?*)scr)?+?sizeof(struct?screen);

return?scr;

}

void

screen_destory(struct?screen**?pscr)

{

if?(NULL?==?pscr?||?NULL?==?*pscr)

return;

free(*pscr);

//?避免野指针

*pscr?=?NULL;

}

//构建开头?的雪花,下面宏表示每?_INT_SHEAD?个步长,一个雪花,需要是2的幂

//static?可以理解为?private,?宏,位操作代码多了确实难读

#define?_INT_SHEAD?(1<<2)

static?void?__snow_head(char*?snow,?int?len)

{

int?r?=?0;

//数据需要清空

memset(snow,?0,?len);

for?(;;)?{

//取余一个技巧?2^3?-?1?=?7?=>?111?,?并就是取余数

int?t?=?rand()?&?(_INT_SHEAD?-?1);

if?(r?+?t?>=?len)

break;

snow[r?+?t]?=?1;

r?+=?_INT_SHEAD;

}

}

#undef?_INT_SHEAD

//通过?上一个?scr->pix[scr->width*(idx-1)]?=>?scr->pix[scr->width*idx]

//下面的宏?规定?雪花左右摇摆?0?向左一个像素,?1?表示?不变,?2表示向右一个像素

#define?_INT_SWING?(3)

static?void?__snow_next(struct?screen*?scr,?int?idx)

{

int?width?=?scr->width;

char*?psnow?=?scr->pix?+?width*(idx?-?1);

char*?snow?=?psnow?+?width;

int?i,?j,?t;?//?i索引,?j保存下一个瞬间雪花的位置,t?临时补得,解决雪花重叠问题

//为当前行重置

memset(snow,?0,?width);

//通过上一次雪花位置?计算下一次雪花位置

for?(i?=?0;?i<width;?++i)?{

for?(t?=?psnow[i];?t>0;?--t)?{?//?雪花可以重叠

//?rand()%_INT_SWING?-?1?表示?雪花?横轴的偏移量,相对上一次位置

j?=?i?+?rand()?%?_INT_SWING?-?1;

j?=?j<0width?-?1?:?j?>=?width0?:?j;?//?j如果越界了,左边越界让它到右边,右边越界到左边

++snow[j];

}

}

}

int

screen_draw_snow(struct?screen*?scr)

{

//?静态变量,默认初始化为0,每次都共用

static?int?__speed?=?0;

int?idx;

if?(++__speed?!=?_INT_VSNOW)

return?1;

//下面?就是?到了雪花飘落的时刻了?既?__speed?==?_INT_VSNOW

__speed?=?0;

//这里重新构建雪花界面,先构建头部,再从尾部开始构建

for?(idx?=?scr->height?-?1;?idx?>?0;?--idx)

__snow_next(scr,?idx);

//构建头部

__snow_head(scr->pix,?scr->width);

return?0;

}

//buf?保存scr?中pix?数据,构建后为?(width+1)*height,?后面宏是雪花图案

#define?_CHAR_SNOW?‘*‘

static?void?__flash_snow_buffer(struct?screen*?scr,?char*?buf)

{

int?i,?j,?rt;

int?height?=?scr->height,?width?=?scr->width;

int?frate?=?scr->frate;?//刷新的帧频率

//每次都等一下

for?(;;sleep_ms(frate))?{

//开始绘制屏幕

rt?=?screen_draw_snow(scr);

if?(rt)

continue;

for?(i?=?0;i<height;?++i)?{

char*?snow?=?scr->pix?+?i*width;

for?(j?=?0;?j<width;?++j)

buf[rt++]?=?snow[j]_CHAR_SNOW?:?‘?‘;

buf[rt++]?=?‘n‘;

}

buf[rt?-?1]?=?‘0‘;

//正式绘制到屏幕上

puts(buf);

//清空老屏幕,屏幕光标回到最上面

__curup(height);

}

}

#undef?_CHAR_SNOW

void

screen_flash_snow(struct?screen*?scr)

{

char*?buf?=?NULL;

//?初始化随机数种子,改变雪花轨迹

srand((unsigned)time(NULL));

buf?=?malloc(sizeof(char)*(scr->width?+?1)*scr->height);

if?(NULL?==?buf)?{

cerr("[FATAL]Out?of?memory!");

exit(EXIT_FAILURE);

}

__flash_snow_buffer(scr,?buf);

//1.这里理论上不会执行到这,没加控制器.?2.对于buf=NULL,这种代码?可以省掉,看编程习惯

free(buf);

buf?=?NULL;

}

 
友情链接
鄂ICP备19019357号-22