NYOJ 46-最少乘法次数(数论)
2016-02-26 14:21
295 查看
题目地址:NYOJ 46
思路:能够化成二进制来求解。结果是最高位的位数-1+最高位后面1的个数。比如:对于3。它的二进制代码为11,就是用这个最高位(2-1)加上后面的1的个数(1个)。
用最高位1的目的是他能代表了转化的次数,由于2+2=4,4+4=8 8+8=16........
思路:能够化成二进制来求解。结果是最高位的位数-1+最高位后面1的个数。比如:对于3。它的二进制代码为11,就是用这个最高位(2-1)加上后面的1的个数(1个)。
用最高位1的目的是他能代表了转化的次数,由于2+2=4,4+4=8 8+8=16........
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map> using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const double pi= acos(-1.0); const double esp=1e-6; const int maxn=21010; char str[110]; int main() { int T,n,i,j; int cnt; scanf("%d",&T); while(T--){ scanf("%d",&n); memset(str,0,sizeof(str)); i=0; while(n/2!=0){ str[i++]='0'+n%2; n=n/2; } str[i]='1'; cnt=0; for(j=0;j<i;j++){ if(str[j]=='1') cnt++; } printf("%d\n",strlen(str)-1+cnt); } return 0; }
相关文章推荐
- bzoj3503 和谐矩阵
- 用委托在listbox中异步显示信息,解决线程间操作无效,从不是创建控件的线程访问它
- Android SwipeRefreshLayout下拉刷新与上拉加载+滑动删除
- android开发小感
- 委托C#(delegate、Action、Func、predicate,Predicate)
- 关于vendor-id和product-id的查找
- UVA 10491(p326)----Cows and Cars
- 一步一步教你使用.net进行Socket通信
- 前端ui框架
- 4145: [AMPPZ2014]The Prices 状压DP
- Hibernate之使用sql语句查询多个表,返回多种字段,将返回结果映射到自定义的普通pojo类中
- Linux/Unix下采用dlopen、dlsym、dlclose加载动态链接库
- MongoDB java 3.2版本查询指定列和排序的补充
- 抽象类一个很好的例子
- UVA 10391(p135)----Compound Words
- UVA 10410(p180)----Tree Reconstruction
- 九、UiWatcher API 详细介绍
- 数据一致性-分区可用性-性能
- bzoj 3223: Tyvj 1729 文艺平衡树
- Uncaught ReferenceError: XXX is not defined