比较x^y和y^x + 找出小数点后某一位的出现位置

A - A
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 900B
use MathJax to parse formulas
Description
现有一式子 a / b. 你需要找出数字 c 在小数点后第一次出现的位置

Input
输入包含三个整数 a, b, c (1 ≤ a < b ≤ 105, 0 ≤ c ≤ 9).

Output
输出数字 c 第一次在小数点后出现的位置,如果 c 不在小数点后出现输出 -1

Sample Input
Input
1 2 0
Output
2
Input
2 3 7
Output
-1
Hint
第一组样例 : 1 / 2 = 0.5000(0) 出现在第二个位置

第二组样例 : 2 / 3 = 0.6666(6) 7 没有出现,输出 -1

思路:模拟一次除法的过程,一开始写的循环100次,WA,,改成了500


#include <iostream>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;

int main()
{
    int a,b,c,d,i;
    cin>>a>>b>>c;
    a=a-a/b*b;

    for(i=0;i<500;i++)
    {
        a=a*10;
        d=a/b;
        a=a-d*b;
        if(d==c)
        {
            cout<<i+1<<endl;
            break;
        }




    }
    if(i==500)
        cout<<"-1";


    return 0;
}

C - C
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 987B
use MathJax to parse formulas
Description
给你两个数x, y, 比较 x^y 和 y ^ x 的大小

Input
两个数 x, y, ( 1 <= x , y <= 109 )

Output
如果 x ^ y < y ^ x , 输出 “<”

如果 x ^ y > y ^ x , 输出 “>”

如果 x ^ y = y ^ x , 输出 “=”

Sample Input
Input
5 8
Output
>
Input
10 3
Output
<
Input
6 6

Output

Hint
第一个例子 5 ^ 8 = 390625, 8 ^ 5 = 32768. 输出 ‘>’.

第二个例子 10 ^ 3 = 1000 < 3 ^10 = 59049.

第三个例子 6 ^ 6 = 46656 = 6 ^ 6.

思路:用数学方法,比较a和b的大小等同于比较log(a)和log(b)的大小,ln a的b次方=b×ln a,,因此可以比较
代码:

#include <iostream>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;

int main()
{
    LL x,y;
    cin>>x>>y;
    if(x==y)
        cout<<"=";
    else
    {


    if((double)y*log(x)>(double)x*log(y))
        cout<<">";
    else if((double)y*log(x)<(double)x*log(y))
        cout<<"<";
    else
        cout<<"=";
    }



    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41700151/article/details/81065368