• XidianOJ 1101 学数学的xry111


    题目描述

    作为西电ACM的老大,xry111的数学非常好,有一天他遇到了一个数学问题,刚想坐下来好好做做,结果被自己一眼看出了答案,他很不爽,把这题目扔给我们看,结果我们都没有做出来,现在只能求助于你们了。
    现在有一个正整数k,3<=k<=15,在k的所有幂次以及他们任意组合(幂次不重复)的和所组成的集合中,按照升序排列第N项是多少?(N<=1000)
    k=3时,这个序列是:
    1,3,4,9,10,12,13,~~~
    3^0,3^1,3^0+3^1,3^2,3^0+3^2,3^1+3^2,3^0+3^1+3^2,…

     现在给你k,N,请你输出第N项是多少

    输入

    多组数据处理到文件结尾。
    每行两个数K,N

    输出

    每行一个数,表示第N项的值。

    --正文
    对每一个幂次看作一位,如果使用了就是1,不使用就是0,则可以看出来规律
      1 10 11 100 101 110 111 (省略高位的0)
    所以只需要把n转换为二进制,如果某一位为1,则结果就那个位所含有的幂次
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    
    int main(){
        int k,n;
        while (scanf("%d %d",&k,&n) != EOF){
            int b[20];
            int temp = n,total = 0;
            while (temp >= 1){
                total ++;
                b[total] = temp % 2;
                temp /= 2;
            }
            int i;
            int now = 1;
            long long base = 1,res = 0;
            while (now <= total){
                if (b[now] == 1) res += base;
                base *= k;
                now ++;
            }
            printf("%lld
    ",res);
        }
        return 0;
    } 
  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6135169.html
Copyright © 2020-2023  润新知