|
|
发表于 2023-8-21 12:51:13
|
显示全部楼层
问题出在代码的最后一行,你使用了printf函数输出结果,但是题目要求输出结果保留两位小数,你应该使用printf函数的格式控制符"%.2f"来输出结果。
修改后的代码如下:
- #include <bits/stdc++.h>
- using namespace std;
- int n, cnt = 0;
- double x[20], y[20], dict[20][20];
- double ans;
- bool vis[20];
- void dfs(int deep, int now, double sum) {
- cnt++;
- if (cnt >= 10000000) {
- printf("%.2f", ans);
- exit(0);
- }
- if (deep > n) {
- if (sum < ans) ans = sum;
- return;
- }
- for (int i = 1; i <= n; i++)
- if (!vis[ i]) {
- double t = sum + dict[now][ i];
- if (t >= ans) continue;
- vis[ i] = true;
- dfs(deep + 1, i, t);
- vis[ i] = false;
- }
- }
- int main() {
- cin >> n;
- ans = 1e6;
- x[0] = y[0] = 0;
- for (int i = 1; i <= n; i++) cin >> x[ i] >> y[ i];
- for (int i = 0; i <= n; i++)
- for (int j = 0; j <= n; j++)
- dict[ i][j] = sqrt((x[ i] - x[j]) * (x[ i] - x[j]) + (y[ i] - y[j]) * (y[ i] - y[j]));
- dfs(1, 0, 0);
- printf("%.2f", ans);
- return 0;
- }
复制代码
这样就可以正确输出结果了。 |
|