The 70th problem,UVa10396 Vampire Numbers
今天看Thinking in Java看到一个吸血鬼数的问题,于是查找UVa里也有类似的问题就动手写了先是用Java写的,不过WA了两次,然后没有发现错误,又用c++写的还是不行。最后发现要排序去重。然后改用Java的SortedSet解决了这个问题,主要就是暴力枚举求解。但是同样的算法Java用了将近5秒。c++只用了1秒。差距啊。还有避免不必要的重复的循环不能为00,还有不能为奇数。尽量提高程序的效率。
题目描述:
Problem D
Vampire Numbers
Input: standard input
Output: standard output
Time Limit: 5 seconds
A number v = xy with an even number (n) of digits formed by multiplying a pair of n/2-digit numbers (where the digits are taken from the original number in any order) x and y together is known as vampire number. Pairs of trailing zeros (Both the numbers have a trailing zero) are not allowed. If v is a vampire number then x and y are called its "fangs." Examples of 4-digit vampire numbers include
1) 21 x 60 = 1260
2) 15 x 93 = 1395
3) 35 x 41 = 1435
4) 30 x 51 = 1530
5) 21 x 87 = 1827
6) 27 x 81 = 2187
7) 80 x 86 = 6880
In this program you will have to find all the 4, 6 and
8 digit even vampire numbers.
Input
The input file contains maximum ten lines of input. Each line contains a single integer n whose value is
4, 6 or 8. Input is terminated by end of file.
Output
For each input n produce all the n-digit vampire numbers that are even in ascending order. Print a blank line after the output for each set of input.
Sample Input:
4
4
Sample Output:
1260
1530
6880
1260
1530
6880
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std; int main()
{
bool isNum(int a,int b);
int n;
int range[] = {1,10,100,1000,10000};
int set[10][10000],flag[10];
memset(set,0,sizeof(range));
memset(flag,0,sizeof(flag));
while(scanf("%d",&n) != -1)
{
if(flag[n] == -1)
{
int k = 0;
for(int i=range[n / 2 - 1]; i<range[n / 2]; i++)
{
for(int j=i; j<range[n / 2]; j++)
{
if(i % 2 != 0 && j % 2 != 0)continue;
if(i % 10 == 0 && j % 10 == 0)continue;
if(isNum(i,j))
{
set[n][k] = i * j;
k++;
}
}
} }
flag[n] = -1;
for(int i=0; ; i++)
{
if(set[n][i] == 0)break;
printf("%d",set[n][i]);
}
printf("\n"); } }
bool isNum(int a,int b)
{
int arr[10];
int mult = a * b;
while(a != 0)
{
arr[a % 10]++;
a /= 10;
}
while(b != 0)
{
arr[b % 10]++;
b /= 10;
}
while(mult != 0)
{
arr[mult % 10]--;
mult /= 10;
}
for(int i=0; i<10; i++){
if(arr[i] != 0)
return false;
}
return true;
}
Java代码:
<pre class="java" name="code">import java.util.*;
public class Main10396 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] range = {1,10,100,1000,10000};
Object[] temp = new Object[100000];
int[] flag = new int[10];
SortedSet<Integer> set4 = new TreeSet<Integer>();
SortedSet<Integer> set6 = new TreeSet<Integer>();
SortedSet<Integer> set8 = new TreeSet<Integer>();
while(scan.hasNext()){
int n = scan.nextInt();
int k = 0;
if(flag[n] != -1){
for(int i=range[n / 2 - 1]; i<range[n / 2]; i++){
for(int j=i; j<range[n / 2]; j++){
if((i % 2 != 0)&& (j % 2 != 0))continue;//除去奇数
if(i % 10 == 0 && j % 10 == 0)continue;//除去00
if(isNum(i,j) == true){
if(n == 4){
set4.add(i * j);
}
if(n == 6){
set6.add(i * j);
}
if(n == 8){
set8.add(i * j);
}
}
}
}
}
flag[n] = -1;
if(n == 4){
for(int i : set4){
System.out.println(i);
}
}
if(n == 6){
for(int i : set6){
System.out.println(i);
}
}
if(n == 8){
for(int i : set8){
System.out.println(i);
}
}
System.out.println();
}
}
public static boolean isNum(int a,int b){
int[] arr = new int[10];
int mult = a * b;
while(a != 0){
arr[a % 10]++;
a /= 10;
}
while(b != 0){
arr[b % 10]++;
b /= 10;
}
while(mult != 0){
arr[mult % 10]--;
mult /= 10;
}
for(int i=0; i<10; i++){
if(arr[i] != 0)
return false;
}
return true;
}
}
最后说一句Tinking in Java 讲的不错。
The 70th problem,UVa10396 Vampire Numbers的更多相关文章
- (Problem 21)Amicable numbers
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into ...
- HNUSTOJ-1565 Vampire Numbers(暴力打表)
1565: Vampire Numbers 时间限制: 3 Sec 内存限制: 128 MB提交: 20 解决: 9[提交][状态][讨论版] 题目描述 The number 1827 is an ...
- 覆盖问题:最大覆盖问题(Maximum Covering Location Problem,MCLP)和集覆盖问题(Location Set Covering Problem,LSCP)
集覆盖问题研究满足覆盖所有需求点顾客的前提下,服务站总的建站个数或建 设费用最小的问题.集覆盖问题最早是由 Roth和 Toregas等提出的,用于解决消防中心和救护车等的应急服务设施的选址问题,他们 ...
- 拜占庭将军问题(Byzantine Generals Problem),一个关于分布式系统容错问题故事
拜占庭将军问题(Byzantine Generals Problem),一个关于分布式系统容错问题故事 背景:拜占庭帝国派出10支军队,去包围进攻一个强大的敌人,至少6支军队同时进攻才能攻下敌国. 难 ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- LeetCode第四题,Add Two Numbers
题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。
Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...
- Prime ring problem,递归,广搜,回溯法枚举,很好的题
题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...
随机推荐
- Python 字符串切片
#-*- coding:utf-8 -*- #字符串切片 names = "abcdefgh" ''' 切片语法 names[起始位置:终止位置:步长] 起始位置:即字符串的下标, ...
- 关于Cocos2d-x中数据的存储
当局分数的打印和最高分数的记录 1.首先定义一个Label类型的节点在GameScene.cpp的init方法中,设置初始分数为0 _myScore = 0; scorelabel = Label:: ...
- 第三百一十七节,Django框架,缓存
第三百一十七节,Django框架,缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返 ...
- ST500LT012-1DG142硬盘參数
ATA 设备物理信息 制造商 Seagate 硬盘名称 Momentus Thin 500LT012 形状特征 2.5" 格式化容量 500 GB 盘片数 1 记录面 2 外形尺寸 100 ...
- LabVIEW中数组的自动索引
我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...
- u3d调用c++ dll的DllNotFoundExceion 问题
原文地址:http://blog.csdn.net/boren31/article/details/8778504 问题年年有,今年特别多. 开发环境: Windows XP sp3 Visual ...
- 【转】理清基本的git(github)流程
概述 当我初次接触git时,我需要快速学习基本的git工作流,以便快速接收一个开源Web项目维护.但是,我很难理解工作流程,因为我不太了解git使用关键点. fork,clone,pull.branc ...
- mysqldump备份时,--master-data选项的作用是什么?
需求描述: 今天在研究mysql的备份和恢复,使用mysqldump备份数据库时,用到--master-data选项, 在此,测试并记录选项的作用 测试过程: 1.不使用--master-data进行 ...
- zookeeper 系列文章
http://blog.csdn.net/tswisdom/article/details/41522069 http://blog.csdn.net/tswisdom/article/details ...
- nagios监控mysql
在nagios上部署check_mysql_health 监控mysql 博客分类: 架构 本监控为基于nagios服务器主动监控方法,利用check_mysql_health实现多种监控模式: ...