2.输入输出
public static void main(String[] args) throws IOException {
int m,n,k;
long l;
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
String str=bfr.readLine();
String s[]=str.split(" ");
n=Integer.parseInt(s[0]);
k=Integer.parseInt(s[1]);
System.out.println(n+" "+k);
}
package test;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner cin = new Scanner(System.in);
BigInteger a;
BigInteger b;
while(cin.hasNext()){
a = cin.nextBigInteger();
b = cin.nextBigInteger();
System.out.println(a.add(b));
System.out.println(a.subtract(b));
System.out.println(a.multiply(b));
System.out.println(a.divide(b));
System.out.println(a.remainder(b));
if( a.compareTo(b) == 0 ) System.out.println("a == b");
else if( a.compareTo(b) > 0 ) System.out.println("a > b");
else if( a.compareTo(b) < 0 ) System.out.println("a < b");
System.out.println(a.abs());
int exponent=10;
System.out.println(a.pow(exponent));
System.out.println(a.toString());
int p=8;
System.out.println(a.toString(p));
}
}
}
4.全排列:
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static int num=0;
static void swap(char c[],int index1,int index2)
{
char temp=c[index1];
c[index1]=c[index2];
c[index2]=temp;
}
static void Perm(char c[],int k,int n)
{
if(k==n)
{
num++;
for(int i=0;i<=n;i++)
{
System.out.print(c[i]+" ");
}
System.out.println();
}
else{
for(int i=k;i<=n;i++)
{
swap(c,i,k);
Perm(c, k+1, n);
swap(c,i,k);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader bfr =new BufferedReader(new InputStreamReader(System.in));
String str =bfr.readLine();
str=str.replace(" ", "");
char c[]=str.toCharArray();
num=0;
Perm(c,0,c.length-1);
System.out.println(num);
}
}
5.集合(hashMap,Set)
package test;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Map<String,String> map=new HashMap();
map.put("a", "aaa");
map.put("d", "ddd");
map.put("b", "bbb");
map.put("c", "ccc");
Iterator<String> iter =map.keySet().iterator();
while(iter.hasNext())
{
String str=iter.next();
System.out.println(str+" "+map.get(str));
}
Map<String,String> tmp=new TreeMap();
tmp.put("a", "aaa");
tmp.put("d", "ddd");
tmp.put("b", "bbb");
tmp.put("c", "ccc");
for(String s : tmp.keySet())
System.out.println(s+" "+map.get(s));
Set<String> set1=new TreeSet<String>();
set1.add("ADAD");
set1.add("ADAD");
set1.add("DAD");
Iterator<String> iter1=set1.iterator();
while(iter1.hasNext())
{
String s1=iter1.next();
System.out.println(s1);
}
for(String val:set1)
{
System.out.println(val);
}
}
}
6.优先队列
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static class Coordinate implements Comparable<Coordinate> {
int x,y;
public Coordinate(int x,int y) {
this.x=x;
this.y=y;
}
@Override
public int compareTo(Coordinate o) {
return o.x==this.x?this.y-o.y:this.x-o.x;
}
}
public static void main(String[] args) throws IOException {
PriorityQueue<Coordinate> pq =new PriorityQueue();
pq.add(new Coordinate(1,4));
pq.add(new Coordinate(1,3));
pq.add(new Coordinate(0,1));
while(!pq.isEmpty())
{
System.out.println(pq.peek().x+" "+pq.peek().y);
pq.poll();
}
return;
}
}
8.多维排序
import java.io.*;
import java.util.*;
public class Main {
public static class Coordinate implements Comparable<Coordinate> {
int x,y;
public Coordinate(int x,int y) {
this.x=x;
this.y=y;
}
@Override
public int compareTo(Coordinate o) {
return o.x==this.x?this.y-o.y:this.x-o.x;
}
}
public static void main(String[] args) throws IOException {
Coordinate a[]=new Coordinate[3];
a[0]=new Coordinate(1,4);
a[1]=new Coordinate(1,3);
a[2]=new Coordinate(0,1);
Arrays.sort(a);
for(Coordinate i:a)
{
System.out.println(i.x+" "+i.y);
}
List<Coordinate>arr=new ArrayList<Coordinate>();
arr.add(new Coordinate(1,4));
arr.add(new Coordinate(1,3));
arr.add(new Coordinate(0,1));
for(Coordinate i:arr)
{
System.out.println(i.x+" "+i.y);
}
Collections.sort(arr);
for(Coordinate i:arr)
{
System.out.println(i.x+" "+i.y);
}
return;
}
}
9.并查集
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=1000000;
static HashMap<Integer, Integer>map=new HashMap<Integer, Integer>();
static int count=0;
static int find(int i)
{
while(i!=map.get(i))
i=map.get(i);
return i;
}
static void union(int i,int j)
{
int ri=find(i);
int rj=find(j);
if(ri!=rj)
{
map.put(rj,ri);
count--;
}
return;
}
public static void main(String[] args) throws IOException {
int m,n,k;
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
String str=bfr.readLine();
String s[]=str.split(" ");
m=Integer.parseInt(s[0]);
n=Integer.parseInt(s[1]);
str=bfr.readLine();
s=str.split(" ");
k=Integer.parseInt(s[0]);
map=new HashMap<Integer, Integer>();
for(int i=1;i<=m*n;i++)
map.put(i, i);
count=m*n;
for(int i=1;i<=k;i++)
{
str=bfr.readLine();
s=str.split(" ");
int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
union(a,b);
}
System.out.println(count);
}
}
10.进制转换
public class Main {
public static void main(String[] args) {
int a=11;
String s="11";
for(int i=2;i<20;i++)
System.out.println(Integer.valueOf(s, i)+" "+Integer.parseInt(s, i));
System.out.println();
for(int i=2;i<20;i++)
{
System.out.println(Integer.toString(a, i));
}
System.out.println(Integer.toHexString(a));
System.out.println(Integer.toOctalString(a));
System.out.println(Integer.toBinaryString(a));
}
}
11.快速幂
import java.io.*;
import java.util.*;
public class Main {
public static int qPow(int a,int b) {
int base=a;
int re=1;
while(b!=0)
{
if((b&1)==1)
re*=base;
base*=base;
b=b>>1;
}
return re;
}
public static void main(String[] args) throws IOException {
System.out.println(qPow(3, 4));
}
}
12.拓扑排序
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<Integer> list=new ArrayList<Integer>();
static ArrayList<Integer>edge[]=new ArrayList[105];
static HashMap<String,Integer> map=new HashMap<String,Integer>();
static int inDegree[]=new int[105];
static void init()
{
list.clear();
map.clear();
for(int i=0;i<105;i++)
edge[i] = new ArrayList<Integer>();
Arrays.fill(inDegree, 0);
}
static String intToString(int i,HashMap<String,Integer> map)
{
Iterator<String> iterator=map.keySet().iterator();
while(iterator.hasNext())
{
String key=iterator.next();
if(map.get(key)==i)
return key;
}
return null;
}
static void topologicalSort(HashMap<String,Integer> map,ArrayList<Integer>edge[],int inDegree[],ArrayList<Integer> list,int count)
{
Queue<Integer>q=new LinkedList<Integer>();
Iterator<String> iterator=map.keySet().iterator();
while(iterator.hasNext())
{
String key=iterator.next();
if(inDegree[map.get(key)]==0)
q.add(map.get(key));
}
while(!q.isEmpty())
{
int now=q.poll();
list.add(now);
for(int i=0;i<edge[now].size();i++)
{
inDegree[edge[now].get(i)]--;
if(inDegree[edge[now].get(i)]==0)
q.add(edge[now].get(i));
}
}
if(list.size()!=count)
{
System.out.println("Error:存在环!");
return;
}
System.out.println();
for(int i=0;i<list.size();i++)
{
if(i==0)
System.out.print(intToString(list.get(i),map));
else
System.out.print(" "+intToString(list.get(i),map));
}
}
public static void main(String[] args) throws IOException {
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
String str=bfr.readLine();
String s[]=str.split(" ");
int n=Integer.parseInt(s[0]);
for(int i=0;i<n;i++)
{
str=bfr.readLine();
s=str.split(" ");
int m=Integer.parseInt(s[0]);
init();
int count=0;
for(int j=0;j<m;j++)
{
str=bfr.readLine();
s=str.split(" ");
String first=s[0];
String second=s[1];
if(!map.containsKey(first))
map.put(first, count++);
if(!map.containsKey(second))
map.put(second, count++);
inDegree[map.get(second)]++;
edge[map.get(first)].add(map.get(second));
}
topologicalSort(map,edge,inDegree,list,count);
}
}
}
13、最小生成树
package test;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class a {
static class node implements Comparable<node>{
int weight;
node lchild;
node rchild;
public node(int w,node l,node r)
{
this.weight=w;
this.lchild=l;
this.rchild=r;
}
public int compareTo(node o) {
return this.weight-o.weight;
}
}
static node createHuffmanTree(PriorityQueue<node> nodes)
{
while(nodes.size()>1)
{
node l=nodes.peek();
nodes.poll();
node r=nodes.peek();
nodes.poll();
node p=new node(l.weight+r.weight,l,r);
nodes.add(p);
}
return nodes.peek();
}
static long WPL=0;
static void preorder(node root,int levelNum)
{
if(root.lchild==null)
{
WPL+=root.weight*levelNum;
return;
}
else
{
preorder(root.lchild,levelNum+1);
preorder(root.rchild,levelNum+1);
}
};
public static void main(String[] args) {
PriorityQueue<node> nodes=new PriorityQueue<>();
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
for(int i=0;i<n;i++)
{
int w=cin.nextInt();
nodes.add(new node(w,null,null));
}
node r=createHuffmanTree(nodes);
WPL=0;
preorder(r,0);
System.out.println("WPL: "+WPL);
cin.close();
}
}
14、最短路的几种算法 (Dijkstra,spfa,floyd)
15、归并排序
package test;
public class Main {
public static void main(String[] args) {
int[] ans = {6, 8, 4, 4, 6, 36, 673, 13, 6, 7, 3, 4, 6, 8, 3, 7, 5, 7, 9, 5};
System.out.print("原数组:");
for (int i = 0; i < ans.length; i++) {
System.out.print(ans[i]+",");
}
System.out.println();
mergeSort(ans);
System.out.print("归并排序之后的数组:");
for (int i = 0; i < ans.length; i++) {
System.out.print(ans[i]+",");
}
}
private static void mergeSort(int[] ans) {
sort(ans, 0, ans.length - 1);
}
private static void sort(int[] ans, int left, int right) {
int mid = (left + right) / 2;
if (left >= right) {
return;
}
sort(ans, left, mid);
sort(ans, mid + 1, right);
merge(ans, left, mid, right);
}
private static void merge(int[] ans, int left, int mid, int right) {
int Actr = left;
int Bctr = mid + 1;
int Cctr = 0;
int lenA = mid - left + 1;
int lenB = right - mid;
int[] tmp = new int[right - left + 1];
while (Actr <= mid && Bctr <= right) {
if (ans[Actr] <= ans[Bctr]) {
tmp[Cctr++] = ans[Actr];
Actr++;
} else {
tmp[Cctr++] = ans[Bctr];
Bctr++;
}
}
while (Actr <= mid){
tmp[Cctr ++] = ans[Actr ++];
}
while (Bctr <= right){
tmp[Cctr ++] = ans[Bctr ++];
}
for (int i = 0; i < tmp.length; i++) {
ans[left++] = tmp[i];
}
}
}
public class HeapSorting {
public static void main(String[] args) {
int[] a=new int[]{0,9,8};
Sort(a,a.length-1);
for(int i:a){
System.out.println(i);
}
}
public static void MaxHeapify(int[] a,int index,int size){
int l=2*index;
int r=2*index+1;
int largest=index;
if(l<=size && a[l]>a[index]){
largest=l;
}
if(r<=size && a[r]>a[largest]){
largest=r;
}
if(largest!=index){
int temp=a[largest];
a[largest]=a[index];
a[index]=temp;
MaxHeapify(a,largest,size);
}
}
public static void HeapBuild(int[] a,int size){
for(int i=size/2;i>=1;i--){
MaxHeapify(a,i,size);
}
}
public static void Sort(int[] a,int size){
HeapBuild(a,size);
for(int i=size;i>=2;i--){
int temp=a[i];
a[i]=a[1];
a[1]=temp;
MaxHeapify(a,1,i-1);
}
}
}