• 51Nod--1010 只包含235的数


    51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
     
    K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。
    所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。
    例如:n = 13,S中 >= 13的最小的数是15,所以输出15。
     
    Input
    第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
    第2 - T + 1行:每行1个数N(1 <= N <= 10^18)
    Output
    共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。
    Input示例
    5
    1
    8
    13
    35
    77
    Output示例
    2
    8
    15
    36
    80

    题解:

    使用打表法, 打印出数组, 然后对丑数数组,进行二分查找。

    #include <iostream> 
    #include <cstdio> 
    #include <cstdlib>
    #include <cstring> 
    using namespace std; 
    const int maxn = 2000000; 
    
    typedef long long LL; 
    
    LL num[maxn]; 
    int cnt; 
    void init(){
    	int pt2=0, pt3=0, pt5=0; 
    	cnt=0;
    	num[cnt++] = 1;  
    	LL minval; 
    	while(cnt < maxn){
    		minval = min(2*num[pt2], min(3*num[pt3], 5*num[pt5])); 
    		if(minval > 2*1e18){ break; }
    		num[cnt++] = minval; 
    		if(2*num[pt2] == minval){ ++pt2; } 
    		if(3*num[pt3] == minval){ ++pt3; } 
    		if(5*num[pt5] == minval){ ++pt5; } 
    	}
    	num[0] = 0; 
    }
    
    LL Find(LL n){
    	int mid, l = 0, r = cnt-1; 
    	while(l <= r){
    		mid = l + (r - l)/2; 
    		if(num[mid] >= n && num[mid-1]<n){
    			return num[mid]; 
    		}else if(num[mid-1] >= n){
    			r = mid - 1; 
    		}else{
    			l = mid + 1; 
    		}
    	}
    	return -1; 
    }
    
    int main(){
    	//freopen("in.txt", "r", stdin); 
    
    	int test_num; 
    	LL n, ans; 
    	scanf("%d", &test_num); 
    	init(); 
    	while(test_num--){
    		scanf("%lld", &n); 
    		ans = Find(n); 
    		printf("%lld
    ",  ans);
    	}
    	return 0; 
    }
    

      

  • 相关阅读:
    Git学习的网址
    (转)读懂diff
    如何让Beamer的logo放在右上角
    测试面试的一些分享
    python学习-使用制表符或者换行符来添加空白
    python学习-python变量的命名和使用
    python学习-运行.py时,python做了啥
    2020年,很特殊的1年
    python md5验签
    postman使用当前时间戳
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/5999609.html
Copyright © 2020-2023  润新知