前前后后共写了三种版本,Java的运行的最快,C#的最慢,C++次之.我不想说这说明这三种语言谁的更好,我想造成差异的主要原因是我自己写代码水平不够好...至少C++的版本有很多可以优化的地方.
居然用java写出来的运行的最快,我最不熟的就是java了,其它两个都做过项目,就java只是学了个半调子...看来java入门是比较简单(玩笑话)
第一个,java版,执行时间21秒
package thinkinjava;
import java.util.Arrays;
import java.util.HashSet;
/**
*
* @author lyra
*/
public class VampireNumber {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
printvampirenum(1);
printvampirenum(2);
printvampirenum(3);
printvampirenum(4);
//printnum(5);//这个好像不太现实了
}
public static void printvampirenum(int faclen)
{
long[] product = new long[faclen*2];
long[] factor = new long[faclen*2];
long[] exp = new long[faclen*2+1];
HashSet<Long> has= new HashSet<Long>();
exp[0]=1L;
for(int i=1;i<=2*faclen;++i)
exp[i]=exp[i-1]*10L;
for(long i = exp[faclen-1]; i <= exp[faclen] -1 ; ++i)
for(long j= exp[faclen] -1 ; j>=i;--j)
{
long s = i * j;
if(s%100 == 0)
continue;
if(s<exp[2*faclen-1])
continue;
for(int n=0;n!=faclen;++n)
{
factor[n]=i%exp[n+1]/exp[n];
factor[n+faclen]=j%exp[n+1]/exp[n];
}
for(int n=0;n!= 2*faclen;++n)
{
product[n]=s%exp[n+1]/exp[n];
}
Arrays.sort(product);
Arrays.sort(factor);
if(Arrays.equals(product, factor))
{
if(has.contains(s))
{
//System.out.printf("[iterant]\t%d = %d \u00d7 %d;\n",s,i,j);//打印重复数据
continue;
}
has.add(s);
System.out.printf("%d = %d \u00d7 %d;\n",s,i,j);
}
}
}
}
第二个版本,C#版,执行时间28秒(x86),26秒(x64)
using System.Collections.Generic;
using System;
namespace VampireNumber
{
class Program
{
public static void Main(string[] args)
{
DateTime dt = DateTime.Now;
PrintVampirenum(2);
PrintVampirenum(3);
PrintVampirenum(4);
Console.Write("运行时间{0}\n",DateTime.Now - dt);
Console.ReadKey(true);
}
public static bool ArraysEquals(long[] a, long[] a2)
{
if (a == a2)
return true;
if (a == null || a2 == null)
return false;
int length = a.Length;
if (a2.Length != length)
return false;
for (int i = 0; i < length; i++)
if (a[i] != a2[i])
return false;
return true;
}
public static void PrintVampirenum(int faclen)
{
long[] product = new long[faclen*2];
long[] factor = new long[faclen*2];
long[] exp = new long[faclen*2+1];
HashSet<long> has= new HashSet<long>();
exp[0]=1L;
for(int i=1;i<=2*faclen;++i)
exp[i]=exp[i-1]*10L;
for (long i = exp[faclen - 1]; i <= exp[faclen] - 1; ++i)
{
for (long j = exp[faclen] - 1; j >= i; --j)
{
long s = i * j;
if (s % 100 == 0)
continue;
if (s < exp[2 * faclen - 1])
continue;
for (int n = 0; n != faclen; ++n)
{
factor[n] = i % exp[n + 1] / exp[n];
factor[n + faclen] = j % exp[n + 1] / exp[n];
}
for (int n = 0; n != 2 * faclen; ++n)
{
product[n] = s % exp[n + 1] / exp[n];
}
Array.Sort(product);
Array.Sort(factor);
if (ArraysEquals(product, factor))
{
if (has.Contains(s))
{
//System.out.printf("[iterant]\t%d = %d \u00d7 %d;\n",s,i,j);
continue;
}
has.Add(s);
//System.out.printf("%d = %d \u00d7 %d;\n",s,i,j);
Console.Write("{0} = {1} \u00d7 {2};\n", s, i, j);
}
}
}
}
}
}
第三个,C++版,运行时间22秒
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cstdint>
#include<conio.h>
#include<ctime>
using std::cout;
using std::endl;
using std::set;
struct lex_compare {
bool operator() (const int64_t& lhs, const int64_t& rhs) const
{
return lhs < rhs;
}
};
void PrintVampirenum(int);
bool equal(int * l, int * r, int count);
void select_sort(int* dst, int count);
int main()
{
int i = time(0);
PrintVampirenum(2);
PrintVampirenum(3);
PrintVampirenum(4);
cout<<"运行时间"<<time(0)-i<<"秒"<<std::endl;
getch();
}
void PrintVampirenum(int faclen)
{
int32_t product[20];
int32_t factor[20];
int64_t exp[20];
set<int64_t, lex_compare> has;
has.insert(0L);
exp[0] = 1L;
for(int i=1;i<=2*faclen;++i)
exp[i]=exp[i-1]*10L;
for (int64_t i = exp[faclen - 1]; i <= exp[faclen] - 1; ++i)
{
for (int64_t j = exp[faclen] - 1; j >= i; --j)
{
int64_t s = i * j;
if (s % 100 == 0)
continue;
if (s < exp[2 * faclen - 1])
continue;
for (int n = 0; n != faclen; ++n)
{
factor[n] = i % exp[n + 1] / exp[n];
factor[n + faclen] = j % exp[n + 1] / exp[n];
}
for (int n = 0; n != 2 * faclen; ++n)
{
product[n] = s % exp[n + 1] / exp[n];
}
select_sort(factor, 2* faclen);
select_sort(product,2* faclen);
if(equal(factor,product, 2*faclen))
{
if(has.end()!= has.find(s))
{
continue;
}
has.insert(s);
cout<<s<<" = " <<i<<" * "<<j<<endl;
}
}
}
}
bool equal(int * l, int * r, int count)
{
int i =0;
while(l[i]==r[i])
{
++i;
if(i==count) return true;
}
return false;
}
void select_sort(int* dst, int count)
{
register int i, j, min, t;
for( i = 0; i < count - 1; i ++)
{
min = i;
//查找最小值
for( j = i + 1; j < count; j ++)
if( dst[ min] > dst[ j])
min = j;
//交换
if( min != i)
{
t = dst[ min];
dst[ min] = dst[ i];
dst[ i] = t;
}
}
}