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获得屏幕高度和宽度
获取屏幕的宽度与高度有以下几种方法: .WindowManager wm = (WindowManager) getContext() .getSystemSe ...
- C++获得系统路径
源码: [cpp] view plaincopy #include <Shlobj.h> #include <stdio.h> #include <locale.h> ...
- Robotium--通过Id寻找控件
在自动化测试中,UI上经常有一些控件是没有名称的,那么此时,就可以通过id来找到这些控件. 案例:对两个EditText进行测试 package com.tangbc.tedit.test; impo ...
- [Redux] Avoiding Object Mutations with Object.assign() and ...spread
Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects. ...
- Android 使用 RemoteViews 发送自定义通知 ,遇到 Couldn't expand RemoteViews问题总结
在RemoteViews这种调用方式中,你只能使用以下几种界面组件: Layout: FrameLayout, LinearLayout, RelativeLayout Component: ...
- Linux控制台下的快捷键
Linux控制台(文本模式)下提高工作效率的快捷键 在Linux环境里,有一些按键有特殊的含意.# Ctrl-U: 擦除一行光标前面的部分.# Ctrl-H: 擦除光标前面的一个字符.# Ctrl-D ...
- SQL 使用Cursor(游标)遍历结果集
使用Cursor(游标)可以在存储过程中遍历select 结果集,对其进行相关的操作. Cursor(游标)语法格式 DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段 ...
- 第一次启动MySQL时报错
[root@localhost~]#/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mys ...
- iOS中如何使状态栏与下面的搜索栏或NavigationBar或toolBar颜色一致
在iOS7之后,status bar是透明的(transparent),navigation bars,tab bars,toolbars,search bars 和 scope bars 是半透明的 ...
- UIGestureRecognizer手势
常用手势: 滑动,轻点,捏合,旋转,拖拽,长按 1.滑动(快速滑动) let swipeUp = UISwipeGestureRecognizer(target: self, action: Sele ...