ACM之Java输入输出[通俗易懂]

ACM之Java输入输出[通俗易懂]一、Java之ACM注意点1. 类名称必须采用public class Main方式命名2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非常之垃圾3. 有些OJ上的题目会直接将OI上的题目拷贝过来,所以即便是题目中有输入和输出文件,可能也不需要,因为在OJ系统中一般是采用标准输入输出,不需要文件4. 在有多行数据输入的情况下

大家好,又见面了,我是你们的朋友全栈君。

一、Java之ACM注意点

1. 类名称必须采用public class Main方式命名

2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非常之垃圾

3. 有些OJ上的题目会直接将OI上的题目拷贝过来,所以即便是题目中有输入和输出文件,可能也不需要,因为在OJ系统中一般是采用标准输入输出,不需要文件

4. 在有多行数据输入的情况下,一般这样处理,

static Scanner in = new Scanner(System.in);
while(in.hasNextInt())
或者是
while(in.hasNext())

5. 有关System.nanoTime()
函数的使用,该函数用来返回最准确的可用系统计时器的当前值,以毫微秒为单位。

 

   long startTime = System.nanoTime();
   // ... the code being measured ...
   long estimatedTime = System.nanoTime() - startTime;

二、Java之输入输出处理

由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。

1. 输入:

格式1:Scanner sc = new Scanner (new BufferedInputStream(System.in));

格式2:Scanner sc = new Scanner (System.in);

在读入数据量大的情况下,格式1的速度会快些。

读一个整数: int n = sc.nextInt(); 相当于 scanf(“%d”, &n); 或 cin >> n; 

读一个字符串:String s = sc.next(); 相当于 scanf(“%s”, s); 或 cin >> s; 

读一个浮点数:double t = sc.nextDouble(); 相当于 scanf(“%lf”, &t); 或 cin >> t; 

读一整行: String s = sc.nextLine(); 相当于 gets(s); 或 cin.getline(…); 

判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

例1:读入整数

Input  输入数据有多组,每组占一行,由一个整数组成。 
Sample Input 
56
67
100
123 
 
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
while(sc.hasNext()){  //判断是否结束
int score = sc.nextInt();//读入整数
。。。。
}
}
}
 

例2:读入实数

 

输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数。

Sample Input
4 
56.9  67.7  90.5  12.8 
5 
56.9  67.7  90.5  12.8 
 
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
for(int i=0;i<n;i++){
double a = sc.nextDouble();
。。。。。。
}
}
}
}
 

例3:读入字符串【杭电2017 字符串统计】

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Sample Input  
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
 
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
String str = sc.next();
......
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++){
String str = sc.nextLine();
......
}
}
}
 

例3:读入字符串【杭电2005 第几天?】

给定一个日期,输出这个日期是该年的第几天。 
Input  输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成
1985/1/20
2006/3/12
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};
while(sc.hasNext()){
int days = 0;
String str = sc.nextLine();
String[] date = str.split("/");
int y = Integer.parseInt(date[0]);
int m = Integer.parseInt(date[1]);
int d = Integer.parseInt(date[2]);
if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;
days += d;
for(int i=0;i<m;i++){
days += dd[i];
}
System.out.println(days);
}
}
}

 

2. 输出  

函数:

System.out.print(); 

System.out.println(); 

System.out.format();

System.out.printf();  

 

例4 杭电1170Balloon Comes!

Give you an operator (+,-,*, / –denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result. 

Input

Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator. 

Output

For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.

Sample Input

4

+ 1 2

– 1 2

* 1 2

/ 1 2

Sample Output

3

-1

2

0.50

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
String op = sc.next();
int a = sc.nextInt();
int b = sc.nextInt();
if(op.charAt(0)=='+'){
System.out.println(a+b);
}else if(op.charAt(0)=='-'){
System.out.println(a-b);
}else if(op.charAt(0)=='*'){
System.out.println(a*b);
}else if(op.charAt(0)=='/'){
if(a % b == 0) System.out.println(a / b);
else System.out.format("%.2f", (a / (1.0*b))). Println();
}
}
}
}

