[学习笔记-C++篇]day2 plus(刷题篇)

刷题网站:阶段1第一关:基本数据类型


一般整理2+思路,一个常规思路,一个改进思路,若有其它思路也会同步整理。

为便于调试,一般会编写主函数。

1.反转3位数

// 常规思路
// 分别取三位,然后交换构造新三位数

#include <iostream>
#include <cmath>
 
using namespace std;
class Solution {
public:
    /**
     * @param number: A 3-digit number.
     * @return: Reversed number.
     */
    int reverseInteger(int number) {
        // write your code here
        
        int temp[3];
        temp[0]=floor(number/100);
        cout<<temp[0]<<endl;
        temp[1]=floor((number-temp[0]*100)/10);
        cout<<temp[1]<<endl;
        temp[2]=number-temp[0]*100-temp[1]*10;
        cout<<temp[2]<<endl;

        return temp[2]*100+temp[1]*10+temp[0];
    }


};

int main()
{
    int num=123;
    Solution test;
    int output;
    
    output=test.reverseInteger(num);
    cout << output << endl;
    return 0;
}
// 升级思路,大佬解法
// 改进基础解法表达
#include <iostream>
#include <cmath>
 
using namespace std;
class Solution {
public:
    /**
     * @param number: A 3-digit number.
     * @return: Reversed number.
     */
    int reverseInteger(int number) {
        // write your code here
        
        return (number%10)*100 + (number%100)/10*10 + number/100;
    }


};

int main()
{
    int num=123;
    Solution test;
    int output;
    
    output=test.reverseInteger(num);
    cout << output << endl;
    return 0;
}

2.A+B问题

//常规思路
//直接ACM模式
class Solution {
public:
    /**
     * @param a: An integer
     * @param b: An integer
     * @return: The sum of a and b 
     */
    int aplusb(int a, int b) {
        // write your code here
        return a+b;
    }
};
//升级思路
//异或,补充进1
//参考
// 主要利用异或运算来完成 
        // 异或运算有一个别名叫做:不进位加法
        // 那么a ^ b就是a和b相加之后,该进位的地方不进位的结果
        // 然后下面考虑哪些地方要进位,自然是a和b里都是1的地方
        // a & b就是a和b里都是1的那些位置,a & b << 1 就是进位
        // 之后的结果。所以:a + b = (a ^ b) + (a & b << 1)
        // 令a' = a ^ b, b' = (a & b) << 1
        // 可以知道,这个过程是在模拟加法的运算过程,进位不可能
        // 一直持续,所以b最终会变为0。因此重复做上述操作就可以
        // 求得a + b的值。
//这里对思路有点要求,如果直接用return (a^b) + (a&b<<1);是不行的
//只能说,带符号的话,二进制直接这样算是有问题的,只能用下面的这种
class Solution {
public:
    /*
     * @param : An integer
     * @param : An integer
     * @return: The sum of a and b
     */
    int aplusb(int a, int b) {
        // write your code here
        int sum = a;
        int carry = b;
        while(carry) {
            int tmp = sum;
            sum = tmp ^ carry;
            carry = (tmp & carry) << 1;//!!!!注意,&必须要带括号才能在<<前运算
        }
        return sum;
    }
};

3.巴什博奕

参见[学习笔记-C++篇]经典题解1——巴什博奕

//为了保证先手优势,石头数必须不是(最大最小和)的倍数。
class Solution {
public:
    /**
     * @param n: an integer
     * @return: whether you can win the game given the number of stones in the heap
     */
    bool canWinBash(int n) {
        // Write your code here
        if(n%4!=0)
        return true;
        else return false;
    }
};

4.计算圆周长和面积

掌握的不好vector定义,初始化vectorvector参数的输入要用push_back函数,返回直接用变量名称(指针)即可;

class Solution {
public:
    /**
     * @param r: a Integer represent radius
     * @return: the circle's circumference nums[0] and area nums[1]
     */
    vector<double> calculate(int r) {
        // write your code here
        #define PI 3.14

        vector<double> output;

        output.push_back(2*PI*r);
        output.push_back(PI*pow(r,2));

        return output;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值