Blue Bridge Cup Training Strange Donation Problem Solution

topic:

The last wish of real estate tycoon Mr. Q is: to give 1 million yuan to the residents of X community to draw a lottery. The trouble is, he has a strange request:

11,000,000 yuan must be divided into exactly a number of shares (no remainder). Each portion must be a certain power of 7.

For example: 1 yuan, 7 yuan, 49 yuan, 343 yuan, ...

2. The number of copies of the same amount cannot exceed 5.

3. In the case of meeting the above requirements, the more shares the better!

Can you help me figure out how many can be divided into at most?

Idea:
From another perspective, if 1234567890 yuan is distributed to residents, each share must be a certain power of 10 yuan, and the number of shares of the same amount does not exceed 9, then how many shares can be divided at most?
Then there is only one case, 9 copies for 10 yuan, 8 copies for 100 yuan, 7 copies for 1,000 yuan, 6 copies for 10,000 yuan... It can be seen that this is actually a hexadecimal problem, that is, the title requires a hexadecimal format. The number after the representation, and then add up each number to get the number of copies.

Code:

//奇怪的捐赠
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

//10进制转iadix进制 
int ten2radix(int a, int radix){
    int cur = 0;
    int res[100];
    while(a > 0){
        res[cur++] = a % radix;
        a /= radix;
    }
    for(int i = cur - 1; i >= 0; --i){
        cout<<res[i];
    }
    cout<<endl;
}

//将10进制转成7进制
int main(){
    int a = 1000000;
    char arr[100];

//  ten2radix(a,7);

    itoa(a, arr, 7);
    int len = strlen(arr);
    int sum = 0;
    for(int i = 0; i < len; ++i){
        sum += arr[i] - '0';
    }
    cout<<sum<<endl;    
    return 0;
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348890&siteId=291194637