一道算法题,跪求大神。在线等。

一个十进制数,按题目要求转换为特定形式。
输入291,二进制数为100100011
291=2(8)+2(5)+2(1)+2(0)
但是8也必须转换为特定的形式,除2(1)  2(2) 2(0)不用转换外。
最终输出为:2(2(2(1)+2(0)))+2(2(2)+2(0))+2(1)+2(0)

全部评论
自己目前可以做到一层变换。 public static void main(String args[]) { Scanner cin = new Scanner(System.in); int in = cin.nextInt(); //先转换为二进制数 char[] ch = Integer.toBinaryString(in).toCharArray(); String res = ""; //记录从左到右的幂次位数 int temp = ch.length - 1; for (int i = 0; i < ch.length; i++) { if (ch[i] == '1') { if(res == "") { res += "2(" + temp + ")"; } else { res += "+" + "2(" + temp + ")"; } } temp--; } System.out.println(res); cin.close(); }
点赞 回复
分享
发布于 2017-01-08 11:53
我的是java版本的,不过我认为还不是最优化的 public static void main(String [] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { int n = in.nextInt(); if(n == 0) { System.out.println(n); continue; } else if(n<0) { System.out.println("-("+DecimaltoBinary(-n)+")"); continue; } else System.out.println(DecimaltoBinary(n)); } } public static String DecimaltoBinary(int num) { StringBuilder temp = new StringBuilder(); String result = ""; while(num>0) { temp.append(num%2); num /= 2; } char [] a = temp.reverse().toString().toCharArray(); int len = a.length; int j = len-1; if(a[0] == '1') { result += "2("+j+")"; int k = j; if(k>2) { result = result.replaceFirst(k+"",DecimaltoBinary(k)); k--; } } j--; for(int i = 1;i<len;i++) { if(a[i] == '1') { result += "+2("+j+")"; int k = j; while(k>2) { result = result.replace(k+"",DecimaltoBinary(k)); k--; } } j--; } result = result.replace("2(1)", "2"); return result; }
点赞 回复
分享
发布于 2017-01-08 21:14
小红书
校招火热招聘中
官网直投
/* *一个十进制数,按题目要求转换为特定形式。 *输入291,二进制数为100100011 *291=2(8)+2(5)+2(1)+2(0) *但是8也必须转换为特定的形式,除2(1)  2(2) 2(0)不用转换外。 *最终输出为:2(2(2(1)+2(0)))+2(2(2)+2(0))+2(1)+2(0) */ #include<iostream> using namespace std; //转换函数  void dec_to_2(int num); //求2^n函数  int n_2(int n); int main() {  int dec_num = 0;  cout << "place input num: ";   cin >> dec_num;  cout << dec_num << " = ";     //函数调用      dec_to_2(dec_num);     cout << "\n";  return 0;  } //转换函数  void dec_to_2(int num)  {  int temp = num;//定义临时变量用于储存下一次递归传入参数  int count_num = 0; //2进制计算  while(num > 1) { num /= 2; ++count_num; } //打印  if(num != 0)//num为2的倍数时不打印2(0) { cout <<"2(" ; if(count_num != 0 && count_num !=1 && count_num !=2) { dec_to_2(count_num ); } else//防止递归后程序沿原路径返回 count_num 值  {  cout << count_num; } cout << ")"; } //递归调用  if(temp > 1 && (temp != n_2(count_num)))//num为2的倍数时末尾不打印'+' { cout << " + "; dec_to_2(temp%(n_2(count_num))); }  }    //求2^n函数   int n_2(int n)  {  int temp = 1;  while(n != 0)  { temp *= 2; --n;  }  return temp;  }   结果如下 place input num: 291 291 = 2(2(2(1) + 2(0))) + 2(2(2) + 2(0)) + 2(1) + 2(0) Press any key to continue . . .  初学者感觉有点瞎写的,优化什么的没考虑。。。。
点赞 回复
分享
发布于 2017-01-09 00:28
Scala版本的。函数式编程简洁的代码风格,喜欢可以尝试下
点赞 回复
分享
发布于 2017-01-14 07:56
public static String DecimaltoBinary(int num){ char[] ch = Integer.toBinaryString(num).toCharArray(); String res = "";                 if(ch.length == 1 && ch[0] == '0'){ return "0"; } //从左到右记录幂的位置 int temp = ch.length-1; String s = ""; for (int i = 0; i < ch.length; i++) { if (ch[i] == '1') { if(temp > 2) { s = DecimaltoBinary(temp); }else{ s = temp+""; } if(res == "") { res += "2(" + s + ")"; } else { res += "+" + "2(" + s + ")"; } } temp--; } return res; } 就一个递归调用,没有多判断负数,上面有大神已经判断了!不过没想过复杂度的问题,能力不够
点赞 回复
分享
发布于 2017-01-16 09:16
点赞 回复
分享
发布于 2017-01-08 11:55
这个用递归来做,如果还没到最后一层就继续调用。
点赞 回复
分享
发布于 2017-01-08 19:15
点赞 回复
分享
发布于 2017-01-09 00:29
object Main extends App{ val num = 291                               //转换为2进制 def toBin(number: Int, bin: List[Int]): List[Int] = number match { case 0 => bin case _ => toBin(number/2, number%2::bin) }                                 //记录二进制中1的位置 def Ones(bin: List[Int], index: Int, result: List[Int]): List[Int] = bin match { case Nil => result case x::y => if (x == 0) Ones(y,index-1,result) else Ones(y,index-1,result:+index) }                                                                                       //转换函数   def change(number: Int): String = number match { case x if(x<=2) => "2(" + x + ")+" case _ => "2(" + Ones(toBin(number,Nil),toBin(number,Nil).length- 1 ,Nil) .map { x=> change(x)}.reduceRight(_+_).dropRight(1) + ")+" }                                    println(change( num ).dropRight(1))                           }
点赞 回复
分享
发布于 2017-01-14 08:02

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务