第一个java代码:华工校赛D:Lets Play Another Game

本文介绍如何使用Java中的BigInteger类处理高精度问题,包括读取、赋值、数学运算及转换等基本操作,并通过实例展示如何解决二进制与十进制转换时超出整数范围的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java大数(以下为转)

JAVA之BigInteger

Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂。用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的来处理。下面是写的一些Java中一些基本的函数的及其……

头文件:import java.io.*;

import java.util.*;

import java.math.*;

读入: Scanner cin = Scanner (System.in);

while(cin.hasNext())//等价于!=EOF

n=cin.nextInt();//读入一个int型的数

n=cin.nextBigInteger();//读入一个大整数

输出: System.out.print(n);//打印n

System.out.println();//换行

System.out.printf("%d\n",n);//也可以类似c++里的输出方式

定义: int i,j,k,a[];

a = new int[100];

BigInteger n,m;

BigDecimal n;

String s;

数据类型:

数据类型 类型名 位长 取值范围 默认值

布尔型 boolean true,false false

字节型 byte -128-127 0

字符型 char 16 ‘\u000’-\uffff ‘\u0000’

短整型 short 16 -32768-32767 0

整型 int 32 -2147483648,2147483647 0

长整型 long 64 -9.22E18,9.22E18 0

浮点型 float 32 1.4E-45-3.4028E+38 0.0

双精度型 double 64 4.9E-324,1.7977E+308 0.0

这里特别要提出出的两种类型:

BigInteger 任意大的整数,原则上是,只要你的计算机的内存足够大,可以有无限位的

BigInteger 任意大的实数,可以处理小数精度问题。

BigInteger中一些常见的函数:

A=BigInteger.ONE

B=BigInteger.TEN

C=BigInteger.ZERO

一些常见的数的赋初值。将int型的数赋值给BigInteger,BigInteger.valueOf(k);

基本的函数:

valueOf:赋初值

add:+ a.add(b);

subtract:-

multiply:*

divide:/

remainder:this % val

divideAndRemainder:a[0]=this / val; a[1]=this % val

pow:a.pow(b)=a^b

gcd,abs:公约数,绝对值

negate:取负数

signum:符号函数

mod:a.mod(b)=a%b;

shiftLeft:左移,this << n ,this*2^n;

shiftRight:右移,this >> n,this/2^n;

and:等同于c++的&&,且;

or:||,或;

xor:异或,BigInteger xor(BigInteger val),this^val

not:!,非;

bitLength:返回该数的最小二进制补码表示的位的个数,即 *不包括* 符号位 (ceil(log2(this <0 ? -this : this + 1)))。对正数来说,这等价于普通二进制表示的位的个数。

bitCount:返回该数的二进制补码表示中不包扩符号位在内的位的个数。该方法在 BigIntegers 之上实现位向量风格的集合时很有用。

isProbablePrime:如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2**certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。

compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1;

equals:判断两数是否相等,也可以用compareTo来代替;

min,max:取两个数的较小、大者;

intValue,longValue,floatValue,doublue:把该数转换为该类型的数的值。

今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换。都会变成0.
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:

1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。

2,其构造方法有很多,但现在偶用到的有: BigInteger(String val)
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
BigInteger(String val, int radix)
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。
如要将int型的2转换为BigInteger型,要写为BigInteger two=new BigInteger("2"); //注意2双引号不能省略

3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。

4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下:
String toString()
返回此 BigInteger 的十进制字符串表示形式。
输出方法:System.out.print(two.toString());

5,另外说明三个个用到的函数。 BigInteger remainder(BigInteger val)
返回其值为 (this % val) 的 BigInteger。
BigInteger negate()
返回其值是 (-this) 的 BigInteger。
int compareTo(BigInteger val)
将此 BigInteger 与指定的 BigInteger 进行比较。
remainder用来求余数。
negate将操作数变为相反数。
compare的详解如下:

compareTo
public int compareTo(BigInteger val)将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。
指定者:
接口 Comparable<BigInteger> 中的 compareTo
参数:
val - 将此 BigInteger 与之比较的 BigInteger。
返回:

将BigInteger的数转为2进制:

public class TestChange {
public static void main(String[] args) {
System.out.println(change("3",10,2));
}
//num 要转换的数 from源数的进制 to要转换成的进制
private static String change(String num,int from, int to){
return new java.math.BigInteger(num, from).toString(to);
}
}


Problem D: Lets Play Another Game

Description

OK, if you finished previous game, lets play another game :D

Now n participants of this game sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would become right, and right - left).

Input

The first line is the amount of tests. Each next line contains one integer n (1 <= n <= 1e18) - the amount of participants.

Output

For each number n of participants to game print on the standard output, on a separate line, the minimum time required for all participants to sit in reverse order.

Sample Input

3456

Sample Output

246


这题通过找规律得出公式,重点在于大数

具体代码:

import java.math.BigInteger;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Administrator
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        
        Scanner sb=new Scanner(System.in);
        int test;
        test=sb.nextInt();
        
        BigInteger one=BigInteger.ONE;
            BigInteger zero=one.subtract(one);
        BigInteger two=one.add(one);
          BigInteger three=two.add(one);
             BigInteger four=three.add(one);
        while(test-->0)
        {
          BigInteger n=sb.nextBigInteger();
          BigInteger sum;
          if(n.mod(two).compareTo(one)==0)
          {
              BigInteger xiang=n.subtract(one).divide(two);
              sum=xiang.add(xiang.multiply(xiang.subtract(one)));
              
        }
          else    
          {            
               BigInteger xiang=n.subtract(two).divide(two);
               if(xiang.mod(two).compareTo(zero)==0)
               {
                   BigInteger sbb=xiang.divide(two);
                       // System.out.println(sbb);
                   sum=sbb.multiply(three).add(sbb.multiply(sbb.subtract(one).multiply(two)));
                        //.out.println(sum);
               sum=sum.multiply(two);
                   // System.out.println(sum);
               }
               else
               {
                   BigInteger sbb=xiang.divide(two);
                    // System.out.println(sbb);
                   sum=sbb.multiply(three).add(sbb.multiply(sbb.subtract(one).multiply(two)));
                    // System.out.println(sum);
                             sum=sum.multiply(two);
                         //      System.out.println(sum);
                             BigInteger kk=xiang.divide(two).multiply(four).add(three);
                           //    System.out.println(kk);
                             sum=sum.add(kk).subtract(one);    
                           //    System.out.println(sum);
               }
            }
          System.out.println(sum);
               
          }
        
      
      
    }
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值