java在acm中的运用

java在acm中的运用

1. 输入

格式

Scanner cin = new Scanner (new BufferedInputStream(System.in));
// 当然也可以直接 Scanner cin = new Scanner(System.in);只是加Buffer可能会快一些

读数类型

1. 读一个整数:int n = cin.nextInt()//相当于scanf("%d", &n);或 cin >> n;
2. 读一个字符串:String s = cin.next();//相当于scanf("%s", s);或 cin >> s;
3. 读一个浮点数:double t = cin.nextDouble();//相当于scanf("%lf", &t); 或 cin >> t;
4. 读一整行:String s = cin.nextLine();//相当于gets(s);或 cin.getline(...);

例程

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public classMain
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner (new BufferedInputStream(System.in));
        int a; double b; BigInteger c; String st;
        a = cin.nextInt(); b = cin.nextDouble(); c = cin.nextBigInteger(); d = cin.nextLine(); // 每种类型都有相应的输入函数.
    }
}

2. 输出

格式

System.out.print(); // cout << …;
System.out.println(); // cout << … << endl;
System.out.printf(); // 与C中的printf用法类似.

规格化输出

// 这里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));

例程

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public classMain
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner (new BufferedInputStream(System.in));
        int a; double b;
        a = 12345; b = 1.234567;
        System.out.println(a + " " + b);
        System.out.printf("%d %10.5f\n", a, b); // 输入b为字宽为10,右对齐,保留小数点后5位,四舍五入.
    }
}

3. 字符串处理

用法
1.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类。

例程

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public classMain
{
    public static void main(String[] args)
    {
        int i;
        Scanner cin = new Scanner (new BufferedInputStream(System.in));
        String st = "abcdefg";
        System.out.println(st.charAt(0)); // st.charAt(i)就相当于st[i].
        char [] ch;
        ch = st.toCharArray(); // 字符串转换为字符数组.
        for (i = 0; i < ch.length; i++) ch[i] += 1;
        System.out.println(ch); // 输入为“bcdefgh”.
        if (st.startsWith("a")) // 如果字符串以'0'开头.
        {
            st = st.substring(1); // 则从第1位开始copy(开头为第0位).
        }
    }
}

4. 高精度

介绍
    BigInteger 和 BigDecimal 是在java.math包中已有的类,前者表示整数,后者表示浮点数

用法

(import java.math.*)    // 需要引入 java.math 包
BigInteger a = BigInteger.valueOf(100);
BigInteger b = BigInteger.valueOf(50);
BigInteger c = a.add(b)    // c = a + b;

方法

BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
int compareTo(BigInteger other)
static BigInteger valueOf(long x)
输出大数字时直接使用 System.out.println(a) 即可。

例程

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public classMain
{
    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");
    }
}

5. 进制转换

用法

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的进制.
// 1.如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2);当然也可以使用其他进制方式读入;
// 2.如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);//将它转换成2进制表示的字符串

例程

import java.io.*;
import java.util.*;
import java.math.*;

public classMain
{
    public static void main(String[] args)
    {
        int b;
        BigInteger p,m,ans;
        String str ;
        Scanner cin = new Scanner (new BufferedInputStream(System.in));
        while(cin.hasNext())
        {
            b=cin.nextInt();
            if(b==0)
                break;
            p=cin.nextBigInteger(b);
            m=cin.nextBigInteger(b);
            ans=p.mod(m);
            str=ans.toString(b);
            System.out.println(str);
        }
    }
}

6. 排序

例程

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public classMain
{
    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] + " ");
    }
}

7. 其他注意

(1) Java 是面向对象的语言,思考方法需要变换一下,里面的函数统称为方法,不要搞错。
(2) Java 里的数组有些变动,多维数组的内部其实都是指针,所以Java不支持fill多维数组。数组定义后必须初始化,如 int[] a = new int[100];
(3) 布尔类型为 boolean,只有true和false二值,在 if (…) / while (…) 等语句的条件中必须为boolean类型。在C/C++中的 if (n % 2) … 在Java中无法编译通过
(4) 下面在java.util包里Arrays类的几个方法可替代C/C++里的memset、qsort/sort 和 bsearch:

Arrays.fill()
Arrays.sort()
Arrays.binarySearch() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值