You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kattis平台Adolescent Architecture问题求解出错,求技术支持

Kattis Adolescent Architecture 问题排查求助

我在解决Kattis平台的Adolescent Architecture问题时,提交的C++代码返回错误答案,希望能帮忙排查问题。

解题思路

  • 排序规则:按形状的表面维度升序排序
    • 若形状相同,直接比较维度大小
    • 若形状不同(立方体vs圆柱),比较立方体底面正方形的对角线与圆柱底面圆的直径(核心逻辑:判断正方形能放进圆内或圆能放进正方形内的等效条件)
  • 堆叠检查规则:排序后依次检查相邻的两个形状(上方的S[i]和下方的S[j],i=j-1)是否满足堆叠条件
    • 圆柱放在立方体上:圆柱直径 ≤ 立方体边长
    • 立方体放在圆柱上:立方体底面正方形的对角线 ≤ 圆柱直径
    • 形状相同时:上方形状的维度 ≤ 下方形状的维度
  • 正方形对角线计算:使用公式 diagonal = √2 * 边长,代码中用宏ROOT_2存储√2的近似值1.41428571429

错误代码

#include<iostream>
#define ROOT_2 1.41428571429

bool cube_on_cylinder(int dCb, int dCy) { //test if cube can be placed on cylinder
  return ROOT_2 * dCb <= 2 * dCy;
}

bool cylinder_on_cube(int dCy, int dCb) { //test if cylinder can be placed on cube
  return 2 * dCy <= dCb;
}

bool shapeA_on_shapeB(char *shapeA, int dA, char *shapeB, int dB) { // test if shapeA can be placed on shapeB
  if(shapeA[1] == shapeB[1]) return dA <= dB;
  if(shapeA[1] == 'u') return cube_on_cylinder(dA, dB);
  return cylinder_on_cube(dA, dB);
}

bool shapeA_gt_shapeB(char *shapeA, int dA, char *shapeB, int dB) {
  if(shapeA[1] == shapeB[1]) return dA > dB;
  if(shapeA[1] == 'u') return ROOT_2 * dA > 2 * dB;
  return 2 * dA > ROOT_2 * dB;
}
int main() {
  int n, *ds;
  char **shapes;
  std::cin >> n;
  ds = new int[n];
  shapes = new char*[n];

  for(int i = 0; i < n; i++) {
    shapes[i] = new char[8];
    std::cin >> shapes[i] >> ds[i];
  }

  for(int i = 0; i < n; i++) { // sorting
    for(int j = i + 1; j < n; j++) {
      if(shapeA_gt_shapeB(shapes[i], ds[i], shapes[j], ds[j])) {
        char *tempShape = shapes[i];
        shapes[i] = shapes[j];
        shapes[j] = tempShape;
        int tempD = ds[i];
        ds[i] = ds[j];
        ds[j] = tempD;
      }
    }
  }

  for(int i = 0, j = 1; j < n; i++, j++) {
    if(!shapeA_on_shapeB(shapes[i], ds[i], shapes[j], ds[j])) {
      std::cout << "impossible";
      return 0;
    }
  }

  for(int i = 0; i < n; i++) {
    std::cout << shapes[i] << " " << ds[i] << "\n";
  }
  return 0;
}

提交以上代码后未通过测试,请求帮忙排查问题。

内容的提问来源于stack exchange,提问作者Kelden Zimba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 09:37:14