《C语言入门100例》(第17例) ASCII码《解题报告》

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

一道规律题,可以发现横坐标与纵坐标加和为偶数则为白色,奇数则为黑色

//c
bool squareIsWhite(char * coordinates){
int x=coordinates[0]-'a';//ASCII码的关系,假设a为0
int y=coordinates[1]-'0';
if((x+y)%2==0)return true;
return false;
}

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

//c++
class Solution {
public:
    bool squareIsWhite(string coordinates) {
        int x = coordinates[0] - 'a';//偶数为黑,奇数为白
        int y = coordinates[1] - '0';
        return ((x + y) % 2 == 0);
    }
};

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

这道题就是遍历数组,符合哪个就执行哪个

//c
int calculate(char* s){
int len=strlen(s);
int x=1,y=0;
for(int i=0;i<len;i++)
{
    if(s[i]=='A')
    {
        x=2*x+y;
    }
    else if(s[i]=='B')
    {
        y=2*y+x;
    }
}
return x+y;
}
//c++
class Solution {
public:
    int calculate(string s) {
int x=1,y=0;
int len=s.size();
for(int i=0;i<len;i++)
{
if(s[i]=='A')
{
    x=2*x+y;
}
else if(s[i]=='B')
{
    y=2*y+x;
}
}
return x+y;
    }
};

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

这道题遍历数组就可以

class Solution {
public:
    int finalValueAfterOperations(vector<string>& operations) {
int x=0;
int len=operations.size();
for(int i=0;i<len;i++)
{
    if(operations[i]=="++X"||operations[i]=="X++")
    {
        x++;
    }
    else
    {
        x--;
    }
}
return x;
    }
};

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

forget hurt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值