-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52ac2fc
commit 64fe95f
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# A1104 Sum of Number Segments | ||
|
||
## 1.题意理解 | ||
给出一个正数序列,求它的所有连续子序列的和的和。 | ||
|
||
## 2.思路分析 | ||
选定一个数包含在序列中,向左右分别扩展。想象有两个指针从中间向两侧移动,左指针有i个位置,右指针有(n - i + 1)个位置。 | ||
|
||
柳神的代码中使用double类型,但是这道题的测试点后来有更新,第三个3分case答案错误,需要用更高精度的long double类型。 | ||
## 3.参考代码 | ||
```cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int n; | ||
long double sum = 0, num; | ||
|
||
scanf("%d", &n); | ||
for(int i = 1; i <= n; i++) | ||
{ | ||
cin >> num; | ||
sum += num * i * (n - i + 1); | ||
} | ||
|
||
printf("%.2Lf\n", sum); | ||
return 0; | ||
} | ||
``` | ||
本地输出不对,可能是编译器对```long double```类型的支持有问题,这启示我们,一定要常提交试试。。。。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# A1105 Spiral Matrix | ||
|
||
## 1.题意理解 | ||
模拟一个m行n列的“螺旋矩阵”,从左上角开始,顺时针填充,要求m>=n且最接近。 | ||
|
||
## 2.思路分析 | ||
直接根据题意模拟**顺时针填充**这个过程即可。模拟题最好都在纸上画一画,方便理解题意,也能发现一些边界细节。 | ||
|
||
## 3.参考代码 | ||
```cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int MAX = 128; | ||
int mat[MAX][MAX]; | ||
|
||
int main() | ||
{ | ||
int N, x; | ||
vector<int> vec; | ||
|
||
scanf("%d", &N); | ||
for(int i = 0; i < N; i++) | ||
{ | ||
scanf("%d", &x); | ||
vec.push_back(x); | ||
} | ||
sort(vec.begin(), vec.end(), greater<int>()); | ||
|
||
int n = (int)sqrt(1.0 * N); | ||
while(N % n) n--; | ||
int m = N / n; | ||
|
||
int idx = 0, i = 1, j = 1; | ||
int right = n, down = m, left = 1, up = 2; // 这四个挡板可以在草稿纸上模拟一下,就知道初值设为多少了 | ||
// 动手模拟,会发现总有最后一个元素填不上,后面要单独处理。 | ||
while(idx < N - 1) | ||
{ | ||
while(idx < N - 1 && j < right) mat[i][j++] = vec[idx++]; | ||
right--; | ||
|
||
while(idx < N - 1 && i < down) mat[i++][j] = vec[idx++]; | ||
down--; | ||
|
||
while(idx < N - 1 && j > left) mat[i][j--] = vec[idx++]; | ||
left++; | ||
|
||
while(idx < N - 1 && i > up) mat[i--][j] = vec[idx++]; | ||
up++; | ||
} | ||
// i,j总是指向刚填充完的元素的下一个位置,因此最后必定剩一个 | ||
mat[i][j] = vec[idx]; | ||
|
||
for(i = 1; i <= m; i++) | ||
{ | ||
for(j = 1; j <= n; j++) | ||
{ | ||
printf("%d", mat[i][j]); | ||
if(j == n) printf("\n"); | ||
else printf(" "); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ int main() | |
else | ||
printf(" "); | ||
} | ||
// system("pause"); | ||
|
||
return 0; | ||
} | ||
``` | ||
|