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

update exercises

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