• NYIST 46 最少乘法次数


    最少乘法次数

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述

    给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘。如24:2*2=22(第一次乘),22*22=24(第二次乘),所以最少共2次;

                   

     
    输入
    第一行m表示有m(1<=m<=100)组测试数据;
    每一组测试数据有一整数n(0<n<=10000);
    输出
    输出每组测试数据所需次数s;
    样例输入
    3
    2
    3
    4
    样例输出
    1
    2
    2
    上传者
    李剑锋
    解题:快速幂。。。弱菜。。。几个月前时的我居然不会。。。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 int ans;
    18 void Fast(int n){
    19     while(n){
    20         if(n&1) ans++;
    21         n >>= 1;
    22         if(n) ans += 1;
    23     }
    24 }
    25 int main() {
    26     int n,m;
    27     scanf("%d",&n);
    28     while(n--){
    29         scanf("%d",&m);
    30         ans = 0;
    31         Fast(m);
    32         printf("%d
    ",ans?ans-1:ans);
    33     }
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    win8应用的编码UI测试
    什么是Peer Review
    Android开发环境的搭建
    运用int.parse()判断闰年代码实现
    等价类划分方法的应用之EditBox(二)
    等价类划分方法的应用之EditBox
    集成测试
    数据可视化简介
    关于processing
    白盒测试VS黑盒测试
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4078833.html
Copyright © 2020-2023  润新知