solution.md 1.6 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 分数
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5 6
1/1 + 1/2 + 1/4 + 1/8 + 1/16 + … 每项是前一项的一半,如果一共有20项,求这个和是多少,结果用分数表示出来。  
类似:3/2  
当然,这只是加了前2项而已。分子分母要求互质。  

每日一练社区's avatar
每日一练社区 已提交
7
下面哪一项是<font color="red">错误</font>的?
每日一练社区's avatar
每日一练社区 已提交
8
## aop
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10
### before
F
fix bug  
feilong 已提交
11

每日一练社区's avatar
每日一练社区 已提交
12 13 14 15 16
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
17

每日一练社区's avatar
每日一练社区 已提交
18
```cpp
每日一练社区's avatar
每日一练社区 已提交
19

每日一练社区's avatar
每日一练社区 已提交
20 21 22
```

## 答案
F
fix bug  
feilong 已提交
23

每日一练社区's avatar
每日一练社区 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
```cpp
long pow_2(int b)
{
    long x = 2;
    long res = 1;
    while (b > 0)
    {
        if (b & 1)
            res *= x;
        b >>= 1;
        x = x * x;
    }
    return res;
}
每日一练社区's avatar
每日一练社区 已提交
38 39 40 41 42 43 44 45 46 47 48

int gcd(long a, long b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int main()
{
    cout << pow_2(20) << "/" << pow_2(19) << endl;
}
每日一练社区's avatar
每日一练社区 已提交
49 50 51
```
## 选项

F
fix bug  
feilong 已提交
52

每日一练社区's avatar
每日一练社区 已提交
53
### A
F
fix bug  
feilong 已提交
54

每日一练社区's avatar
每日一练社区 已提交
55 56 57 58 59 60 61 62 63
```cpp
long pow_2(int b)
{
    long x = 2;
    long res = 1;
    while (b > 0)
    {
        if (b & 1)
            res *= x;
每日一练社区's avatar
每日一练社区 已提交
64
        b >>= 1;
每日一练社区's avatar
每日一练社区 已提交
65 66 67 68
        x = x * x;
    }
    return res;
}
每日一练社区's avatar
每日一练社区 已提交
69 70 71 72 73 74 75 76 77 78 79

int gcd(long a, long b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int main()
{
    cout << pow_2(20) - 1 << "/" << pow_2(19) << endl;
}
每日一练社区's avatar
每日一练社区 已提交
80 81 82
```

### B
F
fix bug  
feilong 已提交
83

每日一练社区's avatar
每日一练社区 已提交
84
```cpp
每日一练社区's avatar
每日一练社区 已提交
85
int gcd(int a, int b)
每日一练社区's avatar
每日一练社区 已提交
86
{
每日一练社区's avatar
每日一练社区 已提交
87 88 89 90 91 92 93 94 95
    return a % b ? gcd(b, a % b) : b;
}

int main()
{
    int a = 1048575, b = 524288;
    int t = gcd(a, b);
    cout << a / t << "/" << b / t << endl;
    return 0;
每日一练社区's avatar
每日一练社区 已提交
96 97 98 99
}
```

### C
F
fix bug  
feilong 已提交
100

每日一练社区's avatar
每日一练社区 已提交
101
```cpp
每日一练社区's avatar
每日一练社区 已提交
102
int gcd(int a, int b)
每日一练社区's avatar
每日一练社区 已提交
103 104 105
{
    while (b > 0)
    {
每日一练社区's avatar
每日一练社区 已提交
106 107 108 109
        int c;
        c = a % b;
        a = b;
        b = c;
每日一练社区's avatar
每日一练社区 已提交
110
    }
每日一练社区's avatar
每日一练社区 已提交
111 112 113 114 115 116 117 118 119 120 121
    return a;
}
int main()
{
    int a = 1048575;
    int b = 524288;
    int c = gcd(a, b);

    cout << a / c << "/" << b / c << endl;

    return 0;
每日一练社区's avatar
每日一练社区 已提交
122 123
}
```