acm java输入输出_在竞赛ACM Java处理输入输出
一、Java之ACM注意點(diǎn)
1.?類名稱必須采用public?class?Main方式命名
2.?在有些OJ系統(tǒng)上,即便是輸出的末尾多了一個“?”,程序可能會輸出錯誤,所以在我看來好多OJ系統(tǒng)做的是非常之垃圾
3.?有些OJ上的題目會直接將OI上的題目拷貝過來,所以即便是題目中有輸入和輸出文件,可能也不需要,因?yàn)樵贠J系統(tǒng)中一般是采用標(biāo)準(zhǔn)輸入輸出,不需要文件
4.?在有多行數(shù)據(jù)輸入的情況下,一般這樣處理,
static?Scanner?in?=?new?Scanner(System.in);
while(in.hasNextInt())
或者是
while(in.hasNext())
5.?有關(guān)System.nanoTime()函數(shù)的使用,該函數(shù)用來返回最準(zhǔn)確的可用系統(tǒng)計(jì)時器的當(dāng)前值,以毫微秒為單位。
long?startTime?=?System.nanoTime();
//?...?the?code?being?measured?...
long?estimatedTime?=?System.nanoTime()?-?startTime;
二、Java之輸入輸出處理
由于ACM競賽題目的輸入數(shù)據(jù)和輸出數(shù)據(jù)一般有多組(不定),并且格式多種多樣,所以,如何處理題目的輸入輸出是對大家的一項(xiàng)最基本的要求。這也是困擾初學(xué)者的一大問題。
1.?輸入:
格式1:Scanner?sc?=?new?Scanner?(new?BufferedInputStream(System.in));
格式2:Scanner?sc?=?new?Scanner?(System.in);
在讀入數(shù)據(jù)量大的情況下,格式1的速度會快些。
讀一個整數(shù):?int?n?=?sc.nextInt();?相當(dāng)于?scanf("%d",?&n);?或?cin?>>?n;
讀一個字符串:String?s?=?sc.next();?相當(dāng)于?scanf("%s",?s);?或?cin?>>?s;
讀一個浮點(diǎn)數(shù):double?t?=?sc.nextDouble();?相當(dāng)于?scanf("%lf",?&t);?或?cin?>>?t;
讀一整行:?String?s?=?sc.nextLine();?相當(dāng)于?gets(s);?或?cin.getline(...);
判斷是否有下一個輸入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()
例1:讀入整數(shù)
Input??輸入數(shù)據(jù)有多組,每組占一行,由一個整數(shù)組成。
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()){??//判斷是否結(jié)束
int?score?=?sc.nextInt();//讀入整數(shù)
。。。。
}
}
}
例2:讀入實(shí)數(shù)
輸入數(shù)據(jù)有多組,每組占2行,第一行為一個整數(shù)N,指示第二行包含N個實(shí)數(shù)。
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
double?a?=?sc.nextDouble();
。。。。。。
}
}
}
}
例3:讀入字符串【杭電2017?字符串統(tǒng)計(jì)】
輸入數(shù)據(jù)有多行,第一行是一個整數(shù)n,表示測試實(shí)例的個數(shù),后面跟著n行,每行包括一個由字母和數(shù)字組成的字符串。
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
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
String?str?=?sc.nextLine();
......
}
}
}
例3:讀入字符串【杭電2005?第幾天?】
給定一個日期,輸出這個日期是該年的第幾天。
Input??輸入數(shù)據(jù)有多組,每組占一行,數(shù)據(jù)格式為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
days?+=?dd[i];
}
System.out.println(days);
}
}
}
2.?輸出
函數(shù):
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
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
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.?規(guī)格化的輸出:
函數(shù):
//?這里0指一位數(shù)字,#指除0以外的數(shù)字(如果是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方法來取出其中某一字節(jié),計(jì)數(shù)從0開始:
String?a?=?"Hello";?//?a.charAt(1)?=?'e'
用substring方法可得到子串,如上例
System.out.println(a.substring(0,?4))?//?output?"Hell"
注意第2個參數(shù)位置上的字符不包括進(jìn)來。這樣做使得?s.substring(a,?b)?總是有?b-a個字符。
字符串連接可以直接用?+?號,如
String?a?=?"Hello";
String?b?=?"world";
System.out.println(a?+?",?"?+?b?+?"!");?//?output?"Hello,?world!"
如想直接將字符串中的某字節(jié)改變,可以使用另外的StringBuffer類。
5.?高精度
BigInteger和BigDecimal可以說是acmer選擇java的首要原因。
函數(shù):add,?subtract,?divide,?mod,?compareTo等,其中加減乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之間的運(yùn)算,所以需要把int(double)類型轉(zhuǎn)換為BigInteger(BigDecimal),用函數(shù)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.?進(jìn)制轉(zhuǎn)換
String?st?=?Integer.toString(num,?base);?//?把num當(dāng)做10進(jìn)制的數(shù)轉(zhuǎn)成base進(jìn)制的st(base?<=?35).
int?num?=?Integer.parseInt(st,?base);?//?把st當(dāng)做base進(jìn)制,轉(zhuǎn)成10進(jìn)制的int(parseInt有兩個參數(shù),第一個為要轉(zhuǎn)的字符串,第二個為說明是什么進(jìn)制).
BigInter?m?=?new?BigInteger(st,?base);?//?st是字符串,base是st的進(jìn)制.
7.?數(shù)組排序
函數(shù):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?
Arrays.sort(a);
for?(int?i?=?0;?i?
}
}
總結(jié)
以上是生活随笔為你收集整理的acm java输入输出_在竞赛ACM Java处理输入输出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python程序入门设计_程序设计入门—
- 下一篇: java zip malformed_关