题解

237 条题解

  • 0
    @ 2015-08-26 14:58:08

    弱爆了
    var
    f:array[1..1000]of longint;
    n,i:integer;
    begin
    readln(n);
    f[1]:=1;
    for i:=2 to n do
    begin
    if i mod 2=1 then f[i]:=f[i-1]
    else f[i]:=f[i-1]+f[i div 2];
    end;
    writeln(f[n]);
    end.

  • 0
    @ 2015-08-20 14:27:28

    11行,简单无压力,只是第三行有点长
    #include <stdio.h>

    const int d[1001]={1,1,2,2,4,4,6,6,10,10,14,14,20,20,26,26,36,36,46,46,60,60,74,74,94,94,114,114,140,140,166,166,202,202,238,238,284,284,330,330,390,390,450,450,524,524,598,598,692,692,786,786,900,900,1014,1014,1154,1154,1294,1294,1460,1460,1626,1626,1828,1828,2030,2030,2268,2268,2506,2506,2790,2790,3074,3074,3404,3404,3734,3734,4124,4124,4514,4514,4964,4964,5414,5414,5938,5938,6462,6462,7060,7060,7658,7658,8350,8350,9042,9042,9828,9828,10614,10614,11514,11514,12414,12414,13428,13428,14442,14442,15596,15596,16750,16750,18044,18044,19338,19338,20798,20798,22258,22258,23884,23884,25510,25510,27338,27338,29166,29166,31196,31196,33226,33226,35494,35494,37762,37762,40268,40268,42774,42774,45564,45564,48354,48354,51428,51428,54502,54502,57906,57906,61310,61310,65044,65044,68778,68778,72902,72902,77026,77026,81540,81540,86054,86054,91018,91018,95982,95982,101396,101396,106810,106810,112748,112748,118686,118686,125148,125148,131610,131610,138670,138670,145730,145730,153388,153388,161046,161046,169396,169396,177746,177746,186788,186788,195830,195830,205658,205658,215486,215486,226100,226100,236714,236714,248228,248228,259742,259742,272156,272156,284570,284570,297998,297998,311426,311426,325868,325868,340310,340310,355906,355906,371502,371502,388252,388252,405002,405002,423046,423046,441090,441090,460428,460428,479766,479766,500564,500564,521362,521362,543620,543620,565878,565878,589762,589762,613646,613646,639156,639156,664666,664666,692004,692004,719342,719342,748508,748508,777674,777674,808870,808870,840066,840066,873292,873292,906518,906518,942012,942012,977506,977506,1015268,1015268,1053030,1053030,1093298,1093298,1133566,1133566,1176340,1176340,1219114,1219114,1264678,1264678,1310242,1310242,1358596,1358596,1406950,1406950,1458378,1458378,1509806,1509806,1564308,1564308,1618810,1618810,1676716,1676716,1734622,1734622,1795932,1795932,1857242,1857242,1922286,1922286,1987330,1987330,2056108,2056108,2124886,2124886,2197788,2197788,2270690,2270690,2347716,2347716,2424742,2424742,2506282,2506282,2587822,2587822,2673876,2673876,2759930,2759930,2850948,2850948,2941966,2941966,3037948,3037948,3133930,3133930,3235326,3235326,3336722,3336722,3443532,3443532,3550342,3550342,3663090,3663090,3775838,3775838,3894524,3894524,4013210,4013210,4138358,4138358,4263506,4263506,4395116,4395116,4526726,4526726,4665396,4665396,4804066,4804066,4949796,4949796,5095526,5095526,5248914,5248914,5402302,5402302,5563348,5563348,5724394,5724394,5893790,5893790,6063186,6063186,6240932,6240932,6418678,6418678,6605466,6605466,6792254,6792254,6988084,6988084,7183914,7183914,7389572,7389572,7595230,7595230,7810716,7810716,8026202,8026202,8252302,8252302,8478402,8478402,8715116,8715116,8951830,8951830,9200058,9200058,9448286,9448286,9708028,9708028,9967770,9967770,10239926,10239926,10512082,10512082,10796652,10796652,11081222,11081222,11379220,11379220,11677218,11677218,11988644,11988644,12300070,12300070,12625938,12625938,12951806,12951806,13292116,13292116,13632426,13632426,13988332,13988332,14344238,14344238,14715740,14715740,15087242,15087242,15475494,15475494,15863746,15863746,16268748,16268748,16673750,16673750,17096796,17096796,17519842,17519842,17960932,17960932,18402022,18402022,18862450,18862450,19322878,19322878,19802644,19802644,20282410,20282410,20782974,20782974,21283538,21283538,21804900,21804900,22326262,22326262,22869882,22869882,23413502,23413502,23979380,23979380,24545258,24545258,25135020,25135020,25724782,25724782,26338428,26338428,26952074,26952074,27591230,27591230,28230386,28230386,28895052,28895052,29559718,29559718,30251722,30251722,30943726,30943726,31663068,31663068,32382410,32382410,33130918,33130918,33879426,33879426,34657100,34657100,35434774,35434774,36243644,36243644,37052514,37052514,37892580,37892580,38732646,38732646,39605938,39605938,40479230,40479230,41385748,41385748,42292266,42292266,43234278,43234278,44176290,44176290,45153796,45153796,46131302,46131302,47146570,47146570,48161838,48161838,49214868,49214868,50267898,50267898,51361196,51361196,52454494,52454494,53588060,53588060,54721626,54721626,55897966,55897966,57074306,57074306,58293420,58293420,59512534,59512534,60777212,60777212,62041890,62041890,63352132,63352132,64662374,64662374,66020970,66020970,67379566,67379566,68786516,68786516,70193466,70193466,71651844,71651844,73110222,73110222,74620028,74620028,76129834,76129834,77694142,77694142,79258450,79258450,80877260,80877260,82496070,82496070,84172786,84172786,85849502,85849502,87584124,87584124,89318746,89318746,91114678,91114678,92910610,92910610,94767852,94767852,96625094,96625094,98547380,98547380,100469666,100469666,102456996,102456996,104444326,104444326,106500434,106500434,108556542,108556542,110681428,110681428,112806314,112806314,115004102,115004102,117201890,117201890,119472580,119472580,121743270,121743270,124090986,124090986,126438702,126438702,128863444,128863444,131288186,131288186,133794468,133794468,136300750,136300750,138888572,138888572,141476394,141476394,144150270,144150270,146824146,146824146,149584076,149584076,152344006,152344006,155194954,155194954,158045902,158045902,160987868,160987868,163929834,163929834,166967782,166967782,170005730,170005730,173139660,173139660,176273590,176273590,179508916,179508916,182744242,182744242,186080964,186080964,189417686,189417686,192861218,192861218,196304750,196304750,199855092,199855092,203405434,203405434,207068524,207068524,210731614,210731614,214507452,214507452,218283290,218283290,222177814,222177814,226072338,226072338,230085548,230085548,234098758,234098758,238237116,238237116,242375474,242375474,246638980,246638980,250902486,250902486,255297602,255297602,259692718,259692718,264219444,264219444,268746170,268746170,273411566,273411566,278076962,278076962,282881028,282881028,287685094,287685094,292634890,292634890,297584686,297584686,302680212,302680212,307775738,307775738,313024652,313024652,318273566,318273566,323675868,323675868,329078170,329078170,334641518,334641518,340204866,340204866,345929260,345929260,351653654,351653654,357547444,357547444,363441234,363441234,369504420,369504420,375567606,375567606,381808538,381808538,388049470,388049470,394468148,394468148,400886826,400886826,407492292,407492292,414097758,414097758,420890012,420890012,427682266,427682266,434670350,434670350,441658434,441658434,448842348,448842348,456026262,456026262,463415834,463415834,470805406,470805406,478400636,478400636,485995866,485995866,493806582,493806582,501617298,501617298,509643500,509643500,517669702,517669702,525922004,525922004,534174306,534174306,542652708,542652708,551131110,551131110,559846226,559846226,568561342,568561342,577513172,577513172,586465002,586465002,595665060,595665060,604865118,604865118,614313404,614313404,623761690,623761690,633469718,633469718,643177746,643177746,653145516,653145516,663113286,663113286,673353212,673353212,683593138,683593138,694105220,694105220,704617302,704617302,715413954,715413954,726210606,726210606,737291828,737291828,748373050,748373050,759752270,759752270,771131490,771131490,782808708,782808708,794485926,794485926,806474570,806474570,818463214,818463214,830763284,830763284,843063354,843063354,855689292,855689292,868315230,868315230,881267036,881267036,894218842,894218842,907510958,907510958,920803074,920803074,934435500,934435500,948067926,948067926,962056258,962056258,976044590,976044590,990388828,990388828,1004733066,1004733066,1019448806,1019448806,1034164546,1034164546,1049251788,1049251788,1064339030,1064339030,1079814524,1079814524,1095290018,1095290018,1111153764,1111153764,1127017510,1127017510,1143286258,1143286258,1159555006,1159555006,1176228756,1176228756,1192902506,1192902506,1209999302,1209999302,1227096098,1227096098,1244615940,1244615940,1262135782,1262135782,1280096714,1280096714,1298057646,1298057646,1316459668,1316459668,1334861690,1334861690,1353724140,1353724140,1372586590,1372586590,1391909468,1391909468,1411232346,1411232346,1431034990,1431034990,1450837634,1450837634,1471120044,1471120044,1491402454,1491402454,1512185428,1512185428,1532968402,1532968402,1554251940,1554251940,1575535478,1575535478,1597340378,1597340378,1619145278,1619145278,1641471540,1641471540,1663797802,1663797802,1686667684,1686667684,1709537566,1709537566,1732951068,1732951068,1756364570,1756364570,1780343950,1780343950,1804323330,1804323330,1828868588,1828868588,1853413846,1853413846,1878548866,1878548866,1903683886,1903683886,1929408668,1929408668,1955133450,1955133450,1981471878};

    int main()
    {
    int n;
    scanf("%d",&n);
    printf("%d",d[n]);
    return 0;
    }
    11行,简单无压力

  • 0
    @ 2015-08-09 20:49:18

    #include<cstdio>
    using namespace std;
    const int MAXN = 1010;

    int f[MAXN];

    int main()
    {
    int n;
    scanf("%d", &n);
    f[1] = 1;
    for(int i=2; i<=n; i++){
    for(int j=1; j<=i/2; j++)

    f[i] += f[j];
    f[i] ++;
    }
    printf("%d", f[n]);
    return 0;
    }
    呵呵

  • 0
    @ 2015-08-05 09:02:21

    简单递推

  • 0
    @ 2015-07-18 10:33:34

    include<iostream> include<stdlib.h> include<windows.h> include<time.h> include<conio.h>
    using namespace std;
    HANDLE Mutex=CreateMutex(NULL,FALSE,NULL);//互斥对象
    int GameOver=0;
    int level=0;
    int map[23][23];
    //坦克种类,Normal为玩家坦克
    define Normal 0 define Red 1 define Blue 2 define Green 3
    //方向的宏定义
    define Up 0 define Down 1 define Left 2 define Right 3
    //地图标记的宏定义
    define Empty 0 define Player 1 define PlayerBullet 2 define EnemyBullet 3 define Enemy 4
    int Kill;
    int KillRed;
    int KillGreen;
    int EnemyExist;
    void SetPos(int i,int j)//设定光标位置
    {
    COORD pos={i,j};
    HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(Out, pos);
    }
    void HideCurSor(void)//隐藏光标
    {
    CONSOLE_CURSOR_INFO info={1,0};
    HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorInfo(Out,&info);
    }
    int sharp[4][12]=
    {
    {0,1,1,0,1,1,1,2,2,0,2,2},
    {0,0,0,2,1,0,1,1,1,2,2,1},
    {0,1,0,2,1,0,1,1,2,1,2,2},
    {0,0,0,1,1,1,1,2,2,0,2,1},
    };//此数组用来保存坦克各个方向的形状信息
    DWORD WINAPI Bulletfly(LPVOID lpParameter);//子弹函数申明
    void Updata();//更新界面信息函数申明
    class Tank//坦克类
    {
    private:
    int Direction;//方向
    int hotpoint[2];//活动点
    int Speed;//速度
    int FirePower;//火力
    public:
    Tank(int dir,int hot1,int hot2,int typ,int spe,int firepow)//构造函数
    {
    Direction=dir;
    hotpoint[0]=hot1;
    hotpoint[1]=hot2;
    Type=typ;
    Speed=spe;
    FirePower=firepow;
    }
    int Type;//坦克的种类(详见宏定义)
    int ID;//坦克在MAP中的标记(详见宏定义)
    int FireEnable;//是否可以开火
    int Life;//生命值
    void Running();//运行函数
    int Judge(int x,int y,int ID);//判断是否可以绘制坦克
    void DrawTank();//重绘坦克
    void Redraw();//擦除坦克
    int GetSpeed()//获取速度
    {
    return Speed;
    }
    int GetFire()//获取火力
    {
    return FirePower;
    }
    int GetDirection()//获取方向
    {
    return Direction;
    }
    int GetHotX()//获取活动点坐标
    {
    return hotpoint[0];
    }
    int GetHotY()
    {
    return hotpoint[1];
    }
    void IncreaseFire()//火力+
    {
    FirePower++;
    }
    void IncreaseSpeed()//速度+
    {
    Speed++;
    }
    void ChangeDirection(int newD)//改变方向
    {
    Direction=newD;
    }
    void ChangePos(int x,int y)//改变活动点
    {
    hotpoint[0]=x;
    hotpoint[1]=y;
    }
    };
    Tank player(Right,0,0,Normal,1,1);//玩家
    Tank enemy(Left,20,0,Red,1,1);//敌人
    void Tank::DrawTank()//绘制坦克
    {
    int i;
    int nx,ny;
    if(Type==Red)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
    else if(Type==Blue)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
    else if(Type==Green)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
    else if(Type==Normal)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    for(i=0;i<6;i++)
    {
    nx=hotpoint[0]+sharp[Direction][i2];
    ny=hotpoint[1]+sharp[Direction][i2+1];
    SetPos((ny+1)*2,nx+1);//利用sharp数组相对于点x,y绘制形状
    map[nx][ny]=ID;
    cout<<"■";
    }
    }
    void Tank::Redraw()//擦除坦克,原理同上
    {
    int i;
    int nx,ny;
    for(i=0;i<6;i++)
    {
    nx=hotpoint[0]+sharp[Direction][i2];
    ny=hotpoint[1]+sharp[Direction][i2+1];
    map[nx][ny]=Empty;
    SetPos((ny+1)*2,nx+1);
    cout<<" ";
    }
    }
    int Tank::Judge(int x,int y,int dir)//判断当前是否可以绘制坦克
    {
    int i;
    int nx,ny;
    for(i=0;i<6;i++) { nx=x+sharp[dir][i*2]; ny=y+sharp[dir][i*2+1]; if(nx<0||nx>=23||ny<0||ny>=23||map[nx][ny]!=Empty)//不能绘制,返回1
    return 1;
    }
    return 0;
    }
    void Tank::Running()//坦克运行函数
    {
    int newD;
    //坦克的运行
    while(1)
    {
    if(Life==0)
    {
    EnemyExist=0;//敌人不存在
    return;
    }
    if(GameOver==1)
    return;
    if(FireEnable==1&&GameOver==0)//如果可以开火
    {
    WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
    FireEnable=0;//设为不可开火
    HANDLE bullet=CreateThread(NULL,0,Bulletfly,&ID,0,NULL);//创建子弹线程
    CloseHandle(bullet);
    ReleaseMutex(Mutex);//释放互斥对象
    Sleep(100);
    }
    WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
    srand((int)time(0));
    newD=rand()%4;
    if(newD==Up)//随机出新的方向并重新绘制坦克
    {
    Redraw();
    if(Judge(hotpoint[0]-1,hotpoint[1],newD)==0)
    {
    hotpoint[0]--;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    else if(newD==Down)
    {
    Redraw();
    if(Judge(hotpoint[0]+1,hotpoint[1],newD)==0)
    {
    hotpoint[0]++;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    else if(newD==Left)
    {
    Redraw();
    if(Judge(hotpoint[0],hotpoint[1]-1,newD)==0)
    {
    hotpoint[1]--;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    else if(newD==Right)
    {
    Redraw();
    if(Judge(hotpoint[0],hotpoint[1]+1,newD)==0)
    {
    hotpoint[1]++;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    if(GameOver==0&&Life!=0)
    DrawTank();
    ReleaseMutex(Mutex);//释放互斥对象
    Sleep(500-80*Speed);
    }
    }
    /*子弹线程函数*/
    DWORD WINAPI Bulletfly(LPVOID lpParameter)
    {
    int ID=(int )lpParameter;//ID用来获取发射子弹坦克的ID
    int Pos[2];//子弹活动点
    int direction;
    int Speed;
    int type;
    int hit=0;//击中标记
    int oldx,oldy;//旧活动点
    int flag=0;//子弹是否有移动的标记
    if(ID==Player)//如果是玩家坦克
    {
    type=PlayerBullet;
    direction=player.GetDirection();
    Speed=player.GetFire();
    Pos[0]=player.GetHotX();
    Pos[1]=player.GetHotY();
    }
    else if(ID==Enemy)//如果是敌人坦克
    {
    type=EnemyBullet;
    direction=enemy.GetDirection();
    Speed=enemy.GetFire();
    Pos[0]=enemy.GetHotX();
    Pos[1]=enemy.GetHotY();
    }
    if(direction==Up)//根据坦克的位置和方向确定子弹的初始坐标
    {
    Pos[0]--;
    Pos[1]++;
    }
    else if(direction==Down)
    {
    Pos[0]+=3;
    Pos[1]++;
    }
    else if(direction==Left)
    {
    Pos[0]++;
    Pos[1]--;
    }
    else if(direction==Right)
    {
    Pos[0]++;
    Pos[1]+=3;
    }
    //子弹的运行
    while(1)
    {
    WaitForSingleObject(Mutex,INFINITE);//这个不再注释了。。。。。
    if(flag==1&&hit!=1)//擦除原位置
    {
    map[oldx][oldy]=Empty;
    SetPos((oldy+1)*2,oldx+1);
    cout<<" ";
    }
    if(GameOver==1)
    return 0;
    if(hit==1||Pos[0]<0||Pos[0]>22||Pos[1]<0||Pos[1]>22)//如果击中
    {
    ReleaseMutex(Mutex);
    Sleep(500);
    if(type==PlayerBullet)
    player.FireEnable=1;
    else if(type=EnemyBullet)
    enemy.FireEnable=1;
    break;
    }
    switch(map[Pos[0]][Pos[1]])//子弹经过的MAP的标记
    {
    case Empty://如果是空位置就绘制子弹
    map[Pos[0]][Pos[1]]=type;
    SetPos((Pos[1]+1)*2,Pos[0]+1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout<<"■";
    break;
    case Player://如果是玩家位置
    if(type!=PlayerBullet)
    {
    player.Life--;//生命减少
    if(player.Life<=0)
    GameOver=1;
    }
    Updata();
    hit=1;
    break;
    case Enemy://如果是敌人位置
    if(type!=PlayerBullet)
    hit=1;
    else
    {
    hit=1;
    Kill++;
    if(Kill%20==0&&player.Life<5)//击杀数++
    player.Life++;
    if(enemy.Type==Red)//如果击杀红坦克
    {
    KillRed++;
    if(KillRed%10==0&&player.GetFire()<5)
    player.IncreaseFire();
    }
    if(enemy.Type==Green)///如果击杀绿坦克
    {
    KillGreen++;
    if(KillGreen%10==0&&player.GetSpeed()<5)
    player.IncreaseSpeed();
    }
    enemy.Redraw();//擦除敌人
    enemy.Life=0;//敌人死亡
    }
    Updata();
    break;
    }
    oldx=Pos[0];
    oldy=Pos[1];
    if(direction==Up)//子弹移动
    Pos[0]--;
    else if(direction==Down)
    Pos[0]++;
    else if(direction==Left)
    Pos[1]--;
    else if(direction==Right)
    Pos[1]++;
    ReleaseMutex(Mutex);
    flag=1;
    Sleep(60-10*Speed);
    }
    return 0;
    }
    /*敌人线程函数*/
    DWORD WINAPI TankRuning(LPVOID lpParameter)
    {
    Sleep(400);
    int Pos;
    int Start[2];//敌人起始地址
    int typ;
    int fire;
    int spe;
    while(1)
    {
    if(GameOver==1)
    return 0;
    srand((int)time(0));//随机出敌人起始地址
    Pos=rand()%4;
    if(Pos==0)
    {
    Start[0]=2;
    Start[0]=2;
    }
    else if(Pos==1)
    {
    Start[0]=2;
    Start[1]=18;
    }
    else if(Pos==2)
    {
    Start[0]=18;
    Start[1]=2;
    }
    else if(Pos==3)
    {
    Start[0]=18;
    Start[1]=18;
    }
    if(player.Judge(Start[0],Start[1],Down)==0)
    break;
    }
    WaitForSingleObject(Mutex,INFINITE);
    srand((int)time(0));
    typ=rand()%3+1;//随机出敌人的种类
    if(typ==Blue)
    {
    spe=1+level;
    fire=1+level;
    }
    else if(typ==Red)
    {
    spe=1+level;
    fire=3+level;
    }
    else if(typ==Green)
    {
    spe=3+level;
    fire=1+level;
    }
    enemy=Tank(Down,Start[0],Start[1],typ,spe,fire);//重新生成敌人坦克
    enemy.ID=Enemy;
    enemy.Life=1;
    enemy.FireEnable=1;
    ReleaseMutex(Mutex);
    enemy.Running();
    return 0;
    }
    void Init()//初始化函数
    {
    Kill=0;
    KillRed=0;
    KillGreen=0;
    player=Tank(Left,0,0,Normal,1,1);
    enemy=Tank(Left,0,0,Red,1,1);
    player.Life=2;
    player.FireEnable=1;
    enemy.Life=0;
    enemy.FireEnable=1;
    player.ID=Player;
    enemy.ID=Enemy;
    EnemyExist=0;
    }
    void Updata()//更新界面信息
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    int i;
    SetPos(53,0);
    cout<<"生命值:";
    SetPos(53,1);
    for(i=0;i<5;i++)
    {
    if(i<player.Life)
    cout<<"■";
    else
    cout<<" ";
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
    SetPos(53,3);
    cout<<"移动速度:";
    SetPos(53,4);
    for(i=0;i<5;i++)
    {
    if(i<player.GetSpeed())
    cout<<"■";
    else
    cout<<" ";
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
    SetPos(53,5);
    cout<<"火力:";
    SetPos(53,6);
    for(i=0;i<5;i++)
    {
    if(i<player.GetFire())
    cout<<"■";
    else
    cout<<" ";
    }
    SetPos(53,8);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout<<"杀敌数:"<<Kill;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
    SetPos(53,9);
    cout<<"杀死红坦克:"<<KillRed;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
    SetPos(53,10);
    cout<<"杀死绿坦克:"<<KillGreen;
    }
    void DrawMap()//画界面
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    system("cls");
    int i;
    for(i=0;i<25;i++)
    {
    SetPos(i2,0);
    cout<<"■";
    }
    for(i=1;i<25;i++)
    {
    SetPos(0,i);
    cout<<"■";
    SetPos(242,i);
    cout<<"■";
    }
    for(i=0;i<25;i++)
    {
    SetPos(i*2,24);
    cout<<"■";
    }
    Updata();
    }
    void Welcome()//欢迎界面
    {
    int x;
    system("cls");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    SetPos(10,5);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    SetPos(10,6);
    cout<<"■ 坦克大战控制台版 ■";
    SetPos(10,7);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    SetPos(10,8);
    cout<<"■ 方向键移动,空格键射击 ■";
    SetPos(10,9);
    cout<<"■ 敌人分为3种,蓝色为普通敌人 ■";
    SetPos(10,10);
    cout<<"■ 红色敌人高射速,绿色敌人高机动性 ■";
    SetPos(10,11);
    cout<<"■ 每杀死10个红坦克,玩家射速提高(最高五级) ■";
    SetPos(10,12);
    cout<<"■ 每杀死10个绿坦克,玩家移动性提高(最高五级)■";
    SetPos(10,13);
    cout<<"■ 每杀死20个坦克,玩家生命+1(最高五格) ■";
    SetPos(10,14);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    SetPos(10,15);
    cout<<"■ 阎奕然作(百度ID:HapHapYear) ■";
    SetPos(10,16);
    cout<<"■ 按1-3选择难度 ■";
    SetPos(10,17);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    while(1)
    {
    x=getch();
    if(x<='3'&&x>='1')
    break;
    }
    level=x-'0'-1;
    }
    int main()
    {
    Init();
    HideCurSor();
    Welcome();
    DrawMap();
    HANDLE temp;
    int newD;
    player.DrawTank();
    while(GameOver==0)
    {
    if(GetAsyncKeyState(VK_UP))//按键上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Up;
    player.Redraw();
    if(player.Judge(player.GetHotX()-1,player.GetHotY(),newD)==0)//移动玩家坦克,原理和敌人函数一样
    {
    player.ChangePos(player.GetHotX()-1,player.GetHotY());
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(200-player.GetSpeed()20);//按键延迟,决定玩家坦克的速度
    }
    else if(GetAsyncKeyState(VK_DOWN))//按键下,同上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Down;
    player.Redraw();
    if(player.Judge(player.GetHotX()+1,player.GetHotY(),newD)==0)
    {
    player.ChangePos(player.GetHotX()+1,player.GetHotY());
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(200-player.GetSpeed()20);
    }
    else if(GetAsyncKeyState(VK_RIGHT))//按键右,同上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Right;
    player.Redraw();
    if(player.Judge(player.GetHotX(),player.GetHotY()+1,newD)==0)
    {
    player.ChangePos(player.GetHotX(),player.GetHotY()+1);
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(200-player.GetSpeed()20);
    }
    else if(GetAsyncKeyState(VK_LEFT))//按键左,同上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Left;
    player.Redraw();
    if(player.Judge(player.GetHotX(),player.GetHotY()-1,newD)==0)
    {
    player.ChangePos(player.GetHotX(),player.GetHotY()-1);
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(110-player.GetSpeed()10);
    }
    else if(GetAsyncKeyState(VK_SPACE))//按键空格,发射子弹
    {
    WaitForSingleObject(Mutex,INFINITE);
    if(player.FireEnable==1)//如果可以发射
    {
    HANDLE bullet=CreateThread(NULL,0,Bulletfly,&(player.ID),0,NULL);//创建玩家子弹进程
    CloseHandle(bullet);
    player.FireEnable=0;
    }
    ReleaseMutex(Mutex);
    }
    if(EnemyExist==0&&GameOver==0)//如果敌人不存在生成新敌人
    {
    WaitForSingleObject(Mutex,INFINITE);
    EnemyExist=1;
    temp=CreateThread(NULL,0,TankRuning,NULL,0,NULL);//创建敌人线程
    CloseHandle(temp);
    ReleaseMutex(Mutex);
    }
    }
    system("cls");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
    SetPos(20,10);
    cout<<"游戏结束"<<endl;
    SetPos(20,11);
    cout<<"杀敌数:"<<Kill;
    SetPos(20,12);
    cout<<"杀死红坦克"<<KillRed;
    SetPos(20,13);
    cout<<"杀死绿坦克"<<KillGreen<<endl;
    return 0;
    }

  • 0
    @ 2015-07-18 10:31:33

    #include<iostream>
    #include<stdlib.h>
    #include<windows.h>
    #include<time.h>
    #include<conio.h>
    using namespace std;

    HANDLE Mutex=CreateMutex(NULL,FALSE,NULL);//互斥对象

    int GameOver=0;
    int level=0;
    int map[23][23];
    //坦克种类,Normal为玩家坦克
    #define Normal 0
    #define Red 1
    #define Blue 2
    #define Green 3
    //方向的宏定义
    #define Up 0
    #define Down 1
    #define Left 2
    #define Right 3
    //地图标记的宏定义
    #define Empty 0
    #define Player 1
    #define PlayerBullet 2
    #define EnemyBullet 3
    #define Enemy 4

    int Kill;
    int KillRed;
    int KillGreen;
    int EnemyExist;

    void SetPos(int i,int j)//设定光标位置
    {
    COORD pos={i,j};
    HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(Out, pos);
    }

    void HideCurSor(void)//隐藏光标
    {
    CONSOLE_CURSOR_INFO info={1,0};
    HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorInfo(Out,&info);
    }

    int sharp[4][12]=
    {
    {0,1,1,0,1,1,1,2,2,0,2,2},
    {0,0,0,2,1,0,1,1,1,2,2,1},
    {0,1,0,2,1,0,1,1,2,1,2,2},
    {0,0,0,1,1,1,1,2,2,0,2,1},
    };//此数组用来保存坦克各个方向的形状信息

    DWORD WINAPI Bulletfly(LPVOID lpParameter);//子弹函数申明
    void Updata();//更新界面信息函数申明

    class Tank//坦克类
    {
    private:
    int Direction;//方向
    int hotpoint[2];//活动点
    int Speed;//速度
    int FirePower;//火力
    public:
    Tank(int dir,int hot1,int hot2,int typ,int spe,int firepow)//构造函数
    {
    Direction=dir;
    hotpoint[0]=hot1;
    hotpoint[1]=hot2;
    Type=typ;
    Speed=spe;
    FirePower=firepow;
    }
    int Type;//坦克的种类(详见宏定义)
    int ID;//坦克在MAP中的标记(详见宏定义)
    int FireEnable;//是否可以开火
    int Life;//生命值
    void Running();//运行函数
    int Judge(int x,int y,int ID);//判断是否可以绘制坦克
    void DrawTank();//重绘坦克
    void Redraw();//擦除坦克
    int GetSpeed()//获取速度
    {
    return Speed;
    }
    int GetFire()//获取火力
    {
    return FirePower;
    }
    int GetDirection()//获取方向
    {
    return Direction;
    }
    int GetHotX()//获取活动点坐标
    {
    return hotpoint[0];
    }
    int GetHotY()
    {
    return hotpoint[1];
    }
    void IncreaseFire()//火力+
    {
    FirePower++;
    }
    void IncreaseSpeed()//速度+
    {
    Speed++;
    }
    void ChangeDirection(int newD)//改变方向
    {
    Direction=newD;
    }
    void ChangePos(int x,int y)//改变活动点
    {
    hotpoint[0]=x;
    hotpoint[1]=y;
    }
    };

    Tank player(Right,0,0,Normal,1,1);//玩家
    Tank enemy(Left,20,0,Red,1,1);//敌人

    void Tank::DrawTank()//绘制坦克
    {
    int i;
    int nx,ny;
    if(Type==Red)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
    else if(Type==Blue)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
    else if(Type==Green)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
    else if(Type==Normal)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    for(i=0;i<6;i++)
    {
    nx=hotpoint[0]+sharp[Direction][i*2];
    ny=hotpoint[1]+sharp[Direction][i*2+1];
    SetPos((ny+1)*2,nx+1);//利用sharp数组相对于点x,y绘制形状
    map[nx][ny]=ID;
    cout<<"■";
    }
    }

    void Tank::Redraw()//擦除坦克,原理同上
    {
    int i;
    int nx,ny;
    for(i=0;i<6;i++)
    {
    nx=hotpoint[0]+sharp[Direction][i*2];
    ny=hotpoint[1]+sharp[Direction][i*2+1];
    map[nx][ny]=Empty;
    SetPos((ny+1)*2,nx+1);
    cout<<" ";
    }
    }

    int Tank::Judge(int x,int y,int dir)//判断当前是否可以绘制坦克
    {
    int i;
    int nx,ny;
    for(i=0;i<6;i++)
    {
    nx=x+sharp[dir][i*2];
    ny=y+sharp[dir][i*2+1];
    if(nx<0||nx>=23||ny<0||ny>=23||map[nx][ny]!=Empty)//不能绘制,返回1
    return 1;
    }
    return 0;
    }

    void Tank::Running()//坦克运行函数
    {
    int newD;
    //坦克的运行
    while(1)
    {
    if(Life==0)
    {
    EnemyExist=0;//敌人不存在
    return;
    }
    if(GameOver==1)
    return;
    if(FireEnable==1&&GameOver==0)//如果可以开火
    {
    WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
    FireEnable=0;//设为不可开火
    HANDLE bullet=CreateThread(NULL,0,Bulletfly,&ID,0,NULL);//创建子弹线程
    CloseHandle(bullet);
    ReleaseMutex(Mutex);//释放互斥对象
    Sleep(100);
    }
    WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
    srand((int)time(0));
    newD=rand()%4;

    if(newD==Up)//随机出新的方向并重新绘制坦克
    {
    Redraw();
    if(Judge(hotpoint[0]-1,hotpoint[1],newD)==0)
    {
    hotpoint[0]--;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    else if(newD==Down)
    {
    Redraw();
    if(Judge(hotpoint[0]+1,hotpoint[1],newD)==0)
    {
    hotpoint[0]++;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    else if(newD==Left)
    {
    Redraw();
    if(Judge(hotpoint[0],hotpoint[1]-1,newD)==0)
    {
    hotpoint[1]--;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    else if(newD==Right)
    {
    Redraw();
    if(Judge(hotpoint[0],hotpoint[1]+1,newD)==0)
    {
    hotpoint[1]++;
    Direction=newD;
    }
    else
    {
    if(Judge(hotpoint[0],hotpoint[1],newD)==0)
    Direction=newD;
    }
    }
    if(GameOver==0&&Life!=0)
    DrawTank();
    ReleaseMutex(Mutex);//释放互斥对象
    Sleep(500-80*Speed);
    }
    }

    /*********************子弹线程函数*******************/
    DWORD WINAPI Bulletfly(LPVOID lpParameter)
    {
    int *ID=(int *)lpParameter;//ID用来获取发射子弹坦克的ID
    int Pos[2];//子弹活动点
    int direction;
    int Speed;
    int type;
    int hit=0;//击中标记
    int oldx,oldy;//旧活动点
    int flag=0;//子弹是否有移动的标记
    if(*ID==Player)//如果是玩家坦克
    {
    type=PlayerBullet;
    direction=player.GetDirection();
    Speed=player.GetFire();
    Pos[0]=player.GetHotX();
    Pos[1]=player.GetHotY();
    }
    else if(*ID==Enemy)//如果是敌人坦克
    {
    type=EnemyBullet;
    direction=enemy.GetDirection();
    Speed=enemy.GetFire();
    Pos[0]=enemy.GetHotX();
    Pos[1]=enemy.GetHotY();
    }
    if(direction==Up)//根据坦克的位置和方向确定子弹的初始坐标
    {
    Pos[0]--;
    Pos[1]++;
    }
    else if(direction==Down)
    {
    Pos[0]+=3;
    Pos[1]++;
    }
    else if(direction==Left)
    {
    Pos[0]++;
    Pos[1]--;
    }
    else if(direction==Right)
    {
    Pos[0]++;
    Pos[1]+=3;
    }
    //子弹的运行
    while(1)
    {
    WaitForSingleObject(Mutex,INFINITE);//这个不再注释了。。。。。
    if(flag==1&&hit!=1)//擦除原位置
    {
    map[oldx][oldy]=Empty;
    SetPos((oldy+1)*2,oldx+1);
    cout<<" ";
    }
    if(GameOver==1)
    return 0;
    if(hit==1||Pos[0]<0||Pos[0]>22||Pos[1]<0||Pos[1]>22)//如果击中
    {
    ReleaseMutex(Mutex);
    Sleep(500);
    if(type==PlayerBullet)
    player.FireEnable=1;
    else if(type=EnemyBullet)
    enemy.FireEnable=1;
    break;
    }
    switch(map[Pos[0]][Pos[1]])//子弹经过的MAP的标记
    {

    case Empty://如果是空位置就绘制子弹
    map[Pos[0]][Pos[1]]=type;
    SetPos((Pos[1]+1)*2,Pos[0]+1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout<<"■";
    break;
    case Player://如果是玩家位置
    if(type!=PlayerBullet)
    {
    player.Life--;//生命减少
    if(player.Life<=0)
    GameOver=1;
    }
    Updata();
    hit=1;
    break;
    case Enemy://如果是敌人位置
    if(type!=PlayerBullet)
    hit=1;
    else
    {
    hit=1;
    Kill++;
    if(Kill%20==0&&player.Life<5)//击杀数++
    player.Life++;
    if(enemy.Type==Red)//如果击杀红坦克
    {
    KillRed++;
    if(KillRed%10==0&&player.GetFire()<5)
    player.IncreaseFire();
    }
    if(enemy.Type==Green)///如果击杀绿坦克
    {
    KillGreen++;
    if(KillGreen%10==0&&player.GetSpeed()<5)
    player.IncreaseSpeed();
    }
    enemy.Redraw();//擦除敌人
    enemy.Life=0;//敌人死亡
    }
    Updata();
    break;
    }
    oldx=Pos[0];
    oldy=Pos[1];
    if(direction==Up)//子弹移动
    Pos[0]--;
    else if(direction==Down)
    Pos[0]++;
    else if(direction==Left)
    Pos[1]--;
    else if(direction==Right)
    Pos[1]++;
    ReleaseMutex(Mutex);
    flag=1;
    Sleep(60-10*Speed);
    }
    return 0;
    }

    /*************************敌人线程函数***************************/
    DWORD WINAPI TankRuning(LPVOID lpParameter)
    {
    Sleep(400);
    int Pos;
    int Start[2];//敌人起始地址
    int typ;
    int fire;
    int spe;
    while(1)
    {
    if(GameOver==1)
    return 0;
    srand((int)time(0));//随机出敌人起始地址
    Pos=rand()%4;
    if(Pos==0)
    {
    Start[0]=2;
    Start[0]=2;
    }
    else if(Pos==1)
    {
    Start[0]=2;
    Start[1]=18;
    }
    else if(Pos==2)
    {
    Start[0]=18;
    Start[1]=2;
    }
    else if(Pos==3)
    {
    Start[0]=18;
    Start[1]=18;
    }
    if(player.Judge(Start[0],Start[1],Down)==0)
    break;
    }
    WaitForSingleObject(Mutex,INFINITE);
    srand((int)time(0));
    typ=rand()%3+1;//随机出敌人的种类
    if(typ==Blue)
    {
    spe=1+level;
    fire=1+level;
    }
    else if(typ==Red)
    {
    spe=1+level;
    fire=3+level;
    }
    else if(typ==Green)
    {
    spe=3+level;
    fire=1+level;
    }
    enemy=Tank(Down,Start[0],Start[1],typ,spe,fire);//重新生成敌人坦克
    enemy.ID=Enemy;
    enemy.Life=1;
    enemy.FireEnable=1;
    ReleaseMutex(Mutex);
    enemy.Running();
    return 0;
    }

    void Init()//初始化函数
    {
    Kill=0;
    KillRed=0;
    KillGreen=0;
    player=Tank(Left,0,0,Normal,1,1);
    enemy=Tank(Left,0,0,Red,1,1);
    player.Life=2;
    player.FireEnable=1;
    enemy.Life=0;
    enemy.FireEnable=1;
    player.ID=Player;
    enemy.ID=Enemy;
    EnemyExist=0;
    }

    void Updata()//更新界面信息
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    int i;
    SetPos(53,0);
    cout<<"生命值:";
    SetPos(53,1);
    for(i=0;i<5;i++)
    {
    if(i<player.Life)
    cout<<"■";
    else
    cout<<" ";
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
    SetPos(53,3);
    cout<<"移动速度:";
    SetPos(53,4);
    for(i=0;i<5;i++)
    {
    if(i<player.GetSpeed())
    cout<<"■";
    else
    cout<<" ";
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
    SetPos(53,5);
    cout<<"火力:";
    SetPos(53,6);
    for(i=0;i<5;i++)
    {
    if(i<player.GetFire())
    cout<<"■";
    else
    cout<<" ";
    }
    SetPos(53,8);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout<<"杀敌数:"<<Kill;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
    SetPos(53,9);
    cout<<"杀死红坦克:"<<KillRed;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
    SetPos(53,10);
    cout<<"杀死绿坦克:"<<KillGreen;

    }
    void DrawMap()//画界面
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    system("cls");
    int i;
    for(i=0;i<25;i++)
    {
    SetPos(i*2,0);
    cout<<"■";
    }
    for(i=1;i<25;i++)
    {
    SetPos(0,i);
    cout<<"■";
    SetPos(24*2,i);
    cout<<"■";
    }
    for(i=0;i<25;i++)
    {
    SetPos(i*2,24);
    cout<<"■";
    }

    Updata();

    }

    void Welcome()//欢迎界面
    {
    int x;
    system("cls");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    SetPos(10,5);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    SetPos(10,6);
    cout<<"■ 坦克大战控制台版 ■";
    SetPos(10,7);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    SetPos(10,8);
    cout<<"■ 方向键移动,空格键射击 ■";
    SetPos(10,9);
    cout<<"■ 敌人分为3种,蓝色为普通敌人 ■";
    SetPos(10,10);
    cout<<"■ 红色敌人高射速,绿色敌人高机动性 ■";
    SetPos(10,11);
    cout<<"■ 每杀死10个红坦克,玩家射速提高(最高五级) ■";
    SetPos(10,12);
    cout<<"■ 每杀死10个绿坦克,玩家移动性提高(最高五级)■";
    SetPos(10,13);
    cout<<"■ 每杀死20个坦克,玩家生命+1(最高五格) ■";
    SetPos(10,14);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";

    SetPos(10,15);
    cout<<"■ 何某作(百度ID:HapHapYear) ■";
    SetPos(10,16);
    cout<<"■ 按1-3选择难度 ■";
    SetPos(10,17);
    cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
    while(1)
    {
    x=getch();
    if(x<='3'&&x>='1')
    break;
    }
    level=x-'0'-1;
    }

    int main()
    {
    Init();
    HideCurSor();
    Welcome();
    DrawMap();
    HANDLE temp;
    int newD;
    player.DrawTank();
    while(GameOver==0)
    {
    if(GetAsyncKeyState(VK_UP))//按键上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Up;
    player.Redraw();
    if(player.Judge(player.GetHotX()-1,player.GetHotY(),newD)==0)//移动玩家坦克,原理和敌人函数一样
    {
    player.ChangePos(player.GetHotX()-1,player.GetHotY());
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(200-player.GetSpeed()*20);//按键延迟,决定玩家坦克的速度
    }
    else if(GetAsyncKeyState(VK_DOWN))//按键下,同上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Down;
    player.Redraw();
    if(player.Judge(player.GetHotX()+1,player.GetHotY(),newD)==0)
    {
    player.ChangePos(player.GetHotX()+1,player.GetHotY());
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(200-player.GetSpeed()*20);
    }
    else if(GetAsyncKeyState(VK_RIGHT))//按键右,同上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Right;
    player.Redraw();
    if(player.Judge(player.GetHotX(),player.GetHotY()+1,newD)==0)
    {
    player.ChangePos(player.GetHotX(),player.GetHotY()+1);
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(200-player.GetSpeed()*20);
    }
    else if(GetAsyncKeyState(VK_LEFT))//按键左,同上
    {
    WaitForSingleObject(Mutex,INFINITE);
    newD=Left;
    player.Redraw();
    if(player.Judge(player.GetHotX(),player.GetHotY()-1,newD)==0)
    {
    player.ChangePos(player.GetHotX(),player.GetHotY()-1);
    player.ChangeDirection(newD);
    }
    else
    {
    if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
    player.ChangeDirection(newD);
    }
    if(GameOver==0)
    player.DrawTank();
    ReleaseMutex(Mutex);
    Sleep(110-player.GetSpeed()*10);
    }
    else if(GetAsyncKeyState(VK_SPACE))//按键空格,发射子弹
    {
    WaitForSingleObject(Mutex,INFINITE);
    if(player.FireEnable==1)//如果可以发射
    {
    HANDLE bullet=CreateThread(NULL,0,Bulletfly,&(player.ID),0,NULL);//创建玩家子弹进程
    CloseHandle(bullet);
    player.FireEnable=0;
    }
    ReleaseMutex(Mutex);
    }
    if(EnemyExist==0&&GameOver==0)//如果敌人不存在生成新敌人
    {
    WaitForSingleObject(Mutex,INFINITE);
    EnemyExist=1;
    temp=CreateThread(NULL,0,TankRuning,NULL,0,NULL);//创建敌人线程
    CloseHandle(temp);
    ReleaseMutex(Mutex);
    }
    }
    system("cls");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
    SetPos(20,10);
    cout<<"游戏结束"<<endl;
    SetPos(20,11);
    cout<<"杀敌数:"<<Kill;
    SetPos(20,12);
    cout<<"杀死红坦克"<<KillRed;
    SetPos(20,13);
    cout<<"杀死绿坦克"<<KillGreen<<endl;
    return 0;
    }
    坦克英雄

  • 0
    @ 2015-07-18 10:25:45

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main()
    {
    int a[1000][2],i,n;
    scanf("%d",&n);
    a[0][0]=1;
    a[0][1]=1;
    for (i=1;i<n;i++)
    {
    a[i][1]=a[i%2 ? (i+1)/2-1 : i/2-1][0]+1;
    a[i][0]=a[i][1]+a[i-1][0];
    }
    printf("%d",a[n-1][1]);
    return 0;
    }
    王一文你瓜完了,你个死瓜娃子

  • 0
    @ 2015-06-04 13:01:58

    #include <stdio.h>
    int main()
    {
    int a[1000][2],i,n;
    scanf("%d",&n);
    a[0][0]=1;
    a[0][1]=1;
    for (i=1;i<n;i++)
    {
    a[i][1]=a[i%2 ? (i+1)/2-1 : i/2-1][0]+1;
    a[i][0]=a[i][1]+a[i-1][0];
    }
    printf("%d",a[n-1][1]);
    }
    O(n)直接秒杀

  • 0
    @ 2015-06-02 14:41:29

    非递归做法

    #include<stdio.h>
    int main( )
    {
    int a[1001]={0},i,n,j;

    a[1]=1;
    a[2]=2;
    a[3]=2;

    scanf("%d",&n);

    if(n==1 || n==2 || n==3) printf("%d",a[n]);

    else

    {

    for(i=4;i<=n;i++)
    {
    a[i]++;
    for(j=i/2;j>=1;j--)
    a[i]=a[i]+a[j];
    }

    printf("%d",a[n]);

    }

    return 0;
    }

  • 0
    @ 2015-02-11 19:27:56

    var ans,n:longint;
    procedure count(a:longint);
    var i:longint;
    begin
    inc(ans);
    for i:=1 to a div 2 do count(i);
    end;
    begin
    readln(n);
    count(n);
    writeln(ans);
    end.

  • 0
    @ 2015-02-11 09:39:00

    #include<stdio.h>
    int s=0;
    int sb(int a)
    {
    int i;
    s++;
    if(a>=2)
    for(i=1;i<=a/2;i++)
    sb(i);
    return s;
    }
    int main()
    {
    int a;
    scanf("%d",&a);
    printf("%d",sb(a));
    return 0;
    }
    呵呵AC就这么简单
    额好吧

  • 0
    @ 2015-02-01 08:39:12

    var n,i,j:longint;
    a:array[1..1000] of longint;
    begin
    read(n);
    for i:=1 to n do a[i]:=1;
    for i:=1 to n do
    for j:=1 to (i div 2) do
    inc(a[i],a[j]);
    writeln(a[n]);
    end.

  • 0
    @ 2014-08-16 20:49:41

    var
    n,i,j:integer;
    a:array [1..1000] of longint;
    begin
    read(n);
    for i:=1 to n do
    a[i]:=1;
    for i:=1 to n do
    for j:=1 to (i div 2) do
    inc(a[i],a[j]);
    write(a[n]);
    end.//By Fkc

  • 0
    @ 2014-08-05 08:15:36

    #include<iostream>
    using namespace std;
    int sum=1;
    void aa(int n)
    {
    int i;
    if(n!=1)
    for(i=1;i<=n;i++)
    {
    aa(i/2);
    sum++;
    }
    if(n==1)
    sum++;
    }
    main()
    {
    int n;
    cin>>n;
    aa(n/2);
    cout<<sum;
    }

  • 0
    @ 2014-06-13 16:02:56

    没发好,重发一下。
    #include<iostream>
    using namespace std;
    int js=1;
    void num(int n)
    {
    int i,j,k,l;
    if(n!=1)
    for(i=1;i<=n;i++)num(i/2),js++;
    if(n==1)js++;
    }
    int main()
    {
    int n;
    cin>>n;
    num(n/2);
    cout<<js<<endl;
    return 0;
    }

  • 0
    @ 2014-06-13 16:01:52

    我是用递归做的,100%AC。
    #include<iostream>
    using namespace std;
    int js=1;
    void num(int n)
    {
    int i,j,k,l;
    if(n!=1)
    for(i=1;i<=n;i++)num(i/2),js++;
    if(n==1)js++;
    }
    int main()
    {
    int n;
    cin>>n;
    num(n/2);
    cout<<js<<endl;
    return 0;
    }

  • 0
    @ 2014-06-02 15:20:12

    var n,i,j:integer;
    a:array [1..1000] of longint;
    begin
    read(n);
    for i:=1 to n do a[i]:=1;
    for i:=1 to n do
    for j:=1 to (i div 2) do inc(a[i],a[j]);
    write(a[n]);
    end.

  • 0
    @ 2013-11-09 12:11:24

    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 748 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 744 KiB, score = 10
    测试数据 #2: Accepted, time = 11 ms, mem = 748 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 748 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 748 KiB, score = 10
    Accepted, time = 11 ms, mem = 748 KiB, score = 50

    ==========我是分割线===========

    代码:

    var
    f:array[0..2000]of longint;
    n:longint;
    function get(x:longint):longint;
    var
    s,i:longint;
    begin
    if f[x]>0 then exit(f[x]);
    s:=1;
    for i:=1 to x div 2 do
    inc(s,get(i));
    f[x]:=s;
    exit(s);
    end;
    begin
    readln(n);
    write(get(n));
    end.

  • 0
    @ 2013-10-16 16:27:40

    var
    f:array[0..1000] of longint;
    n:longint;
    function try(k:longint):longint;
    var
    i,s:longint;
    begin
    if f[k]>0 then exit(f[k]);
    s:=1;
    for i:=1 to k div 2 do
    s:=s+try(i);
    f[k]:=s;
    try:=s;
    end;
    begin
    readln(n);
    writeln(try(n));
    end.
    记忆化

  • 0
    @ 2013-10-09 21:32:31

    这是一道简单的递推题。适合NOIP难度
    对于每一个数N,可以从1到N div 2去综合
    var i,j,k,l,n,m,ans:longint;
    a:array[0..1000] of longint;
    begin
    readln(n);
    a[1]:=1;
    for i:=2 to n do begin
    a[i]:=1;
    for j:=1 to i div 2 do a[i]:=a[i]+a[j];
    end;
    writeln(a[n]);
    end.

信息

ID
1130
难度
2
分类
递推 点击显示
标签
递交数
7079
已通过
4174
通过率
59%
被复制
30
上传者