提交 3029e901 编写于 作者: 每日一练社区's avatar 每日一练社区

update exercises

上级 41c78088
......@@ -11,4 +11,5 @@ leetcode_template.md
auto_gen_leetcode_tem.py
leetcode_helper.py
lanqiao_helper.py
lanqiao_template.md
\ No newline at end of file
lanqiao_template.md
./src/__pycache__/*.pyc
\ No newline at end of file
......@@ -2,8 +2,6 @@
#include <string.h>
int co[100100][2];
//n块巧克力,分成x*x型的。
//返回巧克力的块数
int coun(int n, int x)
{
int sum = 0;
......@@ -14,16 +12,15 @@ int coun(int n, int x)
return sum;
}
//二分求解
int binary(int n, int k)
{
int l = 0, r = 100000;
while (l < r)
{
int mid = (r - l) / 2 + l;
if (coun(n, mid) < k)
if (coun(n, mid) > k)
r = mid - 1;
else if (coun(n, mid) > k)
else if (coun(n, mid) < k)
l = mid + 1;
else
return mid;
......
......@@ -29,30 +29,107 @@
## aop
### before
```cpp
#include <stdio.h>
#include <string.h>
int co[100100][2];
int coun(int n, int x)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += (co[i][0] / x) * (co[i][1] / x);
}
return sum;
}
```
### after
```cpp
int main()
{
int n;
int k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d%d", &co[i][0], &co[i][1]);
printf("%d\n", binary(n, k));
}
```
## 答案
```cpp
int binary(int n, int k)
{
int l = 0, r = 100000;
while (l < r)
{
int mid = (r - l) / 2 + l;
if (coun(n, mid) < k)
r = mid - 1;
else if (coun(n, mid) > k)
l = mid + 1;
else
return mid;
}
return r;
}
```
## 选项
### A
```cpp
int binary(int n, int k)
{
int l = 0, r = 100000;
while (l < r)
{
int mid = (r - l) / 2 + l;
if (coun(n, mid) < k)
r = mid;
else if (coun(n, mid) > k)
l = mid;
else
return mid;
}
return r;
}
```
### B
```cpp
int binary(int n, int k)
{
int l = 0, r = 100000;
while (l < r)
{
int mid = (r - l) / 2 + l;
if (coun(n, mid) > k)
r = mid;
else if (coun(n, mid) < k)
l = mid;
else
return mid;
}
return r;
}
```
### C
```cpp
int binary(int n, int k)
{
int l = 0, r = 100000;
while (l < r)
{
int mid = (r - l) / 2 + l;
if (coun(n, mid) > k)
r = mid - 1;
else if (coun(n, mid) < k)
l = mid + 1;
else
return mid;
}
return r;
}
```
#include <bits/stdc++.h>
using namespace std;
long pow_2(int b) //快速幂运算
long pow_2(int b)
{
long x = 2;
long res = 1;
while (b > 0)
{
if (b & 1)
res *= x;
b >>= 1; //右移一位
res = x;
b >>= 1;
x = x * x;
}
return res;
}
int gcd(long a, long b) //求出最大公约数
int gcd(long a, long b)
{
if (b == 0)
return a;
......@@ -21,6 +21,6 @@ int gcd(long a, long b) //求出最大公约数
}
int main()
{
cout << gcd(pow_2(20) - 1, pow_2(19)) << endl; //最大公约数
cout << pow_2(20) - 1 << "/" << pow_2(19) << endl; //输出分子分母
cout << gcd(pow_2(20) - 1, pow_2(19)) << endl;
cout << pow_2(20) - 1 << "/" << pow_2(19) << endl;
}
......@@ -6,30 +6,89 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
cout << gcd(pow_2(20) - 1, pow_2(19)) << endl;
cout << pow_2(20) - 1 << "/" << pow_2(19) << endl;
}
```
## 答案
```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;
}
```
## 选项
### A
```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;
}
```
### B
```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;
}
```
### C
```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;
}
```
......@@ -2,40 +2,40 @@
using namespace std;
int main()
{
int n; //小朋友数量
int n;
cin >> n;
int *Candy = new int[n]; //存储每个小朋友的糖果数
int *Candy = new int[n];
for (int i = 0; i < n; i++)
{
cin >> Candy[i];
}
int total = 0; //补发的糖果总数
int total = 0;
while (1)
{
bool flag = true; //判断每人糖果数量是否相同
bool flag = true;
for (int i = 1; i < n; i++)
{
if (Candy[0] != Candy[i])
{ //不相同
{
flag = false;
break;
}
}
if (flag == true) //数量相同,退出循环
if (flag == true)
break;
int temp = Candy[n - 1]; //记录最后一个小朋友手中的糖果
int temp = Candy[n - 1];
for (int i = n - 1; i >= 0; i--)
{
if (i == 0)
{ //如果是第一个小朋友
Candy[0] = Candy[0] / 2 + temp / 2; //最后一个小朋友获得最初第一个人的一半
{
Candy[0] = Candy[0] / 2 + temp / 2;
}
else
Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2; // 每个小朋友都把自己的糖果分一半给左手边的孩子
if (Candy[i] % 2 != 0)
{ //如果此时手中为奇数,则补糖果
Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2;
if (Candy[i] % 2 == 0)
{
Candy[i] += 1;
total += 1; //补发数量加1
total += 1;
}
}
}
......
......@@ -26,7 +26,8 @@
## aop
### before
```cpp
#include <iostream>
using namespace std;
```
### after
```cpp
......@@ -35,21 +36,182 @@
## 答案
```cpp
int main()
{
int n;
cin >> n;
int *Candy = new int[n];
for (int i = 0; i < n; i++)
{
cin >> Candy[i];
}
int total = 0;
while (1)
{
bool flag = true;
for (int i = 1; i < n; i++)
{
if (Candy[0] != Candy[i])
{
flag = false;
break;
}
}
if (flag == true)
break;
int temp = Candy[n - 1];
for (int i = n - 1; i >= 0; i--)
{
if (i == 0)
{
Candy[0] = Candy[0] / 2 + temp / 2;
}
else
Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2;
if (Candy[i] % 2 != 0)
{
Candy[i] += 1;
total += 1;
}
}
}
cout << total << endl;
}
```
## 选项
### A
```cpp
int main()
{
int n;
cin >> n;
int *Candy = new int[n];
for (int i = 0; i < n; i++)
{
cin >> Candy[i];
}
int total = 0;
while (1)
{
bool flag = true;
for (int i = 1; i < n; i++)
{
if (Candy[0] != Candy[i])
{
flag = false;
break;
}
}
if (flag == true)
break;
int temp = Candy[n - 1];
for (int i = n; i >= 0; i--)
{
if (i == 0)
{
Candy[0] = Candy[0] / 2 + temp / 2;
}
else
Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2;
if (Candy[i] % 2 != 0)
{
Candy[i] += 1;
total += 1;
}
}
}
cout << total << endl;
}
```
### B
```cpp
int main()
{
int n;
cin >> n;
int *Candy = new int[n];
for (int i = 0; i < n; i++)
{
cin >> Candy[i];
}
int total = 0;
while (1)
{
bool flag = true;
for (int i = 1; i < n; i++)
{
if (Candy[0] != Candy[i])
{
flag = false;
break;
}
}
if (flag == true)
break;
int temp = Candy[n - 1];
for (int i = n; i >= 0; i--)
{
if (i == 0)
{
Candy[0] = Candy[0] / 2 + temp / 2;
}
else
Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2;
if (Candy[i] % 2 == 0)
{
Candy[i] += 1;
total += 1;
}
}
}
cout << total << endl;
}
```
### C
```cpp
int main()
{
int n;
cin >> n;
int *Candy = new int[n];
for (int i = 0; i < n; i++)
{
cin >> Candy[i];
}
int total = 0;
while (1)
{
bool flag = true;
for (int i = 1; i < n; i++)
{
if (Candy[0] != Candy[i])
{
flag = false;
break;
}
}
if (flag == true)
break;
int temp = Candy[n - 1];
for (int i = n - 1; i >= 0; i--)
{
if (i == 0)
{
Candy[0] = Candy[0] / 2 + temp / 2;
}
else
Candy[i] = Candy[i] / 2 + Candy[i - 1] / 2;
if (Candy[i] % 2 == 0)
{
Candy[i] += 1;
total += 1;
}
}
}
cout << total << endl;
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册