3. 规格化的输出:
函数:
// 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.
    DecimalFormat fd = new DecimalFormat(“#.00#”);
    DecimalFormat gd = new DecimalFormat(“0.000”);
    System.out.println(“x =” + fd.format(x));
    System.out.println(“x =” + gd.format(x));

public static void main(String[] args) {
    NumberFormat   formatter   =   new   DecimalFormat( "000000"); 
        String  s  =   formatter.format(-1234.567);     //   -001235 
        System.out.println(s);
        formatter   =   new   DecimalFormat( "##"); 
        s   =   formatter.format(-1234.567);             //   -1235 
        System.out.println(s);
        s   =   formatter.format(0);                      //   0 
        System.out.println(s);
        formatter   =   new   DecimalFormat( "##00"); 
        s   =   formatter.format(0);                     //   00 
        System.out.println(s);
 
        formatter   =   new   DecimalFormat( ".00"); 
        s   =   formatter.format(-.567);               //   -.57 
        System.out.println(s);
        formatter   =   new   DecimalFormat( "0.00"); 
        s   =   formatter.format(-.567);              //   -0.57 
        System.out.println(s);
        formatter   =   new   DecimalFormat( "#.#"); 
        s   =   formatter.format(-1234.567);         //   -1234.6 
        System.out.println(s);
        formatter   =   new   DecimalFormat( "#.######"); 
        s   =   formatter.format(-1234.567);        //   -1234.567 
        System.out.println(s);
        formatter   =   new   DecimalFormat( ".######"); 
        s   =   formatter.format(-1234.567);       //   -1234.567 
        System.out.println(s);
        formatter   =   new   DecimalFormat( "#.000000"); 
        s   =   formatter.format(-1234.567);      //   -1234.567000 
        System.out.println(s);
        
        formatter   =   new   DecimalFormat( "#,###,###"); 
        s   =   formatter.format(-1234.567);      //   -1,235 
        System.out.println(s);
        s   =   formatter.format(-1234567.890);  //   -1,234,568 
        System.out.println(s);
 
        //   The   ;   symbol   is   used   to   specify   an   alternate   pattern   for   negative   values 
        formatter   =   new   DecimalFormat( "#;(#) "); 
        s   =   formatter.format(-1234.567);     //   (1235) 
        System.out.println(s);
 
        //   The   '   symbol   is   used   to   quote   literal   symbols 
        formatter   =   new   DecimalFormat( " '# '# "); 
        s   =   formatter.format(-1234.567);        //   -#1235 
        System.out.println(s);
        formatter   =   new   DecimalFormat( " 'abc '# "); 
        s   =   formatter.format(-1234.567);      // - abc 1235
        System.out.println(s);
 
formatter   =   new   DecimalFormat( "#.##%"); 
        s   =   formatter.format(-12.5678987);  
        System.out.println(s);
}

4. 字符串处理 String

String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始: 

String a = “Hello”; // a.charAt(1) = ‘e’ 

用substring方法可得到子串,如上例 

System.out.println(a.substring(0, 4)) // output “Hell” 

注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。 

字符串连接可以直接用 + 号,如 

String a = “Hello”; 

String b = “world”; 

System.out.println(a + “, ” + b + “!”); // output “Hello, world!” 

如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。 

5. 高精度
BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args)   {
Scanner cin = new Scanner (new BufferedInputStream(System.in));
        int a = 123, b = 456, c = 7890;
        BigInteger x, y, z, ans;
        x = BigInteger.valueOf(a); 
        y = BigInteger.valueOf(b); 
        z = BigInteger.valueOf(c);
        ans = x.add(y); System.out.println(ans);
        ans = z.divide(y); System.out.println(ans);
        ans = x.mod(z); System.out.println(ans);
        if (ans.compareTo(x) == 0) System.out.println("1");
    }
}


6. 进制转换
String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,第二个为说明是什么进制).  
BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.
7. 数组排序
函数:Arrays.sort();

public class Main {
public static void main(String[] args)    {
        Scanner cin = new Scanner (new BufferedInputStream(System.in));
        int n = cin.nextInt();
        int a[] = new int [n];
        for (int i = 0; i < n; i++) a[i] = cin.nextInt();
        Arrays.sort(a);
        for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141265.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • zencart的html文件,zencart模板 哪儿有zencart免费模版?

    zencart的html文件,zencart模板 哪儿有zencart免费模版?才接触zencart,但是代码,css+div都懂,毕竟自己不是美工。现在有个B2教你一个方法,把模板down下来,然后先通过CSS+div修改成适合zencart的标签。哪里有漂亮的zencart模板?免费的如果作者只是玩玩,建议你去zencart国内论坛的模板下载区看看如果是商用,免费模板一般都是拿来作为基础模板进行修改的。哪儿有zencart免费模版?zencart模板里,如何实现在商…

    2022年7月27日
  • MQTT服务器搭建和测试[通俗易懂]

    MQTT服务器搭建和测试[通俗易懂]目前MQTT代理的主流平台有下面几个:Mosquitto:https://mosquitto.org/ VerneMQ:https://vernemq.com/ EMQTT:http://emqtt.io/本文将使用Mosquitoo进行测试,进入到安装页面,下载自己电脑的系统所适配的程序注意:安装的目录最好不要带有空格测试第一步:启动brokerwindows下使用命令提示符,进入mosquitto安装目录输入命令:mosquitto-cmosquitto.con

    2022年6月3日
  • 彻底明白Activity启动模式-SingleTop、SingleTask、SingleInstance具体使用场景[通俗易懂]

    彻底明白Activity启动模式-SingleTop、SingleTask、SingleInstance具体使用场景[通俗易懂]启动模式启动模式是什么有这样的场景:当我们使用App的时候,呈现出一个Activity,按下返回键(不考虑重写返回键事件),常常就回退到上一个打开的Activity或者退出App。//重写返回按键事件publicbooleanonKeyDown(intkeyCode,KeyEventevent){if(keyCode==KeyEvent.KEYCODE_BA

    2022年6月26日
  • 验证市场可行性(PMF)的5个步骤[通俗易懂]

    验证市场可行性(PMF)的5个步骤[通俗易懂]在增长黑客的理念中,一切的“猜想”和“创意”都是需要经过验证的,用事实来证明猜想和创意是否可行,这其实也是增长黑客的特质之一,将所有不可量化的东西转化为可量化的评估标准。比如如何证明你的创意能够成功呢?验证PMF的其中一个标准是调研你的用户,如果40%的核心用户认为缺了你的产品会很遗憾,而不是可有可无,那么这就说明找到了P/MF;PMF到底是什么呢?你可以理解为一个指标,例如40%的用户认为没…

    2022年5月23日
  • RabbitMQ入门:路由(Routing)

    在上一篇博客《RabbitMQ入门:发布/订阅(Publish/Subscribe)》中,我们认识了fanout类型的exchange,它是一种通过广播方式发送消息的路由器,所有和exchange建立

    2022年2月16日
  • Opencv cvCircle()函数[通俗易懂]

    Opencv cvCircle()函数[通俗易懂]Circle,即圆形。cvCircle是指绘制圆形的一个程序函数。

    2022年7月24日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号