惊悚算法计算给定长度的数字串是否有重复数字出现

(传说RDTSC计时会不准确)

#include<iostream>
using namespace std;
unsigned long long RDTSC()
{
    __asm("RDTSC");
}
bool algorithm1(unsigned long long a, int length=10) //惊悚算法
{
    int map[]={2,3,5,7,11,13,17,19,23,29};
    unsigned long long foo=1;
    if(a>9876543210) return false;
    for(int i=0;i<length;++i)
    {
        if(foo % map[a%10] ==0)
        {
            return false;
        }
        foo*=map[a%10];
        a/=10;
    }
    return true;
}
bool algorithm2(unsigned long long a, int length=10)
{
    bool hash[10]={0};
    if(a>9876543210) return false;
    for(int i=0;i<length;++i)
    {
        if(hash[a%10])
            return false;
        hash[a%10]=true;
        a/=10;
    }
    return true;
}
int main()
{
    unsigned long long n;
    cin>>n;
    bool a,b;
    unsigned long long t1,t2,t3,t4;
    t1=RDTSC();
    t2=RDTSC();
    a=algorithm1(n,5);
    t3=RDTSC();
    b=algorithm2(n,5);
    t4=RDTSC();
    cout<<"algorithm1: "<<a<<" "<< t3-t2-(t2-t1) <<endl;
    cout<<"algorithm2: "<<b<<" "<< t4-t3-(t2-t1) <<endl;
    return 0;
}

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/4878QrCode