HDOJ 1197 Specialized Four-Digit Numbers
Problem Description
Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.
For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 1*1728 + 8*144 + 9*12 + 3, its duodecimal representation is 1893(12), and these digits also sum up to 21. But in hexadecimal 2991 is BAF16, and 11+10+15 = 36, so 2991 should be rejected by your program.
The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 should be on the listed output. (We don’t want decimal numbers with fewer than four digits - excluding leading zeroes - so that 2992 is the first correct answer.)
Input
There is no input for this problem.
Output
Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below.
Sample Input
There is no input for this problem.
Sample Output
2992
2993
2994
2995
2996
2997
2998
2999
题意:输入一个十进制数,看这个十进制的数的各个位数上的和,和它的12进制和16进制上的各个位数上的和是不是相等,相等就输出。
(十进制的数范围[2992-9999])
public class Main{
public static void main(String[] args) {
for(int i=2992;i<=9999;i++){
int a = i/1000+(i/100)%10+(i/10)%10+i%10;
String str = Integer.toString(i, 12);
//System.out.println(str);
int b = 0;
for(int j=0;j<str.length();j++){
switch(str.charAt(j)){
case 'a':b=b+10;break;
case 'b':b=b+11;break;
default:b=b+(new Integer(str.charAt(j)+""));
}
}
if(a!=b){
continue;
}
int c=0;
str = Integer.toString(i, 16);
//System.out.println(str);
for(int j=0;j<str.length();j++){
switch(str.charAt(j)){
case 'a':c=c+10;break;
case 'b':c=c+11;break;
case 'c':c=c+12;break;
case 'd':c=c+13;break;
case 'e':c=c+14;break;
case 'f':c=c+15;break;
default:c=c+(new Integer(str.charAt(j)+""));
}
}
if(a!=c){
continue;
}
System.out.println(i);
}
}
}
HDOJ 1197 Specialized Four-Digit Numbers的更多相关文章
- 【HDOJ】1197 Specialized Four-Digit Numbers
水题,暴力可解. #include <iostream> using namespace std; int chg(int n, int base); int main() { int i ...
- HDU 1197 Specialized Four-Digit Numbers
Specialized Four-Digit Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1197 Specialized Four-Digit Numbers (枚举+进制转化,简单)
题意:让求从2992-9999中所有数字,满足10进制各位之和和12进制和16进制各位数字之和相等. 析:没啥可说的,只能枚举从2992-9999,每个进制都算一下. 代码如下: #include & ...
- hdoj 2817 A sequence of numbers【快速幂】
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【HDOJ】1061 Rightmost Digit
这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...
- [Swift]LeetCode902. 最大为 N 的数字组合 | Numbers At Most N Given Digit Set
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- 902. Numbers At Most N Given Digit Set
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- LeetCode902. Numbers At Most N Given Digit Set
题目: We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. ...
- [LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
随机推荐
- 字体在Android View中的输出 drawText
Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标. public static class FontMetrics { public flo ...
- 【Hibernate步步为营】--继承映射具体解释
上篇文章讨论了多对多映射,在使用多对多映射时重点是使用<many-to-many>标签,并在标签的两端加入外键这样在生成关系时会创建两个关系之间的关系表,通过关系表来维护它们之间的关系,另 ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- unity3d Find()使用
1. Hierarchy 创建对象如两个cube时,未修改名称,名称都为cube时. js添加至Camera: private var cubeObj : GameObject; //private ...
- [转] Gradle中的buildScript代码块
PS: 在build script中的task apply plugin: 'spring-boot' 需要 classpath("org.springframework.boot:spri ...
- openvpn server部署笔记
openvpn server 部署 1.准备 安装依赖 yum -y install gcc gcc-c++ openssl-devel openssl pam-devel 2.安装 lzo cd / ...
- el和jstl
<%@page import="cn.bdqn.bean.News"%> <%@ page language="java" import=&q ...
- c-整型家族(integer family)
C中,整型有: characters, short integer, integer, long integer 看起来,long integer要比short integer大,但是这也是不一定的. ...
- 升级openssl到1.0.1g
先进行支撑包的安装: # yum install -y zlib openssl升级步骤: 下载最新版本的openssl源码包 # wget ftp://ftp.openssl.org/sourc ...
- JavaScript 类、构造函数、原型
类.构造函数.原型 :本质均为函数 利用的原理是:词法作用域,调用对象及作用域链 闭包 属性查找方式 设计和new运算符一起使用的函数叫做构造函数. 构造函数的工作:初始化一个新创建的对象 ...