首页 > 试题广场 >

2的幂次方

[编程题]2的幂次方
  • 热度指数:7972 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。     Let's present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0).        Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.

输入描述:
    For each case, the input file contains a positive integer n (n<=20000).


输出描述:
    For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
示例1

输入

1315

输出

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
头像 健康快乐最重要
发表于 2020-02-13 11:52:07
之前学了汉诺塔,然后做了几道关于递归的题,一直感觉对递归一知半解,感觉递归的题不能100%做出来,但是做了这道题,感觉递归是有技巧的。从思路讲起:首先这是一个简单的求幂的方法。我们如何得到这个式子呢?7的二次方是 111,也就是说我们可以把二进制的形式转换成这个公式。7的这个例子太特殊了,我们想一个 展开全文
头像 大内高手
发表于 2020-03-12 20:25:34
此题的题意是给定一个数n,先分解成二进制和的形势2的i次方的和 。比如: ,而系数 ,直到2上面的系数i全为0或者1为止。2直接输出为2, 输出为2(0),所以输入为9,输出为2(2+2(0))+2(0),即 。 所以此题非常适合使用递归策略。 // runtime: 4ms // space: 4 展开全文
头像 牛客668768843号
发表于 2022-02-25 12:09:27
最大只有2的14次方,找到规律直接打表。 #include <string> #include <map> #include <vector> #include <stdio.h> #include <cmath> using namesp 展开全文
头像 向某人
发表于 2023-02-19 05:14:40
//n最多不超过2的15次方,所以可以枚举1到14的表达式,初步遍历一遍生成基础式子, //此基础式子形如:2(9)+2(7)+...,再遍历一遍此基础式子,将中间1到14的数字替换为对应表达式即可。 #include <iostream> #include <string> 展开全文
头像 糖分椰蓉糯米糍
发表于 2022-02-10 22:48:46
递归:以n = 137为例 将n转换成二进制表示10001001,每个非0位后面还有几位,其对应幂次就是几。 例如第一个1后面还有7位,则该项幂次为7,7输入下一轮递归。 以此类推…… 但要注意特殊情况:幂次遇到1时就直接转化为2。 #include<bits/stdc++.h> us 展开全文
头像 KimKitsuragi
发表于 2024-03-05 14:13:30
#include <iostream> using namespace std; // 2^15=32768, 2^14=16384 void getExp(int *exp){ exp[0]=1; for(int i=1;i<15;i++){ ex 展开全文
头像 v0rd
发表于 2022-01-25 15:36:47
KY103 这个题有点意思 大体思路:首先设置一个辅助数组,按下标保存2^0, 2^1, 2^2.... 2^14。 递归时从后往前依次将数字减去2的k次幂,如果能减去,则递归分解k 递归的终止条件为:当前要分解的数字为0或者1。如果当前数字为0,则直接输出即可,如果为1,则说明减去一个2^1 刚刚 展开全文
头像 Huster水仙
发表于 2023-01-27 00:25:51
递归分解二进制 ①n=0 ②n=1 ③n>=2 :需要递归分解 注意: 当n=1时,输出2(0) 而递归调用时:若分解出的指数为1,对应字符串是2,而不是2(2(0)) #include<iostream> #include<string> using name 展开全文
头像 星雨201903142113898
发表于 2023-03-14 21:08:13
#include <cctype> #include <cstdio> #include <iostream> using namespace std; void func1(int n) { if (n == 1) { cout & 展开全文
头像 小徐小徐啊啊
发表于 2024-03-15 14:20:46
#include <stdio.h> int b[16] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; void ff(int a) { while (a & 展开全文