In PKU OJ,use G++ will AC, but use C++ will TLE.
I think I should prune some useless branches.
在PKU 用 G++可以AC過,用C++會TLE
可能要用點剪枝,還在努力中讓兩個都過...
The following program is my ACcepted code for PKU OJ 1426 Find The Multiple (only in G++).
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃PKU 1426 的AC code! (只有在G++)
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
點這裡看題目 Click here to see this Problem!
//This program is for PKU OJ 1426 Find The Multiple
//http://poj.org/problem?id=1426
#include<stdio .h="">
#include<iostream>
#include<queue>
using namespace std;
long long int BFS(int n)//Breadth First Search
{
long long int ans;
queue<long int="" long=""> q;
while(!q.empty())//clear the queue
q.pop();
q.push(1);
while(1)//use while(!q.empty()) will get WA.... = = and I don't know why.
{
ans = q.front();//access the "oldest" element in the queue
q.pop();//pop the above "oldest" element in the queue
if(ans % n == 0)
return ans;
q.push(10 * ans);
q.push(10 * ans + 1);
}
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
if( n == 0)
break;
printf("%lld\n", BFS(n));
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
I think I should prune some useless branches.
在PKU 用 G++可以AC過,用C++會TLE
可能要用點剪枝,還在努力中讓兩個都過...
The following program is my ACcepted code for PKU OJ 1426 Find The Multiple (only in G++).
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃PKU 1426 的AC code! (只有在G++)
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
點這裡看題目 Click here to see this Problem!
//This program is for PKU OJ 1426 Find The Multiple
//http://poj.org/problem?id=1426
#include<stdio .h="">
#include<iostream>
#include<queue>
using namespace std;
long long int BFS(int n)//Breadth First Search
{
long long int ans;
queue<long int="" long=""> q;
while(!q.empty())//clear the queue
q.pop();
q.push(1);
while(1)//use while(!q.empty()) will get WA.... = = and I don't know why.
{
ans = q.front();//access the "oldest" element in the queue
q.pop();//pop the above "oldest" element in the queue
if(ans % n == 0)
return ans;
q.push(10 * ans);
q.push(10 * ans + 1);
}
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
if( n == 0)
break;
printf("%lld\n", BFS(n));
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!