比较两个INT64_T给出不匹配/匹配错误
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论