HDU 5047 Sawtooth (规律+java各种读入输出模板)


Sawtooth

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 377    Accepted Submission(s): 116


Problem Description
Think about a plane:

● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...

Now we have some figure constructed with two parallel rays in the same direction, joined by two straight segments. It looks like a character “M”. You are given N such “M”s. What is the maximum number of regions that these “M”s can divide a plane ?

 

Input
The first line of the input is T (1 ≤ T ≤ 100000), which stands for the number of test cases you need to solve.

Each case contains one single non-negative integer, indicating number of “M”s. (0 ≤ N ≤ 10 12)
 

Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then an integer that is the maximum number of regions N the “M” figures can divide.
 

Sample Input
  
  
2 1 2
 

Sample Output
  
  
Case #1: 2 Case #2: 19

公式:8*n^2 - 7*n + 1

最基本的读入输出:
import java.math.BigInteger;
import java.util.Scanner;

public class Main 
{
	static Scanner in = new Scanner(System.in);
	public static void main(String[] args) 
	{
		int t = in.nextInt();
		int ca = 1;
		while(t-- > 0)
		{
			System.out.print("Case #" + ca++ + ": ");
			BigInteger n = new BigInteger(in.next());
			BigInteger ans = n.multiply(n).multiply(BigInteger.valueOf(8)).subtract(n.multiply(BigInteger.valueOf(7))).add(BigInteger.valueOf(1));
			System.out.println(ans);
		}
	}
}
打印流的输出:
/*
 测试1:速度:最慢1840ms 最快1419ms
 测试2:速度:都1100ms左右
 */
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(new BufferedInputStream(System.in));
		PrintWriter out = new PrintWriter(System.out);
		int t = in.nextInt();
		int ca = 1;
		while(t-- > 0)
		{
			BigInteger n = new BigInteger(in.next());
			BigInteger ans = n.multiply(n).multiply(BigInteger.valueOf(8)).subtract(n.multiply(BigInteger.valueOf(7))).add(BigInteger.valueOf(1));
			out.println("Case #" + ca++ + ": " + ans);
		}
		out.flush();
	}
}
加buff的读入输出
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;

public class Main 
{
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException 
	{
		int t = Integer.parseInt(in.readLine());
		int ca = 1;
		while(t-- > 0)
		{
			BigInteger n = new BigInteger(in.readLine());
			BigInteger ans = n.multiply(n).multiply(BigInteger.valueOf(8)).subtract(n.multiply(BigInteger.valueOf(7))).add(BigInteger.valueOf(1));
			out.write("Case #" + ca++ + ": " + ans);
			out.newLine();
		}
		out.flush();
		out.close();
	}
}

bufferReader比Scanner要快。。。原因是因为,前者是读入到缓冲区中,而后者直接读入到硬盘里。。。


Sacnner:是一个基于正则表达式的文本扫描器,可以从文件、输入流、字符串中解析出基本类型值和字符串值。

BufferedReader是javaIO流中的一个字符串、包装类,它必须建立在另一个字符流的基础之上,但system.in是字节流,需用InputStreamReader将其包装成字符流。

要使用BufferReader输入一些字符之外的类型的数据,就要相对比较麻烦,需要通过一些XXXX.parseXxx();来转换相应的数据类型,虽然,麻烦一些,但通过在一些OJ系统上的和Scanner作对比,BufferReader的效率要比Scanner高一倍,这个差距可想而知,读取的数据越多,效果就越明显。


另一种读入输出模板:
import java.io.*;  
  
public class Main {  
        public static void main(String[] args) throws IOException  
                // in.nextTokenizer可能抛出这个异常  
        {  
                StreamTokenizer in = new StreamTokenizer(new BufferedReader(  
                                        new InputStreamReader(System.in)));  
                // 这句是io流包装来包装去,记住就好;  
                PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));  
                int a, b;  
                while (in.nextToken() != StreamTokenizer.TT_EOF)  
                        // 用in.nextToken()读下一个标记,StreamTokenizer.TT_EOF这个是个参数,就是EOF  
                {  
                        a = (int) in.nval;  
                        // 读进来的是double型的,所以要转成int  
                        in.nextToken();  
                        // 读入b值(a的值是在while循环中读入的)  
                        b = (int) in.nval;  
                        out.println(a + b);  
                }  
                out.flush();  
                // 刷新缓冲区,必须的,不然a+b会留在缓冲区  
        }  
}  






Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表类型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、中小企业板、创业板、科创板等)数据整理计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值