【发布时间】:2018-03-26 21:19:04
【问题描述】:
我的代码是 C++ 并且目前正在努力完成任务。下面给出的链接
难度:中等
我的算法适用于 20 个测试用例中的 18 个。 其他 2 个因超时而终止。
我知道这意味着什么,但现在我不知道如何提高算法的效率。
我在下面给出了我的代码,任何人都可以帮我解决这个问题 https://www.hackerrank.com/challenges/and-product
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t;
unsigned long int x,y,a;
cin>>t;
while(t)
{
cin>>x>>y;
a=x;
for(;x<=y;x++)
a=a&(x);
cout<<a<<"\n";
t--;
}
return 0;
}
【问题讨论】:
-
我建议在codereview.stackexchange.com 发布代码改进问题。
-
首先解决问题(在最坏的 O(log x) 时间内),“给定 x 设置了位 b,找到最小的 y>x 没有设置位 b。”
-
这意味着你的代码可以工作,但它的时间复杂度需要改进。
标签: c++ algorithm timeout terminate