java的acm輸入輸出格式+大數語法


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

2.多組輸入,讀取到文件尾

Scanner scan=new Scanner(System.in);

while(scan.hasNext())

或scan.hasNextInt()或scan.hasNextDouble()或scan.hasNextLine()

3.輸入輸出

語法1:Scanner scan = new Scanner (new BufferedInputStream(System.in));//import java.io.BufferedInputStream;

語法2:Scanner scan=new Scanner(System.in);

讀入數據多的時候,用語法1比較快

讀整數:int n = scan.nextInt(); = scanf("%d",&n); = cin>>n;

讀小數:double f = scan.nextDouble(); = scanf("%lf",&f); = cin>>f;

讀字符串:String s = scan.next(); = cin >> s; 

讀一整行:String s = sc.next(); 相當於 scanf("%s", s); 或 cin >> s; 

有一些題目是有讀入字符的,hdu2005-第幾天?(多組輸入,YYYY/MM/DD格式)

用字符串讀入再分割。

 1 import java.math.BigInteger;//操作大整數
 2 import java.math.BigDecimal;//操作大小數
 3 import java.io.BufferedInputStream;  4 import java.util.Scanner;  5 import java.util.Arrays;  6 public class Main//hdu2005
 7 {  8     public static void main(String []args)  9  { 10         Scanner scan = new Scanner(System.in); 11         
12         while( scan.hasNext()) 13  { 14             int a[]={0,31,59,90,120,151,181,212,243,273,304,334};//java不能int a[12]
15             String str = scan.nextLine(); 16             String date[] = str.split("/"); 17             int year = Integer.parseInt(date[0]); 18             int month = Integer.parseInt(date[1]); 19             int day = Integer.parseInt(date[2]); 20             int sum=0; 21             if(year%400==0 || ( year%4==0 && year%100!=0)) 22                 a[2]++; 23             sum = a[month-1]+day; 24  System.out.println(sum); 25  } 26  } 27 }
View Code

4.大數類(java殺招)

import java.math.BigInteger;//操作大整數
import java.math.BigDecimal;//操作大小數

大數不能用簡單的加減乘除符號,需要調用方法。

import java.math.BigInteger;//操作大整數
import java.math.BigDecimal;//操作大小數
import java.io.BufferedInputStream; import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String []args) { BigInteger a,b,ans; Scanner scan=new Scanner(System.in); a=scan.nextBigInteger(); b=scan.nextBigInteger(); ans=a.add(b);//ans=a+b;
        ans=a.subtract(b);//ans=a-b
        ans=a.mod(b);//ans=a%b
        ans=a.divide(b);//ans=a/b
        ans=a.max(b); ans=a.min(b);
     ans=a.multiply(b); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM