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;
    }
  • 相关阅读:
    蝶恋花
    JVM解毒——JVM与Java体系结构
    超赞!IDEA 最新版本,支持免打扰和轻量模式!
    SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存
    神奇的 SQL 之 WHERE 条件的提取与应用
    终于放弃了单调的swagger-ui了,选择了这款神器—knife4j
    Git 高级用法,喜欢就拿去用
    既然有 HTTP 请求,为什么还要用 RPC 调用?
    SpringBoot和Spring到底有没有本质的不同?
    一条简单的更新语句,MySQL是如何加锁的?
  • 原文地址:https://www.cnblogs.com/shidianshixuan/p/12663697.html
Copyright © 2011-2022 走看看