UVA11549 Computer Puzzles (Floyd algorithm)

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <Set>7#include <sstream>8 using namespacestd;9 /*int next1 (int n,int k)Ten { One StringStream SS; A ss<< (long Long) k*k; - string S=ss.str (); - if (s.length () >n) s=s.substr (0,n);//result too long, take only the first n bits the int ans; - StringStream SS2 (s); - ss2>>ans; - return ans; + }*/ - intbuf[ -]; + intNEXT2 (intNintk) A { at     if(!k)return 0; -     Long LongK2= (Long Long) k*K; -     intL=0; -      while(k2>0){ -buf[l++]=k2%Ten; k2/=Ten; -     } in     if(n>l) n=L; -     intans=0; to      for(intI=0; i<n;i++) +ans=ans*Ten+BUF[--L];//re-assemble the former min{n,l} bit -     returnans; the } * /*int main () $ {Panax Notoginseng int T; - cin>>t; the While (t--) +     { A int n,k; the cin>>n>>k; + set<int>s; - int ans=k; $ While (!s.count (k)) $         { - S.insert (k); - if (K>ans) ans=k; the K=next1 (n,k); -         }Wuyi cout<<ans<<endl; the     } - return 0; Wu } - */ About intMain () $ { -     intT; -Cin>>T; -      while(t--) A     { +         intn,k; theCin>>n>>K; -         intans=K; $         intK1=k;intK2=K; the          Do{ thek1=next2 (N,K1); theK2=NEXT2 (N,K2);if(K2>ans) ans=K2; theK2=NEXT2 (N,K2);if(K2>ans) ans=K2; -} while(k1!=K2); incout<<ans<<Endl; the     } the     return 0; About}

Suppose there are two children running on an "infinitely forward" roundabout, while one of the children is twice times faster than the other, then the fast-running child will chase the slow-running child,

Commented out code more time Next1 more than 4 seconds, next2 to 1 seconds.

UVA11549 Computer Puzzles (Floyd algorithm)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.