zoukankan      html  css  js  c++  java
  • Leading and Trailing (b*log10(a)的运用)

    Leading and Trailing

    You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.

    Input

    Input starts with an integer T (≤ 1000), denoting the number of test cases.

    Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).

    Output

    For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.

    Sample Input
    5

    123456 1

    123456 2

    2 31

    2 32

    29 8751919

    Sample Output

    Case 1: 123 456

    Case 2: 152 936

    Case 3: 214 648

    Case 4: 429 296

    Case 5: 665 669

    首先要用到一个函数fmod(a,b),在math.h里面。

    求a对b求余,居然是浮点数格式的(个人觉得在这一题里用处不大啊,

    无非是求出的浮点数%1,然后出来的结果就是不带整数的小数了,比如1.2356%1==0.2356)。

    思路:1.输入a,b两个数,输出 a^b 前三位和后三位,

    数很大,所以用log10间接求.设10^n = a^b ;故n=b*log10(a);

    2.得出n是个浮点数,比如第二个例子n就是10.183024.因为 10^x ,x为整数时不会出现其他数字,

    (10,100,1000,1000...就是个进位的,这题用不着)

    3.把n%1得出只有小数位的n,然后10^n求出来的就是想要的数字(用pow()),

    比如第二组数据结果是1.524138,前几位就是1,5,2,4,1,3,8...

    后几位?略~~

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #define ll long long
    int mi(ll a,ll b)
    {
    	if (b==0)
    		return a;
    	ll ans=1;
    	while(b)
    	{
    		if (b&1)
    		{
    			ans=ans*a%1000;
    		}
    		b>>=1;
    		a=a*a%1000;
    	}
    	return ans;
    }
    int main()
    {
    	int t,f=0;
    	scanf ("%d",&t);
    	while(t--)
    	{
    		f++;
    		ll a,b,c,s;
    		double d,e;
    		scanf ("%lld %lld",&a,&b);
    		d=b*1.0*log10(a);
    		e=pow(10,fmod(d,1)); //fmod作用:去掉整数,保留小数才有用
    		s=e*100;//算出是1点多,不过肯定是前几位,*100就成了
    		c=mi(a,b);
    		printf ("Case %d: %lld %03lld
    ",f,s,c);
    	}
    	return 0;
    }
  • 相关阅读:
    Could not load file or assembly 'System.Core, Version=2.0.5.0
    r 数据分组处理
    r函数知识总结
    R-Sys.time计算程序运行时间
    R语言包_dplyr_1
    R语言系列:生成数据
    R语言-分组统计
    导数——平均变化率与瞬时变化率
    梯度下降法
    梯度
  • 原文地址:https://www.cnblogs.com/shidianshixuan/p/12663697.html
Copyright © 2011-2022 走看看