HDOJ(HDU) 2106 decimal system(进制相互转换问题)
Problem Description
As we know , we always use the decimal system in our common life, even using the computer. If we want to calculate the value that 3 plus 9, we just import 3 and 9.after calculation of computer, we will get the result of 12.
But after learning <>,we know that the computer will do the calculation as the following steps:
1 computer change the 3 into binary formality like 11;
2 computer change the 9 into binary formality like 1001;
3 computer plus the two number and get the result 1100;
4 computer change the result into decimal formality like 12;
5 computer export the result;
In the computer system there are other formalities to deal with the number such as hexadecimal. Now I will give several number with a kind of change method, for example, if I give you 1011(2), it means 1011 is a number in the binary system, and 123(10) means 123 if a number in the decimal system. Now I will give you some numbers with any kind of system, you guys should tell me the sum of the number in the decimal system.
Input
There will be several cases. The first line of each case contains one integers N, and N means there will be N numbers to import, then there will be N numbers at the next N lines, each line contains a number with such form : X1….Xn.(Y), and 0<=Xi
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
long sum=0;
int n =sc.nextInt();
String str = null;
for(int i=0;i<n;i++){
str =sc.next();
String strs[] = str.split("\\(");
int num = Integer.parseInt( strs[0]);
String strHex = "";
for(int j=0;j<strs[1].length();j++){
if(strs[1].charAt(j)!=')'){
strHex = strHex+strs[1].charAt(j);
}
}
int hex = Integer.parseInt(strHex);
if(hex==10){
sum+=num;
}else{
//hex为num这个数的进制基数。也就是说num为hex进制的数。
//返回的是一个十进制的数。。
num = Integer.parseInt(Integer.toString(num), hex);
sum+=num;
}
}
System.out.println(sum);
}
}
}
HDOJ(HDU) 2106 decimal system(进制相互转换问题)的更多相关文章
- HDU 2106 decimal system (进制转化求和)
题意:给你n个r进制数,让你求和. 析:思路就是先转化成十进制,再加和. 代码如下: #include <iostream> #include <cstdio> #includ ...
- HDOJ(HDU) 2097 Sky数(进制)
Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=2 ...
- HDOJ(HDU) 2502 月之数(进制)
Problem Description 当寒月还在读大一的时候,他在一本武林秘籍中(据后来考证,估计是计算机基础,狂汗-ing),发现了神奇的二进制数. 如果一个正整数m表示成二进制,它的位数为n(不 ...
- hdu 2106 decimal system
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> # ...
- php实现文件与16进制相互转换
php实现文件与16进制相互转换 <pre><?php/** * php 文件与16进制相互转换 * Date: 2017-01-14 * Author: fdipzone * Ve ...
- Python语言中各种进制相互转换
目录 Python语言中各种进制相互转换 将二进制.八进制.十进制的数分别转换成十进制的方法 将十进制转换成二进制.八进制.十六进制 Python语言中各种进制相互转换 本文参考自https://ww ...
- java中 8进制 10进制 2进制 16进制 相互转换
十进制转成十六进制: Integer.toHexString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成二进制 Integer.toBinar ...
- HDU 3001 Travelling 3进制状压dp
题意:10个点,若干条边,边有花费,每个点最多走两次,求走过所有点,花费最少 分析:因为每个点最多走两次,所以联想到3进制,然后枚举状态,就行了(我也是照着网上大神的代码写的) #include &l ...
- sql 进制转换,支持93内的进制相互转换
功能:实现在SQL内进制的互相转换,支持从2 - 93进制内的转换,若需要支持其他字符,可以自定义@ym变量实现扩充 -- ====================================== ...
随机推荐
- 论前端css初始化的重要性
新手,求喷,刚刚知道每个浏览器都有对 标签的初始化,就造成我们网站开发者开发的web程序,会在不同的网站上有不同的样式风格,这给用户带来了很不好的体验,这也是浏览器本身的原因造成的,这时候,我们不可能 ...
- 实训第二天早上--hibernate之配置文件映射和注解
hibernate 逐步优化第一步 只按照步骤来提取的jre包导入错误第二步 继续封装,把增删改查提取出来,同时进行代码的封装HQL语句 be stranger in the code .be fo ...
- sql语句中like的使用
先看一道题: 写出一条sql语句,找出表B中 字段Value中不全是字母 数字 下划线的数据 初看这道题,我们想到可以用like去进行模糊匹配,找出想要的结果.但是有一个地方需要注意:如果想在SQL ...
- For and While loop choice.
/* Difference between 'for' and 'while'. We can transform everything between 'for' and 'while'. if t ...
- [转帖]vivado & VS2013工具
来源:http://bbs.csdn.net/topics/380057699 添加OpenCV库后,MFC在Debug模式下调试,提示应用程序无法正常启动(0xc000007b). 解决方法:在环境 ...
- OpenGL光源位置
一.OpenGL光源简介 OpenGL提供了多种形式的光源,如点光源.平行光源和聚光灯光源等.所有光源都使用 glLight*接口来设置光源属性,其中包括 glLight{if} 和 glLight{ ...
- Spring(一)简述(转载)
原文出自:http://www.cnblogs.com/liunanjava/p/4396794.html 一.Spring简述 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量 ...
- Ubuntu14.04安装AMD显卡驱动双屏显示器完全解决方案
网上有很多方法,但是针对AMD显卡的方案不多,所以笔者今天想写一篇关于AMD显卡的教程. 首先,进入这个网址下载一些东东:http://support.amd.com/zh-cn/download/d ...
- absolut绝对定位的非绝对定位用法
一.absolute绝对定位的流行用法 一般而言,我们会用absolute绝对定位做什么呢?就是绝对定位,顾名思意,定死在某个位置上.例如,lightbox效果就是使用的绝对定位,例如新浪微博的弹出提 ...
- Grnymotion模拟器和Android真机访问PC端Tomcat下的应用
最近因为要学安卓与服务器交互的知识,所以必须要让android程序能访问一个测试服务器.所以我就考虑让真机或者模拟器访问PC端的Tomcat或者Apache服务. 在介绍步骤之前,有必要说点基础的.我 ...