【万人千题计划11.18】C语言解题报告

1,判断国际象棋棋盘中一个格子的颜色

1812. 判断国际象棋棋盘中一个格子的颜色 - 力扣(LeetCode) (leetcode-cn.com)

bool squareIsWhite(char * coordinates)
{
     int i=*(coordinates+1)-'0';
     switch(*(coordinates))
     {
         case'a':
         case'c':
         case'e':
         case'g':
           if(i%2==0)
           return true;
           else
           return false;
          case'b':
         case'd':
         case'f':
         case'h':
         if(i%2!=0)
         return true;
         else
         return false; 
     }
     return 0;
}

找到棋盘中的规律,用switch语句就可以解决。

2,速算机器人

LCP 17. 速算机器人 - 力扣(LeetCode) (leetcode-cn.com)

int calculate(char* s)
{
    int x=1,y=0;
    char* p=s;
    int i=0;
    while(*(p+i)!='\0')
    {
        switch(*p)
        {
            case'A':
            x=2*x+y;
            break;
            case'B':
            y=2*y+x;
            break;
        }
        i++;
    }
    return x+y;
}

依然是switch语句解决。

3执行操作之后的变量值

2011. 执行操作后的变量值 - 力扣(LeetCode) (leetcode-cn.com)

int finalValueAfterOperations(char ** operations, int operationsSize)
{
       int i=0;
       int x=0;
       while(i<operationsSize)
       {
          if(strcmp(*(operations+i),"X++")==0||strcmp(*(operations+i),"++X")==0)
            x+=1;
          if(strcmp(*(operations+i),"X--")==0||strcmp(*(operations+i),"--X")==0)
             x-=1;
          i++;
       }
       return x;
       
}

,用上strcmp判断字符串是否相同就可以解决啦。

今天的C语言就到这里。

posted @   C_Ryson  阅读(22)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示