From 4c49dc90b394b574bfab700d4030c981f0150b8b Mon Sep 17 00:00:00 2001 From: zhangzc Date: Thu, 28 Oct 2021 17:36:54 +0800 Subject: [PATCH] update exercises --- .../solution.md" | 103 +++++++- .../solution.md" | 132 +++++++++- .../solution.md" | 249 +++++++++++++++++- 3 files changed, 458 insertions(+), 26 deletions(-) diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\346\225\260\345\210\227/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\346\225\260\345\210\227/solution.md" index ad9fe776..60469032 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\346\225\260\345\210\227/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\346\225\260\345\210\227/solution.md" @@ -15,24 +15,14 @@ #### 输出格式 输出一个整数表示答案。 -#### 数据范围 -``` -2≤N≤100000, -0≤Ai≤109 -1 -2 -``` #### 输入样例: ``` 5 2 6 4 10 20 -1 -2 ``` #### 输出样例: ``` 10 -1 ``` #### 样例解释 包含 2、6、4、10、20 的最短的等差数列是 2、4、6、8、10、12、14、16、18、20。 @@ -41,7 +31,17 @@ ## aop ### before ```cpp +#include +#include + +using namespace std; +const int N = 1e5 + 10; +int a[N]; +int gcd(int a, int b) +{ + return b ? gcd(b, a % b) : a; +} ``` ### after ```cpp @@ -50,21 +50,98 @@ ## 答案 ```cpp +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + + sort(a, a + n); + + int d = 0; + for (int i = 1; i < n; i++) + d = gcd(d, a[i] - a[0]); + + if (d) + cout << (a[n - 1] - a[0]) / d + 1; + else + cout << n; + + return 0; +} ``` ## 选项 ### A ```cpp - +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + + sort(a, a + n); + + int d = 0; + for (int i = 1; i < n; i++) + d = gcd(d, a[i] - a[0]); + + if (d) + cout << (a[n] - a[0]) / d + 1; + else + cout << n; + + return 0; +} ``` ### B ```cpp - +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + + sort(a, a + n); + + int d = 0; + for (int i = 1; i < n; i++) + d = gcd(d, a[i] - a[0]); + + if (d) + cout << (a[n - 1] - a[0]) / d; + else + cout << n; + + return 0; +} ``` ### C ```cpp - +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + + sort(a, a + n); + + int d = 0; + for (int i = 1; i < n; i++) + d = gcd(d, a[i] - a[0]); + + if (d) + cout << (a[n + 1] - a[0]) / d + 1; + else + cout << n; + + return 0; +} ``` diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\347\264\240\346\225\260\345\210\227/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\347\264\240\346\225\260\345\210\227/solution.md" index 61ba1fa4..fa41840d 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\347\264\240\346\225\260\345\210\227/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\345\267\256\347\264\240\346\225\260\345\210\227/solution.md" @@ -15,30 +15,154 @@ ## aop ### before ```cpp +#include +#include +#include +using namespace std; +typedef long long LL; +set all; + +bool isPrimt(LL t) +{ + for (int i = 2; i < t / 2; ++i) + { + if (t % i == 0) + return false; + } + return true; +} ``` ### after ```cpp +const int N = 5000; +int main() +{ + LL a[N]; + + a[0] = 2; + a[1] = 3; + all.insert(2); + all.insert(3); + int index = 2; + LL t = 5; + while (index < N) + { + if (isPrimt(t)) + { + a[index++] = t; + all.insert(t); + } + t++; + } + + printf("%d\n", f(a, N)); + return 0; +} ``` ## 答案 ```cpp - +int f(LL a[], int n) +{ + for (int i = 0; i < n; ++i) + { + LL first = a[i]; + for (int delta = 1; delta < a[n - 1] - first; ++delta) + { + int m = first; + for (int j = 1; j < 10; ++j) + { + m += delta; + if (all.find(m) == all.end()) + break; + if (m > a[n - 1]) + break; + if (j == 9) + return delta; + } + } + } + return -1; +} ``` ## 选项 ### A ```cpp - +int f(LL a[], int n) +{ + for (int i = 0; i < n; ++i) + { + LL first = a[i]; + for (int delta = 1; delta < a[n] - first; ++delta) + { + int m = first; + for (int j = 1; j < 10; ++j) + { + m += delta; + if (all.find(m) == all.end()) + break; + if (m > a[n - 1]) + break; + if (j == 9) + return delta; + } + } + } + return -1; +} ``` ### B ```cpp - +int f(LL a[], int n) +{ + for (int i = 0; i < n; ++i) + { + LL first = a[i]; + for (int delta = 1; delta < a[n + 1] - first; ++delta) + { + int m = first; + for (int j = 1; j < 10; ++j) + { + m += delta; + if (all.find(m) == all.end()) + break; + if (m > a[n - 1]) + break; + if (j == 9) + return delta; + } + } + } + return -1; +} ``` ### C ```cpp - +int f(LL a[], int n) +{ + for (int i = 0; i < n; ++i) + { + LL first = a[i]; + for (int delta = 1; delta < a[n + 1] + first; ++delta) + { + int m = first; + for (int j = 1; j < 10; ++j) + { + m += delta; + if (all.find(m) == all.end()) + break; + if (m > a[n - 1]) + break; + if (j == 9) + return delta; + } + } + } + return -1; +} ``` diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\350\205\260\344\270\211\350\247\222\345\275\242/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\350\205\260\344\270\211\350\247\222\345\275\242/solution.md" index d1ddb692..2625353c 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\350\205\260\344\270\211\350\247\222\345\275\242/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\255\211\350\205\260\344\270\211\350\247\222\345\275\242/solution.md" @@ -4,7 +4,7 @@ 先用1,2,3,…的自然数拼一个足够长的串 用这个串填充三角形的三条边。从上方顶点开始,逆时针填充。 比如,当三角形高度是8时: -![](https://img-blog.csdnimg.cn/20190203174943179.png) +![](https: 输入,一个正整数n(3 +using namespace std; +string str; +stringstream ss; ``` ### after ```cpp @@ -43,21 +46,249 @@ ## 答案 ```cpp - +int main() +{ + int n; + cin >> n; + int x = 2 * n - 1; + for (int i = 1; i <= 1500; i++) + { + string str1; + ss << i; + ss >> str1; + str += str1; + ss.clear(); + } + int length = 4 * n - 4; + for (int i = 1; i <= n; i++) + { + if (i != n && i != 1) + { + for (int j = 1; j <= n + i - 1; j++) + { + if (j == n - i + 1) + { + cout << str[i - 1]; + } + else if (j == 2 * n - (n - i + 1)) + { + cout << str[4 * n - 4 - (i - 1)] << endl; + } + else + { + cout << '.'; + } + } + } + else if (i == 1) + { + for (int x = 1; x <= n; x++) + { + if (x == n) + { + cout << '1' << endl; + } + else + { + cout << '.'; + } + } + } + else + { + for (int m = 1; m <= 2 * n - 1; m++) + { + cout << str[n + (m - 2)]; + } + } + } + return 0; +} ``` ## 选项 ### A ```cpp - +int main() +{ + int n; + cin >> n; + int x = 2 * n - 1; + for (int i = 1; i <= 1500; i++) + { + string str1; + ss << i; + ss >> str1; + str += str1; + ss.clear(); + } + int length = 4 * n - 4; + for (int i = 1; i <= n; i++) + { + if (i != n && i != 1) + { + for (int j = 1; j <= n + i - 1; j++) + { + if (j == n - i + 1) + { + cout << str[i - 1]; + } + else if (j == 2 * n - (n - i + 1)) + { + cout << str[4 * n - 4 - i] << endl; + } + else + { + cout << '.'; + } + } + } + else if (i == 1) + { + for (int x = 1; x <= n; x++) + { + if (x == n) + { + cout << '1' << endl; + } + else + { + cout << '.'; + } + } + } + else + { + for (int m = 1; m <= 2 * n - 1; m++) + { + cout << str[n + (m - 2)]; + } + } + } + return 0; +} ``` ### B ```cpp - +int main() +{ + int n; + cin >> n; + int x = 2 * n - 1; + for (int i = 1; i <= 1500; i++) + { + string str1; + ss << i; + ss >> str1; + str += str1; + ss.clear(); + } + int length = 4 * n - 4; + for (int i = 1; i <= n; i++) + { + if (i != n && i != 1) + { + for (int j = 1; j <= n + i - 1; j++) + { + if (j == n - i + 1) + { + cout << str[i - 1]; + } + else if (j == 2 * n - (n - i + 1)) + { + cout << str[4 * n - 4 - (i + 1)] << endl; + } + else + { + cout << '.'; + } + } + } + else if (i == 1) + { + for (int x = 1; x <= n; x++) + { + if (x == n) + { + cout << '1' << endl; + } + else + { + cout << '.'; + } + } + } + else + { + for (int m = 1; m <= 2 * n - 1; m++) + { + cout << str[n + (m - 2)]; + } + } + } + return 0; +} ``` ### C ```cpp - +int main() +{ + int n; + cin >> n; + int x = 2 * n - 1; + for (int i = 1; i <= 1500; i++) + { + string str1; + ss << i; + ss >> str1; + str += str1; + ss.clear(); + } + int length = 4 * n - 4; + for (int i = 1; i <= n; i++) + { + if (i != n && i != 1) + { + for (int j = 1; j <= n + i - 1; j++) + { + if (j == n - i + 1) + { + cout << str[i - 1]; + } + else if (j == 2 * n - (n - i + 1)) + { + cout << str[4 * n - 4 + i] << endl; + } + else + { + cout << '.'; + } + } + } + else if (i == 1) + { + for (int x = 1; x <= n; x++) + { + if (x == n) + { + cout << '1' << endl; + } + else + { + cout << '.'; + } + } + } + else + { + for (int m = 1; m <= 2 * n - 1; m++) + { + cout << str[n + (m - 2)]; + } + } + } + return 0; +} ``` -- GitLab