比较两个INT64_T给出不匹配/匹配错误

发布于 2025-01-20 10:15:22 字数 1354 浏览 0 评论 0原文

#include <iostream>
#include <vector>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
int main()
{

    int64_t default_min = 1'000'000'000;
    int64_t n;
    cin >> n;
    vector<int64_t> city(n);
    vector<int64_t> min(n, default_min);
    vector<int64_t> max(n, 0);

    for (auto& i : city) 
    {
        cin >> i;
    }
    for (int i = 0; i < n; i++) {//max
        if (i != 0 || i != n - 1) {
            if (signed int(city[i] - city[i-1]) >= signed int(city[i+1] - city[i]))
            {
                max[i] = abs(city[i] - city[i - 1]);
                min[i] = abs(city[i] - city[i + 1]);
            }
            else {
                min[i] = abs(city[i] - city[i - 1]);
                max[i] = abs(city[i] - city[i + 1]);
            }
        }
        else if (i == 0) {
            min[i] = city[1] - city[0];
            max[i] = city[n-1] - city[0];
        }
        else if (i == n-1) {
            min[i] = city[n-1] - city[n-2];
            max[i] = max[0];
        }
    }
    for (int i=0;i<n;i++)
    {
        cout << min[i] << " " << max[i]<<endl;
    }
    
}

我尝试在比较行中将其转换为无符号或两侧有符号,但它不起作用,我达到了这一点,因为到目前为止我找不到其他方法来轻松比较向量内的值..所以有没有办法通过以更有效的方式转换或比较向量来避免不匹配/匹配错误

#include <iostream>
#include <vector>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
int main()
{

    int64_t default_min = 1'000'000'000;
    int64_t n;
    cin >> n;
    vector<int64_t> city(n);
    vector<int64_t> min(n, default_min);
    vector<int64_t> max(n, 0);

    for (auto& i : city) 
    {
        cin >> i;
    }
    for (int i = 0; i < n; i++) {//max
        if (i != 0 || i != n - 1) {
            if (signed int(city[i] - city[i-1]) >= signed int(city[i+1] - city[i]))
            {
                max[i] = abs(city[i] - city[i - 1]);
                min[i] = abs(city[i] - city[i + 1]);
            }
            else {
                min[i] = abs(city[i] - city[i - 1]);
                max[i] = abs(city[i] - city[i + 1]);
            }
        }
        else if (i == 0) {
            min[i] = city[1] - city[0];
            max[i] = city[n-1] - city[0];
        }
        else if (i == n-1) {
            min[i] = city[n-1] - city[n-2];
            max[i] = max[0];
        }
    }
    for (int i=0;i<n;i++)
    {
        cout << min[i] << " " << max[i]<<endl;
    }
    
}

i tried in the comparison line to cast it as unsigned or signed on both sides but it didn't work ,i reached this point because so far i couldn't find other way to compare values inside vectors easily.. so is there way to avoid the mismatch /match error by either casting or comparing vectors in more efficient way

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文