大数问题,通常用JAVA

e.g.

HDU1002

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

public class Main {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int t=cin.nextInt();
        cin.nextLine();
        int cnt=0;
        while(t!=0)
        {
            t--;
            String s1=cin.next();
            String s2=cin.next();
            BigInteger a1=new BigInteger(s1);
            BigInteger a2=new BigInteger(s2);
            System.out.println("Case "+(++cnt)+":");
            System.out.print(a1+" + "+a2+" = ");
            System.out.println(a1.add(a2));
            if(t!=0)
                System.out.println();
        }

    }
}

HDU1715 大斐波那契数

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

public class Main {
    private static BigInteger[] fib;
    public static void f(){
        fib=new BigInteger[1005];
        fib[2]=new BigInteger("1");
        fib[1]=fib[2];
        for(int i=3;i<1005;i++)
        {
            fib[i]=fib[i-1].add(fib[i-2]);
        }
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int t=cin.nextInt();
        cin.nextLine();
        int cnt=0;
        f();
        while(t!=0)
        {
            t--;
            cnt=cin.nextInt();
            System.out.println(fib[cnt]);
        }

    }
}

HDU1865

分析(转):

简单递推+大数。
    f[n]=f[n-1]+f[n-2]。

why?
    由于数字只有‘1‘和‘2‘这两种,那么当让第n
 位为1的时候,f[n]加上f[n-1];当让最后两位
 合并成2的时候,f[n]加上f[n-2](因为要保证
 倒数第二位一定是1)。

算出来的f就是一个斐波那契数列。

WOW ,问题转化是极为巧妙的

  这种递推关系,说的严重点就是动态规划

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

public class Main {
    private static BigInteger[] fib;
    public static void f(){
        fib=new BigInteger[1005];
        fib[1]=new BigInteger("1");
        fib[0]=fib[1];
        for(int i=2;i<1005;i++)
        {
            fib[i]=fib[i-1].add(fib[i-2]);
        }
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int t=cin.nextInt();
        cin.nextLine();
        int cnt=0;
        f();
        while(t!=0)
        {
            t--;
            String s=cin.next();
            cnt=s.length();
            System.out.println(fib[cnt]);
        }

    }
}
时间: 2024-08-02 12:03:11

大数问题,通常用JAVA的相关文章

HBase的常用Java API

1. 创建HBase表的对象 HBase表的对项名字叫HTable,创建它的方法有很多,常见的有如下: org.apache.hadoop.hbase.client.HTable hTable = new HTable(org.apache.hadoop.hbase.HBaseConfiguration conf, String tableName); 或 org.apache.hadoop.hbase.client.HTable hTable = new HTable(org.apache.h

常用 Java API

常用Java API 一. java.io.BufferedReader类(用于从文件中读入一段字符:所属套件:java.io) 1. 构造函数BufferedReader(java.io.FileReader FileReader变量) 说明:新建一个BufferReader对象. 2. close方法 void close() 说明:关闭BufferReader对象. 3. readLine方法 java.lang.string readLine() 说明:从文件中读取一行字符.若为NULL

常用Java片段

1. 字符串与整型的相互转换 String a = String.valueOf(2);   //integer to numeric string   int i = Integer.parseInt(a); //numeric string to an int  2,得到当前方法的名字 public class MethodName {     private void IamAMethod(){         String methodName = Thread.currentThrea

【转载】常用 Java 静态代码分析工具的分析与比较

摘自:http://www.oschina.net/question/129540_23043常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBugs,PMD,Jtest),最后从功能.特性等方面对它们进行分析和比较,希望能够帮助 Java 软件开发人员了解静态代码分析工具,并选择合适的工具应用到软件开发中. 引言 在 Java 软件开发过程中,开发团

utuntu16.04安装常用Java开发、娱乐软件以及遇见的问题汇总

先上图吧 一.个人比较喜欢这个界面还有主题,更好看的也有,但是没去找,能看就行,基本主要的软件都在左侧栏当中了,下面将一一总结: 1.Ubuntu,这是官网下载的系统,本人台式机,amd显卡,装机过程中没遇见任何问题,直接一次成功,参考链接是: 1.安装完之后遇见了一个是关机后自动重启的问题,这个是Ubuntu自带的电管管理模块太过于简化,很多机型都会出现这个问题,解决方法很简单,自己安装一个电源管理模块,参考链接是: 2.如果是对Ubuntu系统不了解的话,建议先到网上查看一下这个系统的介绍,

[0014] HDFS 常用JAVA 操作实战

目的: 学习用java进行的常用hdfs操作 参考: [0002] Hadoop HDFS cmd常用命令练手 环境: hadoop2.6.4 win7 下的eclipse环境调试已经配置好,参考前面的文章 代码: 1. 创建文件夹 1 package hdfs; 2 3 import java.io.IOException; 4 5 import org.apache.hadoop.conf.Configuration; 6 import org.apache.hadoop.fs.FileSy

properties文件简介及其常用Java操作

一.properties文件简介 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"(推荐)或"键:值"的格式,在properties文件中,可以用"#"(推荐)或者"//"来作注释,properties文件在Java编程中用到的地方很多,操作很方便. 现在定义一个databaseInfo.properties文件,如下:#

51 Nod 1028 大数乘法 V2【Java大数乱搞】

1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 100000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemI

NYOJ 大数类总结(java)

棋盘覆盖 题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45 样例输入 3 1 2 3 样例输出 1 5 21 代码如下: import java.io.*; import java.util.*; import java.math.BigDecimal; import java.math.BigInteger;//声明BigInteger大数类 public class Main { public static void main(