鱼C论坛

 找回密码
 立即注册
查看: 4200|回复: 43

[已解决]求助luoguP1433

[复制链接]
发表于 2023-8-21 12:51:13 | 显示全部楼层
问题出在代码的最后一行,你使用了printf函数输出结果,但是题目要求输出结果保留两位小数,你应该使用printf函数的格式控制符"%.2f"来输出结果。

修改后的代码如下:

  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. int n, cnt = 0;
  4. double x[20], y[20], dict[20][20];
  5. double ans;
  6. bool vis[20];

  7. void dfs(int deep, int now, double sum) {
  8.     cnt++;
  9.     if (cnt >= 10000000) {
  10.         printf("%.2f", ans);
  11.         exit(0);
  12.     }
  13.     if (deep > n) {
  14.         if (sum < ans) ans = sum;
  15.         return;
  16.     }
  17.     for (int i = 1; i <= n; i++)
  18.         if (!vis[ i]) {
  19.             double t = sum + dict[now][ i];
  20.             if (t >= ans) continue;
  21.             vis[ i] = true;
  22.             dfs(deep + 1, i, t);
  23.             vis[ i] = false;
  24.         }
  25. }

  26. int main() {
  27.     cin >> n;
  28.     ans = 1e6;
  29.     x[0] = y[0] = 0;
  30.     for (int i = 1; i <= n; i++) cin >> x[ i] >> y[ i];
  31.     for (int i = 0; i <= n; i++)
  32.         for (int j = 0; j <= n; j++)
  33.             dict[ i][j] = sqrt((x[ i] - x[j]) * (x[ i] - x[j]) + (y[ i] - y[j]) * (y[ i] - y[j]));
  34.     dfs(1, 0, 0);
  35.     printf("%.2f", ans);

  36.     return 0;
  37. }
复制代码


这样就可以正确输出结果了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-2 04:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表