5.4日 笔记

  • 推箱子
#include 
#include 
#include 
int main()
{
    char a[50][50] = {"   ###     ",
                      "   #*#     ",
                      "   # #     ",
                      "####o######",
                      "#*  oS o *#",
                      "#####o#####",
                      "    # #    ",
                      "    #*#    ",
                      "    ###    "
    };
    char b[50][50] = {"   ###     ",
                      "   #*#     ",
                      "   # #     ",
                      "#### ######",
                      "#*       *#",
                      "##### #####",
                      "    # #    ",
                      "    #*#    ",
                      "    ###    "
    };
    int i,x,y,p,q;
    char ch;
    x=4,y=5;
    for(i=0;i<=9;i++)
    {
        puts(a[i]);
    }
    while(a[4][1]!='@'||a[1][4]!='@'||a[4][9]!='@'||a[7][5]!='@')
    {
        ch=getch();
        if(ch=='s')
        {
            if(a[x+1][y]!='#')
            {
                if(a[x+1][y]==' ')
                {
                    a[x][y] = b[x][y];
                    x++;
                    a[x][y]='S';
                }
                else if (a[x + 1][y] == '*') {
                    a[x][y] = b[x][y];
/*
     BOOL;用布尔型二维数组来存储a初始时的星号所在的位置,
                    每当小人走一步都要判断一下小人以前所在的位置是不是星号,如果是星号就再改成星
                    如果不是就变成空格。
                    CHAR:用char型二维数组来存储初始时的a,
                   每当小人走一步都要判断一下小人以前所在的位置是不是星号,如果是星号就再改成星号
                    如果不是就变成空格。*/
                    x++;
                    a[x][y]='S';
                }
                else if (a[x + 1][y] == 'o') // 下一位是箱子
                {
                    if (a[x + 2][y] == ' ') // 下两位是空格,可以推
                    {
                        a[x][y] = b[x][y]; // s离开原来的位置
                        a[x + 1][y] = 'S';
                        a[x + 2][y] = 'o';
                        x++;
                    }
                    else if (a[x + 2][y] == '*') // 下两位是*
                    {
                        a[x][y] = b[x][y];
                        a[x + 1][y] = 'S';
                        a[x + 2][y] = '@';
                        x++;
                    }
                }
                else { // 下一位是@
                    if (a[x + 2][y] == ' ' ) {
                        a[x][y]=b[x][y];
                        a[x+1][y]='S';
                        a[x+2][y]='o';
                        x++;
                    }
                    else if (a[x + 2][y] == '*') {
                        a[x][y]=b[x][y];
                        a[x+1][y]='S';
                        a[x+2][y]='@';
                        x++;
                    }
                }
            }
        }
         if(ch=='w')
        {
            if(a[x-1][y]!='#')
            {
                if(a[x-1][y]==' ')
                {
                    a[x][y] = b[x][y];
                    x--;
                    a[x][y]='S';
                }
                else if (a[x - 1][y] == '*') {
                    a[x][y] = b[x][y];
                    x--;
                    a[x][y]='S';
                }
                else if (a[x - 1][y] == 'o') // 下一位是箱子
                {
                    if (a[x - 2][y] == ' ') // 下两位是空格,可以推
                    {
                        a[x][y] = b[x][y]; // s离开原来的位置
                        a[x - 1][y] = 'S';
                        a[x - 2][y] = 'o';
                        x--;
                    }
                    else if (a[x - 2][y] == '*') // 下两位是*
                    {
                        a[x][y] = b[x][y];
                        a[x - 1][y] = 'S';
                        a[x - 2][y] = '@';
                        x--;
                    }
                }
                else { // 下一位是@
                    if (a[x - 2][y] == ' ' ) {
                        a[x][y]=b[x][y];
                        a[x-1][y]='S';
                        a[x-2][y]='o';
                        x--;
                    }
                    else if (a[x - 2][y] == '*') {
                        a[x][y]=b[x][y];
                        a[x-1][y]='S';
                        a[x-2][y]='@';
                        x--;
                    }
                }
            }
        }
         if(ch=='d')
        {
            if(a[x][y+1]!='#')
            {
                if(a[x][y+1]==' ')
                {
                    a[x][y] = b[x][y];
                    y++;
                    a[x][y]='S';
                }
                else if (a[x][y + 1] == '*') {
                    a[x][y] = b[x][y];
                    y++;
                    a[x][y]='S';
                }
                else if (a[x][y + 1] == 'o') // 下一位是箱子
                {
                    if (a[x][y + 2] == ' ') // 下两位是空格,可以推
                    {
                        a[x][y] = b[x][y]; // s离开原来的位置
                        a[x][y + 1] = 'S';
                        a[x][y + 2] = 'o';
                        y++;
                    }
                    else if (a[x][y + 2] == '*') // 下两位是*
                    {
                        a[x][y] = b[x][y];
                        a[x][y + 1] = 'S';
                        a[x][y + 2] = '@';
                        y++;
                    }
                }
                else { // 下一位是@
                    if (a[x][y + 2] == ' ' ) {
                        a[x][y] = b[x][y];
                        a[x][y+1] = 'S';
                        a[x][y+2] = 'o';
                        y++;
                    }
                    else if (a[x][y + 2] == '*') {
                        a[x][y] = b[x][y];
                        a[x][y+1] = 'S';
                        a[x][y+2] = '@';
                        y++;
                    }
                }
            }
        }
         if(ch=='a')
        {
            if(a[x][y-1]!='#')
            {
                if(a[x][y-1]==' ')
                {
                    a[x][y] = b[x][y];
                    y--;
                    a[x][y]='S';
                }
                else if (a[x][y - 1] == '*') {
                    a[x][y] = b[x][y];
                    y--;
                    a[x][y]='S';
                }
                else if (a[x][y - 1] == 'o') // 下一位是箱子
                {
                    if (a[x][y - 2] == ' ') // 下两位是空格,可以推
                    {
                        a[x][y] = b[x][y]; // s离开原来的位置
                        a[x][y - 1] = 'S';
                        a[x][y - 2] = 'o';
                        y--;
                    }
                    else if (a[x][y - 2] == '*') // 下两位是*
                    {
                        a[x][y] = b[x][y];
                        a[x][y - 1] = 'S';
                        a[x][y - 2] = '@';
                        y--;
                    }
                }
                else { // 下一位是@
                    if (a[x][y - 2] == ' ' ) {
                        a[x][y]=b[x][y];
                        a[x][y-1]='S';
                        a[x][y-2]='o';
                        y--;
                    }
                    else if (a[x][y - 2] == '*') {
                        a[x][y]=b[x][y];
                        a[x][y-1]='S';
                        a[x][y-2]='@';
                        y--;
                    }
                }
            }
        }
        system("cls");
        for(i=0;i<=9;i++)
        {
            puts(a[i]);
        }
    }
    if(a[4][1]=='@'&&a[1][4]=='@'&&a[4][9]=='@'&&a[7][5]=='@')
    {
        system("cls");
        printf("YOU WIN!\n");
        Sleep(5000);
    }

    return 0;
}

 

  • 求绝对值函数

整型用abs();

浮点型用fabs();

  • HDU2005
#include 
int main()
{
    int ds[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
    // 如果是闰年,就用第1行的数据;如果是平年,就用第0行的数据。
    int y, m, d;
    while (~scanf("%d/%d/%d", &y, &m, &d)) {
        int ans = 0;
        int isleap;
        if ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)) {
            isleap = 1; //是闰年
        } else {
            isleap = 0; // 不是闰年
        }
        for (int i = 1; i < m; ++i) { // 求前m-1个月的天数
            ans += ds[isleap][i];
        }
        ans += d;
        printf("%d\n", ans);
    }
    return 0;
}

 

你可能感兴趣的:(C语言学习)