从零开始学Java【基本输入输出、排序(sort)、高精度大数运算(BigInteger)】

今天上了第一次Java课,又得从零开始重新学一门新语言了。就像当年学C语言一样,慢慢做题,熟悉语法。

在oj上做题,可以立即看到正确或者错误的反馈,比较有动力。做了四道题,顺便怀念一下C++。(不过ACM还是要用C++)

hdu 1000 A + B Problem

初学一门语言,第一个要写的程序,除了 Hello World,就是 A + B Problem。

import java.util.Scanner;//输入必须要引用util包中的Scanner类

public class Main {

	public static void main(String [] args) {
	Scanner cin=new Scanner(System.in);//定义输入的名字,我比较怀念 C++ 的输入,所以定义成 cin
	while(cin.hasNext()) {
	//用 hasNext() 表示是否遇到了输入结束标志,括号内什么都不加就是输入到文件末尾才停止
	//比如while(!cin.hasNext("-1"))就是遇到输入字符串为"-1"时停止输入
		int a=cin.nextInt();
		int b=cin.nextInt();
		System.out.println(a+b);	
	}
	}
}

洛谷 P1177 【模板】快速排序

Java的Arrays类中有sort,可以直接排序,内部原理是根据数据元素整体分布确定排序方法,时间复杂度应该是稳定的O(n*logn)。

import java.util.Scanner;
import java.util.Arrays;//数组中进行排序需要Arrays类

public class Main {

	public static void main(String [] args) {
	Scanner cin=new Scanner(System.in);
	int n=cin.nextInt();
	int a[]=new int[n];
	//这里用定义数组的大小为n,是因为有多大开多大,如果大小超过n,则后面未赋值的元素默认为0,
	//则0成为了最小的元素,原数组中不存在的0排在前面,显然是错误输出
	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]+" ");
	}
}          

hdu 1002 A + B Problem II

高精度运算,比如大数加法,用Java很方便,有现成的数据类型可以直接用(这让C++情何以堪!)。
理论上Java中的BigInteger类可以支持无限长的大数运算(只要内存够),BigInteger支持加减乘除运算。

import java.util.Scanner;
import java.math.BigInteger;//用大数类型必须引入math包中的BigInteger类

public class Main {
	public static void main(String [] args) {
		Scanner cin=new Scanner(System.in);
		int T=cin.nextInt();
		for(int cas=1;cas<=T;cas++){
			BigInteger a=cin.nextBigInteger();
			BigInteger b=cin.nextBigInteger();
			System.out.printf("Case %d:%n",cas);//这里写成%s格式也行
			//注意换行的格式不要写成\n,否则会PE(估计是杭电的编译器太老了不支持)
            System.out.printf("%d + %d = %d%n",a,b,a.add(b));//这里我试了一下,%d格式 或 %s格式 都行
			if(cas<T)System.out.println();//换行
		}	
	}
}

湖南大学第十四届ACM程序设计新生杯(重现赛)G题 a+b+c+d=?

Java做法:

BigInteger初始化为0可以写 BigInteger ans=BigInteger.ZERO; 或者 BigInteger ans=new BigInteger("0");

import java.util.Scanner;
import java.math.BigInteger;
 
public class Main {
    public static void main(String [] args) {
        Scanner cin=new Scanner(System.in);
        int T=cin.nextInt();
        while(T--!=0) {//注意T组输入这里和C++不一样,要写!=0,否则会报错
            //BigInteger ans=new BigInteger("0");
            BigInteger ans=BigInteger.ZERO;//初始化ans为0,即BigInteger ans=new BigInteger("0");
            for(int i=0;i<4;i++) {
                BigInteger x=cin.nextBigInteger();
                ans=ans.add(x);
            }
        System.out.println(ans);
        }  
    }
}

C++做法:

当然,这题用C++也是能做的,而且不写高精度也能做。
这题出题人应该是特意卡了long long和unsigned long long的范围。
a+b+c+d,四个数相加,答案最大是4*261=263,最小是-263,而且我们必须要知道:

long long范围[-263,263-1]
unsigned long long范围[0,264-1]

所以我们只需要特判四个数都为261的情况,答案是263,这个时候long long会溢出,直接用unsigned long long格式输出263即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll T,a[5];
int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        ll ans=0;
        for(int i=1;i<=4;i++)
        {
            cin>>a[i];
            ans+=a[i];
        }
        if(a[1]>=0&&a[2]>=0&&a[3]>=0&&a[4]>=0&&ans<0)printf("%llu\n",1ull<<63);//ans溢出为负数
        else printf("%lld\n",ans);
    }
    return 0;
}
发布了100 篇原创文章 · 获赞 131 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ljw_study_in_CSDN/article/details/104617765