using namespace std;
#include<iostream>
class biker
{
public:
int initspeed, acc, speeding;
};
void input(int n, biker a[])
{
for (int i = 0; i < n; i++)
{
//cout<<"Enter the initspeed:"<<"\n";
std::cin >> a[i].initspeed;
//cout<<"Enter the acceleration:"<<"\n";
std::cin >> a[i].acc;
}
}
int main()
{
int n = 0, i = 0, t = 0, max_speed = 0, flag = 0, minimum = 0;
biker a[100];
std::cin >> t;
for (int k = 0; k < t; k++)
{
//cout<<"Enter no of bikers"<<"\n";
std::cin >> n;
//cout<<"enter the max track speed"<<"\n";
std::cin >> max_speed;
//cout<<"Enter the min niker speed"<<"\n";
std::cin >> minimum;
input(n, a);
int j = 1, x = 0;
while (flag < 500)
{
int sum = 0;
for (i = 0; i < n; i++)
{
int prod = 0;
prod = a[i].acc * j;
x = a[i].initspeed + prod;
// cout<<"VAL"<<x<<"\n";
if (x >= minimum)
{
//cout<<"It is greater than minimum";
sum = sum + a[i].initspeed + prod;
//cout<<sum<<"\n";
}
}
if (sum >= max_speed)
{
//cout<<"MAXIMUM ACHIEVED\n";
x = sum;
break;
}
j++;
flag++;
}
//cout<<x<<"\n";
std::cout << j;
}
return 0;
}
以上代码是我在geeksforgeeks中bike racing problem的解决方案。
要点:
组织自行车比赛。将有N个骑自行车的人。你可以通过Hi获得第i个骑自行车的人的初始速度和每小时的Ai KiloMeters的第i个骑车者的加速度。
组织者希望骑车人和观众的安全。他们会在每小时后监控赛道上的总速度。 速度为“L”或更高的骑车人被认为是快速骑车人。 计算赛道上的总速度 - 他们在那个时刻添加每个快速骑车的速度。
一旦赛道上的总速度为每小时“M”千克计或更高,安全警报就会响起。
您需要告知安全警报发出嗡嗡声的最小小时数。
输入:
输出:
对于每个测试用例 - 输出一个整数,表示警报蜂鸣后的最小小时数。
约束:
我的代码在我的计算机上以及ide上正常运行。但是当我点击提交时,它会崩溃返回分段错误(SIGSEGV)。
答案 0 :(得分:0)
数组a
最多可包含100个骑车人。问题陈述指定骑车人的数量可以达到10,000。
超越数组的末尾会调用Undefined Behaviour (UB),这会在判断系统中显示为段错误。 UB在您的开发系统上表现不同,或者您没有使用足够多的骑车人进行测试。