日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

蓝桥杯Java输入输出相关

發(fā)布時(shí)間:2025/5/22 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 蓝桥杯Java输入输出相关 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載自:http://blog.csdn.net/Chen_Tongsheng/article/details/53354169

一、注意點(diǎn)? ?

1. 類名稱必須采用public class Main方式命名

2. 在有些OJ系統(tǒng)上,即便是輸出的末尾多了一個(gè)“ ”,程序可能會(huì)輸出錯(cuò)誤

3. 有些OJ上的題目會(huì)直接將OI上的題目拷貝過來,所以即便是題目中有輸入和輸出文件,可能也不需要,因?yàn)樵贠J系統(tǒng)中一般是采用標(biāo)準(zhǔn)輸入輸出,不需要文件

4. 在有多行數(shù)據(jù)輸入的情況下,一般按以下代碼示例處理:

import java.util.Scanner; import java.io.*; public class Main{ public static void main(String[] args){ Scanner in1 = new Scanner(System.in); Scanner in2 = new Scanner(new BufferedInputStream(System.in));//輸入速度較前者 in1 要快,但但一般不用這個(gè)BufferedInputStream緩存 //輸入多組測試數(shù)據(jù)一般用:while(in1.hasNextInt()) 或者是 while(in1.hasNext()) } } 5.?有關(guān)System.nanoTime()函數(shù)的使用,該函數(shù)用來返回最準(zhǔn)確的可用系統(tǒng)計(jì)時(shí)器的當(dāng)前值,以毫微秒為單位。 long startTime = System.nanoTime(); // Code... 運(yùn)用算法來解決問題的運(yùn)行代碼 long estimatedTime = System.nanoTime() - startTime;

?

二、輸入輸出處理

由于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的速度會(huì)快些。
讀一個(gè)整數(shù): int n = sc.nextInt(); 相當(dāng)于 scanf("%d", &n); 或 cin >> n;?
讀一個(gè)字符串:String s = sc.next(); 相當(dāng)于 scanf("%s", s); 或 cin >> s;?
讀一個(gè)浮點(diǎn)數(shù):double t = sc.nextDouble(); 相當(dāng)于 scanf("%lf", &t); 或 cin >> t;?
讀一整行: String s = sc.nextLine(); 相當(dāng)于 gets(s); 或 cin.getline(...);?
判斷是否有下一個(gè)輸入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

例1:讀入整數(shù)
Input ?輸入數(shù)據(jù)有多組,每組占一行,由一個(gè)整數(shù)組成。?
Sample Input?
56
67
100
123?

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { // 判斷是否結(jié)束 int num = in.nextInt();// 讀入整數(shù) } } }

?

?

例2:讀入實(shí)數(shù)
?
輸入數(shù)據(jù)有多組,每組占2行,第一行為一個(gè)整數(shù)N,指示第二行包含N個(gè)實(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 < n; i++) { double a = sc.nextDouble(); } } } }

例3:讀入字符串【杭電2017 字符串統(tǒng)計(jì)】
輸入數(shù)據(jù)有多行,第一行是一個(gè)整數(shù)n,表示測試實(shí)例的個(gè)數(shù),后面跟著n行,每行包括一個(gè)由字母和數(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 < 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 第幾天?】
給定一個(gè)日期,輸出這個(gè)日期是該年的第幾天。?
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 n = Integer.parseInt(sc.nextLine()); for (int i = 0; i < n; i++) { String str = sc.nextLine(); } } }

2. 輸出??

函數(shù):
System.out.print();?
System.out.println();?
System.out.format();
System.out.printf(); ?

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));

import java.util.Scanner; public class Main { 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?類用來存儲(chǔ)字符串,可以用charAt方法來取出其中某一字節(jié),計(jì)數(shù)從0開始:?

String?a?=?"Hello";?//?a.charAt(1)?=?'e'?

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

System.out.println(a.substring(0,?4))?//?output?"Hell"?

注意第2個(gè)參數(shù)位置上的字符不包括進(jìn)來。這樣做使得?s.substring(a,?b)?總是有?b-a個(gè)字符。?

字符串連接可以直接用?+?號(hào),如?

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有兩個(gè)參數(shù),第一個(gè)為要轉(zhuǎn)的字符串,第二個(gè)為說明是什么進(jìn)制).??
BigInter?m?=?new?BigInteger(st,?base);?//?st是字符串,base是st的進(jìn)制.

?

?7.?數(shù)組排序

函數(shù):Arrays.sort();

?

轉(zhuǎn)載于:https://www.cnblogs.com/pomodoro/p/8525717.html

總結(jié)

以上是生活随笔為你收集整理的蓝桥杯Java输入输出相关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。