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 ?
● 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)
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会留在缓冲区
}
}