百度2017春招笔试真题编程题集合-2
2017-05-09 20:08
441 查看
题目:一个数轴上共有N个点,第一个点的坐标是度度熊现在位置,第N-1个点是度度熊的家。现在他需要依次的从0号坐标走到N-1号坐标。
但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个点,并直接将这个点忽略掉,问度度熊回家至少走多少距离?
以下是源码
但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个点,并直接将这个点忽略掉,问度度熊回家至少走多少距离?
以下是源码
#include<stdio.h> #include<stdlib.h> int rtres(int *M, int x, int N)//给定去除的点的编号,返回此种选择的距离。 { int result = 0; int temp = M[0]; int i = 0; for (int i = 0; i < N; i++) { if (i != x) { int abst = M[i] - temp; if (abst < 0) abst = -abst; result += abst; temp = M[i]; } } return result; } int main() { int N = 0; int i = 0, j = 0; int result = 20000; int temp = 0; scanf("%d", &N); int *M = (int *)malloc(sizeof(int)*N); for (i = 0; i<N; i++) { scanf("%d", &M[i]); } for (i = 1; i < N-1; i++) { temp = rtres(M, i, N); if (temp < result)//选择所有的距离中最近的一个 result = temp; } printf("%d", result); free(M); // printf("\n"); // system("pause"); return 0; }
相关文章推荐
- 百度2017春招笔试真题编程题集合--Python
- 牛客网 之 百度2017春招笔试真题编程题集合(共5题)
- 百度2017春招笔试真题编程题集合-4
- 2017百度实习生招聘笔试真题编程题集合
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合:不等式数列 [python]
- 百度2017春招笔试真题编程题集合
- 3--百度2017春招笔试真题编程题集合--寻找三角形 (Python)
- 百度2017春招笔试真题编程题集合:买帽子 [python]
- 4--百度2017春招笔试真题编程题集合--有趣的排序 (Python)
- 百度2017春招笔试真题编程题集合--不等式数列
- 百度2017春招笔试真题编程题集合:寻找三角形 [python]
- 百度2017春招笔试真题编程题集合之寻找三角形
- 百度2017春招笔试真题编程题集合:度度熊回家 [python]
- 2--百度2017春招笔试真题编程题集合--度度熊回家 (Python)
- 百度2017春招笔试真题编程题集合:有趣的排序 [python]
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合之买帽子
- 1--百度2017春招笔试真题编程题集合--买帽子(Python)
- 百度2017春招笔试真题编程题集合-5