POJ Octal Fractions(JAVA水过)

题目链接:CLICK HERE~

虽然java一下模拟水过,但是我看到别人的一段神奇代码,贴出和大家共享。

import java.math.*;
import java.util.*;

class Main{
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		BigDecimal Eight = new BigDecimal(8);
		while(cin.hasNext()){
			String num;
			num = cin.nextLine();
			BigDecimal ans = new BigDecimal(0);
			BigDecimal tmp = new BigDecimal(1);
			for(int i = 2;i < num.length();++i){
				tmp = tmp.divide(Eight);
				ans = ans.add(new BigDecimal(num.charAt(i) - '0').multiply(tmp));
			}
			System.out.println(num + " [8] = " + ans + " [10]");
		}
	}
}

这个算法是将除法转变为乘法,对于0.d1d2d3 ... dk [8],本来是d1/8+d2/(8^2)+...+dk/(8^k),这个算法将 dn/8转变为 dn * 125,然后不断让 y 乘以 1000,起到保存小数位数的作用,最后的结果就相当于是 对dn每次乘以0.125。

#include<cstdio>

#include<cstring>

char c[50];

int i,l;

double x,y;

int main()

{

    while(scanf("%s",c)!=EOF)

    {

        printf("%s [8] = ",c);

        l=strlen(c)-1;

        x=0;

        y=1;

        for(i=l;i>1;i--)

        {

            x=((c[i]-'0')*y+x)*125;

            y*=1000;

        }

        x/=y;

        i=0;

        while(x)

        {

            c[i]=int(x*=10)%10+'0';

            x-=c[i]-'0';i++;

        }

        c[i]='\0';

        printf("0.%s [10]\n",c);

    }

}



POJ Octal Fractions(JAVA水过),布布扣,bubuko.com

时间: 2024-10-27 13:13:20

POJ Octal Fractions(JAVA水过)的相关文章

POJ 1131 Octal Fractions (Java大数,八进制转十进制)

Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6959   Accepted: 3825 Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0.75 in octal is 0.953125 (7/8 + 5/64) in d

Octal Fractions java秒 C++

Octal Fractions 题目抽象:   将八进制小数转换成十进制小树.小数的为数很大. 可以用java  中的BigDeciaml 秒掉.  time:297ms 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 import java.text.*; 5 6 public class Main 7 { 8 static int MS=3005; 9 public static void main(Stri

poj 2369 Permutations 置换水题

找循环节求lcm就够了,若答案是12345应该输出1,被坑了下. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; #define INF 0x3FFFFFF #define MAXN 2222 #define eps 1e-6 i

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

poj 1002:487-3279(水题,提高题 / hash)

487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 236746   Accepted: 41288 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phras

POJ 1488 Tex Quotes --- 水题

POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 Tex Quotes --- 水题 */ #include <cstdio> #include <cstring> int main() { #ifdef _LOCAL freopen("D:\\input.txt", "r", stdin); #

JAVA 水题

纯粹是让我来掌握熟练度的. 1.金蝉素数 某古寺的一块石碑上依稀刻有一些神秘的自然数. 专家研究发现:这些数是由1,3,5,7,9 这5 个奇数字排列组成的5 位素数,且同时去掉它的最高位与最低位数字后的三位数还是素数,同时去掉它的高二位与低二位数字后的一位数还是素数.因此,人们把这些神秘的素数称为金蝉素数,喻意金蝉脱壳之后仍为美丽的金蝉. 输出所有金蝉素数. ANSWER: 13597 53791 79531 91573 95713 package test; import java.util

poj 1003:Hangover(水题,数学模拟)

Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 99450   Accepted: 48213 Description How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're as

POJ 3982 序列(JAVA,简单,大数)

题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger a99(BigInteger a, BigInteger b, BigInteger c) { f