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

update exercises

上级 cba017e9
...@@ -44,9 +44,9 @@ int main() ...@@ -44,9 +44,9 @@ int main()
int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap(y)) //判断闰年 if (is_leap(y))
{ {
for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 for (int i = 0; i < (m - 1); i++)
{ {
ans += L_m_d[i]; ans += L_m_d[i];
} }
...@@ -76,9 +76,9 @@ int main() ...@@ -76,9 +76,9 @@ int main()
int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap(y)) //判断闰年 if (is_leap(y))
{ {
for (int i = 0; i < m; i++) //记录1-(m-1)月天数 for (int i = 0; i < m; i++)
{ {
ans += L_m_d[i]; ans += L_m_d[i];
} }
...@@ -106,9 +106,9 @@ int main() ...@@ -106,9 +106,9 @@ int main()
int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap(y)) //判断闰年 if (is_leap(y))
{ {
for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 for (int i = 0; i < (m - 1); i++)
{ {
ans += L_m_d[i]; ans += L_m_d[i];
} }
...@@ -136,9 +136,9 @@ int main() ...@@ -136,9 +136,9 @@ int main()
int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap(y)) //判断闰年 if (is_leap(y))
{ {
for (int i = 0; i < m; i++) //记录1-(m-1)月天数 for (int i = 0; i < m; i++)
{ {
ans += L_m_d[i]; ans += L_m_d[i];
} }
......
#include <iostream> #include <iostream>
#include <sstream>
#include <vector>
using namespace std; using namespace std;
void i2s(int num, string &str)
{ //将数字转换为字符串
stringstream ss; //用法类似于cout
ss << num; //>>是将那个num传入到stringstream中
ss >> str; //用stringstream 类的函数str() 将对象所包含的内容赋给一个string对象。
}
void s2i(string &str, int &num)
{
stringstream ss; //将字符串反转为数字
ss << str;
ss >> num;
}
char to(char x)
{ //字符的翻转
if (x == '6')
return '9';
else if (x == '9')
return '6';
else
return x; //x:指0,1,2,5,8,这几个数字反转后还是本身
}
string reserve(const string &str)
{ //reserve作用类似旋转180°
string ans; //结果
for (int i = 3; i >= 0; i--)
{ //扫描,从最后一位开始转换之后
ans.insert(ans.end(), to(str[i])); //把最后一个字符插入
}
return ans;
}
struct price
{ //定义一个价格的结构体
int a, b, c; //a:原始价格 ,b:翻转颠倒价格,c:原始价格与颠倒价格的差
};
vector<price> v1; //存储-200多的
vector<price> v2; //存储+800多的
int main() int main()
{ {
cout << reserve << endl; //在这个地方可以测试原题中给出的数据 int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字
//枚举所有可以颠倒的四位数 int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字
// 将其颠倒过来,与原来的数值做差,将-200多与800多记录下来,分别记录在两个集合中 int profit1[1111][2]; //利润1
//遍历两个集合,-200多和800多两两求和,结果为558的即为正确答案 int profit2[1111][2]; //利润2
for (int i = 1000; i < 10000; i++) int before_reverse; //颠倒前
int after_reverse; //颠倒后
int i = 0;
int j = 0;
for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0
{ {
string str; for (int b = 0; b < 7; b++)
i2s(i, str);
if (str.find('3') != string::npos || str.find('4') != string::npos || str.find('7') != string::npos || str.rfind('0') == 3)
//str里面是i的字符串形式,里面的3,4,7,还有最后一位也就是数组第4位不能为0
continue;
string r = reserve(str); //翻转的字符
int r_int;
s2i(r, r_int); //r_int就是翻转后的价格,i是原始价格
int plus = r_int - i; //plus就是价格差
if (plus > -300 && plus < -200)
{ //价格范围
price p = {i, r_int, plus};
v1.push_back(p);
}
else if (plus > 800 && plus < 900)
{ //价格范围
price p = {i, r_int, plus};
v2.push_back(p);
}
//此时v1存储-200多的价格,v2存储+800多
for (int i = 0; i < v1.size(); i++)
{ {
for (int j = 0; j < v2.size(); j++) for (int c = 0; c < 7; c++)
{ {
if (v1[i].c + v2[j].c == 558) for (int d = 0; d < 7; d++)
{ {
printf("%d %d %d %d %d %d\n", v1[i].a, v1[i].b, v1[i].c, v2[j].a, v2[j].b, v2[j].c); before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d];
after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a];
//因为是颠倒之后所以第一位可以是0
if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200)
//在200-300范围内,我们以正数为准
{
profit1[i][0] = before_reverse;
profit1[i][1] = after_reverse - before_reverse;
i++;
}
else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900)
//在800-900范围内
{
profit1[j][0] = before_reverse;
profit2[j][1] = after_reverse - before_reverse;
j++;
}
} }
} }
} }
} }
int answer = 0;
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
if (profit1[a][1] + profit2[b][1] == 558)
{
answer = profit1[a][0];
}
}
}
cout << answer << endl;
return 0; return 0;
} }
\ No newline at end of file
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
## aop ## aop
### before ### before
```cpp ```cpp
#include <iostream>
using namespace std;
``` ```
### after ### after
```cpp ```cpp
...@@ -23,21 +24,229 @@ ...@@ -23,21 +24,229 @@
## 答案 ## 答案
```cpp ```cpp
int main()
{
int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字
int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字
int profit1[1111][2]; //利润1
int profit2[1111][2]; //利润2
int before_reverse; //颠倒前
int after_reverse; //颠倒后
int i = 0;
int j = 0;
for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0
{
for (int b = 0; b < 7; b++)
{
for (int c = 0; c < 7; c++)
{
for (int d = 0; d < 7; d++)
{
before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d];
after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a];
//因为是颠倒之后所以第一位可以是0
if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200)
//在200-300范围内,我们以正数为准
{
profit1[i][0] = before_reverse;
profit1[i][1] = after_reverse - before_reverse;
i++;
}
else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900)
//在800-900范围内
{
profit1[j][0] = before_reverse;
profit2[j][1] = after_reverse - before_reverse;
j++;
}
}
}
}
}
int answer = 0;
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
if (profit1[a][1] + profit2[b][1] == 558)
{
answer = profit1[a][0];
}
}
}
cout << answer << endl;
return 0;
}
``` ```
## 选项 ## 选项
### A ### A
```cpp ```cpp
int main()
{
int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字
int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字
int profit1[1111][2]; //利润1
int profit2[1111][2]; //利润2
int before_reverse; //颠倒前
int after_reverse; //颠倒后
int i = 0;
int j = 0;
for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0
{
for (int b = 0; b < 7; b++)
{
for (int c = 0; c < 7; c++)
{
for (int d = 0; d < 7; d++)
{
before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d];
after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a];
//因为是颠倒之后所以第一位可以是0
if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200)
//在200-300范围内,我们以正数为准
{
profit1[i][0] = before_reverse;
profit1[i][1] = after_reverse - before_reverse;
i++;
}
else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900)
//在800-900范围内
{
profit1[j][0] = before_reverse;
profit2[j][1] = after_reverse - before_reverse;
j++;
}
}
}
}
}
int answer = 0;
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
if (profit1[a][1] + profit2[b][1] == 558)
{
answer = profit1[b][1];
}
}
}
cout << answer << endl;
return 0;
}
``` ```
### B ### B
```cpp ```cpp
int main()
{
int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字
int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字
int profit1[1111][2]; //利润1
int profit2[1111][2]; //利润2
int before_reverse; //颠倒前
int after_reverse; //颠倒后
int i = 0;
int j = 0;
for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0
{
for (int b = 0; b < 7; b++)
{
for (int c = 0; c < 7; c++)
{
for (int d = 0; d < 7; d++)
{
before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d];
after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a];
//因为是颠倒之后所以第一位可以是0
if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200)
//在200-300范围内,我们以正数为准
{
profit1[i][0] = before_reverse;
profit1[i][1] = after_reverse - before_reverse;
i++;
}
else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900)
//在800-900范围内
{
profit1[j][0] = before_reverse;
profit2[j][1] = after_reverse - before_reverse;
j++;
}
}
}
}
}
int answer = 0;
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
if (profit1[a][1] + profit2[b][1] == 558)
{
answer = profit1[b][0];
}
}
}
cout << answer << endl;
return 0;
}
``` ```
### C ### C
```cpp ```cpp
int main()
{
int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字
int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字
int profit1[1111][2]; //利润1
int profit2[1111][2]; //利润2
int before_reverse; //颠倒前
int after_reverse; //颠倒后
int i = 0;
int j = 0;
for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0
{
for (int b = 0; b < 7; b++)
{
for (int c = 0; c < 7; c++)
{
for (int d = 0; d < 7; d++)
{
before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d];
after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a];
//因为是颠倒之后所以第一位可以是0
if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200)
//在200-300范围内,我们以正数为准
{
profit1[i][0] = before_reverse;
profit1[i][1] = after_reverse - before_reverse;
i++;
}
else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900)
//在800-900范围内
{
profit1[j][0] = before_reverse;
profit2[j][1] = after_reverse - before_reverse;
j++;
}
}
}
}
}
int answer = 0;
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
if (profit1[a][1] + profit2[b][1] == 558)
{
answer = profit1[0][b];
}
}
}
cout << answer << endl;
return 0;
}
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册