ACM競賽 Java編程總結

來源:互聯網
上載者:User

標籤:

1.字串的長度

String  str = new String(" abcd");

int length = str.length();

2.數組的長度

2.1對於 a[][] a.length代表a的行數      a[i].length代表a的列數2.2對於a[]      a.length代表a的長度

3.字串與字元數組的轉化

String str = new String("abcd");

char [] a = str.toCharArray();

4.字串數字與數位轉化

4.1 String—>int

String str = "1234";

int i = Integer.parseInt(str);  //可能會拋出異常 ,(如果報錯)在 main(String[] args)後面加上throws Exception

4.2 int —>String

int i=235;

String s = String.valueOf(i);  //Of大寫

5.從.in檔案匯入資料

import java.IO.*;

public static void main(String [] args) throws IOException{  //會拋出異常

FileReader a = new FileReader("D-small-attempt1.in");//檔案與源碼在同一目錄下
BufferedReader read = new BufferedReader(a);
String textLine="";
String str="";
while(( textLine=read.readLine())!=null){
            str+=textLine+" ";
}
String[] numbersArray=str.split(" ");   //將str按空格分開,儲存成字元數組

}

6.導處資料到.txt檔案

import java.IO.*;

FileWriter fw = new FileWriter("output.txt");  
BufferedWriter bufw = new BufferedWriter(fw);  
        String line = null;  
        for(int i=1;i<n+1;i++){  
            bufw.write("Case #"+i+": "+result[i]);  
            bufw.newLine();    //下一行
            bufw.flush();     //更新
        }  
       bufw.close(); 

7.保留有限位小數

import java.text.*

DecimalFormat sim = new DecimalFormat("0.000000");//保留多少位小數點後面就有幾位
double six = sim.format(source); //將source保留六位小數

比較簡單的方法(對於輸出結果保留有限位小數)

System.out.printf("%.3f",a);

ACM競賽 Java編程總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.