Tianchi Online Programming 2020 National Day Eight Days Le-7 Base

記事ディレクトリ

1.タイトル

https://tianchi.aliyun.com/oj/118289365933779217/122647324212270017

整数を指定すると、そのベース7の文字列表現を返します。

入力範囲は[-1e7、1e7]です。

示例
样例 1:
输入: num = 100
输出: 202

样例 2:
输入: num = -7
输出: -10

2.問題解決

  • 残りを取得するためにベースで除算し、すべての残りの順序を逆にします
class Solution {
    
    
public:
    /**
     * @param num: the given number
     * @return: The base 7 string representation
     */
    string convertToBase7(int num) {
    
    
        // Write your code here
        bool negative = num < 0;
        if(negative)
        	num = -num;
        string ans;
        int base = 7;
        do
        {
    
    
        	ans += (num%base)+'0';
        	num /= base;
        }while(num);
        reverse(ans.begin(), ans.end());
        if(negative)
        	ans = "-"+ans;
        return ans;
    }
};

私のCSDNブログアドレスhttps://michael.blog.csdn.net/

QRコードを長押しまたはスキャンして、私の公式アカウント(Michael Amin)をフォローし、一緒に来て、学び、一緒に進歩してください!
マイケルアミン

おすすめ

転載: blog.csdn.net/qq_21201267/article/details/108895186