NYOJ 46-最少乘法次数(数论)
题目地址: pid=46">NYOJ 46
思路:能够化成二进制来求解。结果是最高位的位数-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;
}
NYOJ 46-最少乘法次数(数论)的更多相关文章
- NYIST 46 最少乘法次数
最少乘法次数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘.如24:2*2 ...
- nyoj最少乘法次数——快速幂思想
最少乘法次数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘.如24:2*2 ...
- 【ACM】最少乘法次数 - 树
最少乘法次数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘.如24:2*2 ...
- nyoj 46-最少乘法次数 (递推)
46-最少乘法次数 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:5 submit:18 题目描述: 给你一个非零整数,让你求这个数的n次方,每次相 ...
- NYOJ--46--最少乘法次数
最少乘法次数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘.如24:2*2 ...
- NYOJ-----最少乘法次数
最少乘法次数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘.如24:2*2 ...
- nyoj--46--最少乘法次数(数学+技巧)
最少乘法次数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘.如24:2*2=2 ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- nyoj 1238 最少换乘(dijkstra)
描述 欧洲某城是一个著名的旅游胜地,每年都有成千上万的人前来观光旅行.Dr. Kong决定利用暑假好好游览一番.. 年轻人旅游不怕辛苦,不怕劳累,只要费用低就行.但Dr. Kong年过半百,他希望乘坐 ...
随机推荐
- LeeCode-Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- nc命令 (NetCat)
摘自http://www.68idc.cn/help/server/linux/2014040682705.html NetCat,在网络工具中有"瑞士军刀"美誉,其有Window ...
- Oracle游标动态赋值
1. oracle游标动态赋值的小例子 -- 实现1:动态给游标赋值 -- 实现2:游标用表的rowtype声明,但数据却只配置表一行的某些字段时,遍历游标时需fetch into到精确字段 CREA ...
- 在web应用程序中使用MemcachedClient
本文来自:http://www.cnblogs.com/yukaizhao/archive/2008/11/10/memcached_client_usage.html 一. 背景: 在大访问量的we ...
- ubuntu 步步为营之uclinux编译和移植(完整版)
本节主要包含(ubuntu10.04) 一,linux下的经常使用压缩解压缩命令 二,环境建立 三,内核编译 四,移植 一,linux下的经常使用压缩解压缩命令 在linux下常见的压缩文件格式有ta ...
- hdu 4750 Count The Pairs (2013南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- mysql跟踪提交的SQL语句
http://www.cnblogs.com/wuyifu/p/3328024.html
- [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)(转)
用AngularJS做项目,但凡用过什么service啊,factory啊,provider啊,开始的时候晕没晕?!晕没晕?!感觉干的事儿都差不多啊,到底用哪个啊?!别告诉我你们几个就是为了跟我炫耀兄 ...
- Email Cover Letter Format
http://jobsearch.about.com/od/sampleletters/ig/Sample-Letter-Formats/Email-Message-Format.htm Copy ...
- 在windows中使用VMWare安装Mac OS 10.7
请参考http://www.cnblogs.com/huwlnew/archive/2011/11/15/2250342.html http://unmi.cc/vmware9-install-mac ...