P1050
import java.util.ArrayList;
import java.util.Scanner;
public class Exercise1050 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 1; i <= n; i++)
if (isPerfNumber(i))
arr.add(i);
if (arr.size() >= 2)
for (int i = 0; i < arr.size() - 1; i++)
System.out.print(arr.get(i) + " ");
System.out.println(arr.get(arr.size() - 1));
}
}
private static boolean isPerfNumber(int n) {
if (n < 6)
return false;
int b = 1;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n%i==0)
b = b + i + n / i;
}
if (b == n)
return true;
return false;
}
}
P1051
import java.math.BigInteger;
import java.util.Scanner;
public class Exercise1051 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
BigInteger a = new BigInteger(sc.next());
int n = sc.nextInt();
BigInteger sum = new BigInteger("0");
for (int i = n; i > 0; i--) {
sum = sum.add(a.multiply(new BigInteger(i + "")));
a = a.multiply(new BigInteger(10 + ""));
}
System.out.println(sum);
}
}
}
注意非java的大数类实现
P1052
import java.util.Scanner;
public class Exercise1052 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
int j = sc.nextInt();
System.out.println(embody(a, j));
}
}
private static int embody(int[] a, int j) {
for (int i = 0; i < a.length; i++)
if (j == a[i])
return i;
return -1;
}
}
P1053
import java.util.Scanner;
public class Exercise1053 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = sc.nextInt();
a = exch(a);
for (int i = 0; i < a.length - 1; i++)
System.out.print(a[i] + " ");
System.out.println(a[a.length - 1]);
}
}
private static int[] exch(int[] a) {
int min = 0;
int max = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] < a[min])
min = i;
if (a[i] > a[max])
max = i;
}
int temp = a[min];
a[min] = a[max];
a[max] = temp;
return a;
}
}
注意输出最后没有空格
P1054
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Exercise1054 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
char[] a = s.toCharArray();
ArrayList<Character> arr = new ArrayList<Character>();
for (char c : a)
arr.add(c);
Collections.sort(arr);
s = "";
for (Character c : arr)
s += c;
System.out.println(s);
}
}
}
P1055
import java.util.Scanner;
public class Exercise1055 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
String s = sc.next();
char[] c = s.toCharArray();
s="";
for(int i =c.length-1;i>=0;i--)
s+=c[i];
System.out.println(s);
}
}
}
1056
import java.util.Scanner;
public class Exercise1056 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int p = sc.nextInt();
int q = sc.nextInt();
System.out.println(GCD(p, q));
}
}
private static int GCD(int p, int q) {
int r = p % q;
if (r == 0)
return q;
return GCD(q, r);
}
}
P1057
import java.util.Scanner;
public class Exercise1057 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[11];
while (sc.hasNext()) {
for (int i = 0; i < a.length; i++)
a[i] = 0;
int max = 0;
for (int i = 0; i < 20; i++)
a[sc.nextInt()]++;
for (int i = 1; i <= 10; i++) {
if (a[i] > a[max])
max = i;
}
System.out.println(max);
}
}
}
P1058
import java.util.Scanner;
public class Exercise1058 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
String a = "";
for (int i = s.length() - 1; i >= 0; i--)
a += s.charAt(i);
System.out.println(a);
}
}
}
P1059
public class Exercise1059 {
public static void main(String[] args)
{
System.out.println(3+" "+2+" "+1);
}
}
P1061
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Exercise1061 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ArrayList<Student1> arr;
while(sc.hasNext())
{
arr =new ArrayList<Student1>();
int n=sc.nextInt();
for(int i=0;i<n;i++)
arr.add(new Student1(sc.next(),sc.nextInt(),sc.nextInt()));
Collections.sort(arr);
for(Student1 student:arr)
System.out.println(student);
}
}
}
class Student1 implements Comparable{
private String name;
private int score;
private int age;
public Student1(String name,int age,int score)
{
this.name =name;
this.age=age;
this.score=score;
}
@Override
public int compareTo(Object o) {
Student1 t =(Student1)o;
if(this.score != t.score)
return this.score-t.score;
if(!this.name.equals(t.name))
return this.name.compareTo(t.name);
return this.age-t.age;
}
@Override
public String toString()
{
return this.name+" "+this.age+" "+this.score;
}
}
P1078
import java.util.Scanner;
public class Exercise1078 {
private static StringBuilder sb;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sb = new StringBuilder();
while (sc.hasNext()) {
sb.delete(0, sb.length());
String a = sc.next();
String b = sc.next();
print(a, b);
System.out.println(sb);
}
}
private static void print(String a, String b) {
//如果只有1个字母,直接输出
if (a.length() == 1) {
sb.append(a);
return;
}
int i = b.indexOf(a.charAt(0));
//只有右子树
if (i == 0) {
print(a.substring(1, a.length()), b.substring(1, b.length()));
sb.append(b.charAt(i));
return;
}
//只有左子树
if (i == b.length() - 1) {
print(a.substring(1, a.length()), b.substring(0, i));
sb.append(b.charAt(i));
return;
}
int j = a.indexOf(b.charAt(i - 1));
print(a.substring(1, j + 1), b.substring(0, i));
print(a.substring(j + 1, a.length()), b.substring(i + 1, b.length()));
sb.append(b.charAt(i));
}
}
前序遍历的一个个字符为root,对应的中序遍历所在位置的前一个字符一定是先序遍历左子树的最后一个字符
按此思想递归
P1089
import java.util.Scanner;
public class Exercise1089 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int a = sc.nextInt();
int b = sc.nextInt();
int sum =a+b;
if(revInteger(sum)==revSum(a, b))
System.out.println(sum);
else
System.out.println("NO");
}
}
private static int revSum(int a,int b)
{
return revInteger(a)+revInteger(b);
}
private static int revInteger(int a)
{
char[] a1 = (""+a).toCharArray();
String s="";
for(int i=a1.length-1;i>=0;i--)
s+=a1[i];
return Integer.parseInt(s);
}
}
P1090
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class Exercise1090 {
private TreeSet<String> set;
private String str;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Exercise1090 main = new Exercise1090();
main.init(sc);
}
private void init(Scanner sc) {
set = new TreeSet<String>();
StringBuilder sb =new StringBuilder();
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == 0)
System.exit(0);
set.clear();//不用每个案例都new,节省空间
for (int i = 0; i < n; i++) {
str = sc.next();
for (int j = 0; j < str.length(); j++) {
if ('\\' == str.charAt(j))
set.add(str.substring(0, j));
}
if (str.charAt(str.length() - 1) != '\\')
set.add(str);
}
sb.delete(0, sb.length());
int i;
int j;
Iterator<String> iter = set.iterator();
while (iter.hasNext()) {
sb = new StringBuilder();
String s = iter.next();
i = j = s.lastIndexOf('\\');
while (i-- >= 0)
sb.append(" ");
sb.append(s.substring(j + 1));
System.out.println(sb);
}
System.out.println();
}
sc.close();
}
}
每段路径按“\”分割,子目录自然排在父目录下,忽略前面输出
P1100
package Exercise1100;
import java.io.BufferedInputStream;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
* 优先队列+map+dij
*/
public class Exercise1100 {
private static BigDecimal maxBig;
private static Map<Integer, Vertex> map;
public static void main(String[] args) throws Exception {
BigDecimal[] edgeDis = new BigDecimal[501];
maxBig = new BigDecimal(2).pow(502);
for (int i = 0; i < edgeDis.length; i++)
edgeDis[i] = new BigDecimal(2).pow(i);
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
Vertex[] a = new Vertex[n];
for (int i = 0; i < n; i++)
a[i] = new Vertex(i, maxBig);
for (int i = 0; i < m; i++) {
int v = sc.nextInt();
int w = sc.nextInt();
a[v].adj.add(new Edge(w, edgeDis[i]));
a[w].adj.add(new Edge(v, edgeDis[i]));
}
a[0].dist = new BigDecimal(0);
//IndexMinPQ<Vertex> pq = new IndexMinPQ<Vertex>(n);
map = new HashMap<Integer, Vertex>();
for (int i = 0; i < n; i++)
map.put(i, a[i]);
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(n,
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return map.get(o1).compareTo(map.get(o2));
}
});
// pq.add(1, a[0]);
pq.add(0);
while (!pq.isEmpty()) {
int v = pq.poll();
a[v].marked = true;
for (Edge e : a[v].adj) {
int w = e.otherVer;
if (a[w].marked)
continue;
BigDecimal val = a[v].dist.add(e.edgeDis);
if (a[w].dist.compareTo(val) > 0)
a[w].dist = val;
pq.remove(w);
pq.add(w);
}
}
for (int i = 1; i < n; i++) {
if (a[i].dist.equals(maxBig))
System.out.println(-1);
else
System.out.println(a[i].dist.remainder(new BigDecimal(
100000)));
}
}
}
}
class Edge {
public int otherVer;
public BigDecimal edgeDis;
public Edge(int otherVer, BigDecimal edgeDis) {
this.otherVer = otherVer;
this.edgeDis = edgeDis;
}
}
class Vertex implements Comparable<Vertex> {
public BigDecimal dist;
public LinkedList<Edge> adj;
public int topNum;
public boolean marked;
public Vertex(int topNum, BigDecimal dist) {
this.topNum = topNum;
this.dist = dist;
adj = new LinkedList<Edge>();
marked = false;
}
@Override
public int compareTo(Vertex v) {
return this.dist.compareTo(v.dist);
}
}
用map+优先队列代替带索引的堆
package Exercise1100;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.Scanner;
public class Exercise1100_3 {
private static BigDecimal maxBig;
public static void main(String[] args) throws Exception {
BigDecimal[] edgeDis = new BigDecimal[501];
maxBig = new BigDecimal(2).pow(502);
for (int i = 0; i < edgeDis.length; i++)
edgeDis[i] = new BigDecimal(2).pow(i);
//Scanner sc = new Scanner(new BufferedInputStream(System.in));
Scanner sc=new Scanner(new FileReader(new File("a.txt")));
BufferedWriter br=new BufferedWriter(new FileWriter(new File("c.txt")));
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
Vertex[] a = new Vertex[n];
for (int i = 0; i < n; i++)
a[i] = new Vertex(i, maxBig);
for (int i = 0; i < m; i++) {
int v = sc.nextInt();
int w = sc.nextInt();
a[v].adj.add(new Edge(w, edgeDis[i]));
a[w].adj.add(new Edge(v, edgeDis[i]));
}
a[0].dist = new BigDecimal(0);
IndexMinPQ<Vertex> pq = new IndexMinPQ<Vertex>(n);
pq.add(1, a[0]);
while (!pq.isEmpty()) {
int v = pq.delMin()-1;
a[v].marked = true;
for (Edge e : a[v].adj) {
int w = e.otherVer;
if (a[w].marked)
continue;
BigDecimal val = a[v].dist.add(e.edgeDis);
if (a[w].dist.compareTo(val) > 0)
a[w].dist = val;
if (pq.contains(w + 1))
pq.change(w + 1, a[w]);
else
pq.add(w + 1, a[w]);
}
}
for (int i = 1; i < n; i++) {
if (a[i].dist.equals(maxBig))
//System.out.println(-1);
br.write(-1+"\r\n");
else
//System.out.println(a[i].dist.remainder(new BigDecimal(
// 100000)));
br.write(a[i].dist.remainder(new BigDecimal(
100000))+"\r\n");
}
br.close();
}
}
}
class Edge {
public int otherVer;
public BigDecimal edgeDis;
public Edge(int otherVer, BigDecimal edgeDis) {
this.otherVer = otherVer;
this.edgeDis = edgeDis;
}
}
class Vertex implements Comparable<Vertex> {
public BigDecimal dist;
public LinkedList<Edge> adj;
public int topNum;
public boolean marked;
public Vertex(int topNum, BigDecimal dist) {
this.topNum = topNum;
this.dist = dist;
adj = new LinkedList<Edge>();
marked = false;
}
@Override
public int compareTo(Vertex v) {
return this.dist.compareTo(v.dist);
}
}
class IndexMinPQ<E extends Comparable<E>> {
private int N;
private int[] pq;
private int[] qp;
private E[] keys;
public IndexMinPQ(int maxN) {
keys = (E[]) new Comparable[maxN + 1];
pq = new int[maxN + 1];
qp = new int[maxN + 1];
for (int i = 0; i <= maxN; i++)
qp[i] = -1;
}
public void add(int k, E e) {
N++;
pq[N] = k;
keys[k] = e;
qp[k] = N;
swim(N);
}
public void change(int k, E e) {
keys[k] = e;
swim(qp[k]);
sink(qp[k]);
}
public boolean contains(int k) {
return qp[k] != -1;
}
public int delMin() {
int indexOfMin = pq[1];
exch(1, N--);
sink(1);
keys[pq[N + 1]] = null;
qp[pq[N + 1]] = -1;
return indexOfMin;
}
public boolean isEmpty() {
return N == 0;
}
private boolean less(int i, int j) {
return keys[pq[i]].compareTo(keys[pq[j]]) < 0;
}
private void exch(int i, int j) {
int temp = pq[i];
pq[i] = pq[j];
pq[j] = temp;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
private void swim(int k) {
while (k > 1 && less(k, k / 2)) {
exch(k / 2, k);
k = k / 2;
}
}
private void sink(int k) {
while (2 * k <= N) {
int j = 2 * k;
while (j < N && less(j + 1, j))
j++;
if (less(k, j))
break;
exch(k, j);
k = j;
}
}
public E min() {
return keys[pq[1]];
}
public int size() {
return N;
}
}
索引优先队列
package Exercise1100;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Exercise1100_2 {
public static void main(String[] args) throws Exception {
//Scanner sc = new Scanner(new BufferedInputStream(System.in));
Scanner sc=new Scanner(new FileReader(new File("a.txt")));
BufferedWriter br=new BufferedWriter(new FileWriter(new File("b.txt")));
//while (sc.hasNext()) {
int[] edgeDis = new int[501];
edgeDis[0] = 1;
for (int i = 1; i < edgeDis.length; i++)
edgeDis[i] = (edgeDis[i - 1] *2) % 100000;
int n = sc.nextInt();
int m = sc.nextInt();
UF uf = new UF(n);
boolean[] marked = new boolean[n];
LinkedList<EdgeV>[] adj = (LinkedList<EdgeV>[]) new LinkedList[n];
for(int i=0;i<n;i++)
adj[i] =new LinkedList<EdgeV>();
int[] dist = new int[n];
for (int i = 0; i < m && uf.count > 1; i++) {
int v = sc.nextInt();
int w = sc.nextInt();
//System.out.println(v+" "+w);
if (uf.connected(w, v))
continue;
adj[v].add(new EdgeV(w, edgeDis[i]));
adj[w].add(new EdgeV(v, edgeDis[i]));
uf.union(w, v);
}
Queue<Integer> q = new LinkedList<Integer>();
q.add(0);
dist[0] = 0;
while (!q.isEmpty()) {
int i = q.poll();
marked[i] = true;
for (EdgeV e : adj[i]) {
int j = e.other;
if (marked[j])
continue;
dist[j] = (dist[i] + e.dist) % 100000;
q.add(j);
}
}
for (int i = 1; i < n; i++)
{
if (dist[i] == 0)
//System.out.println(-1);
br.write(-1+"\r\n");
else
//System.out.println(dist[i]);
//System.out.println(edgeDis[i]);
br.write(dist[i]+"\r\n");
}
br.close();
//}
}
}
class EdgeV {
public int other;
public int dist;
public EdgeV(int other, int dist) {
this.other = other;
this.dist = dist;
}
}
class UF {
private int[] a;
private int[] sz;
public int count;
public UF(int n) {
count = n;
a = new int[n + 1];
sz = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = i;
for (int i = 1; i <= n; i++)
sz[i] = 1;
}
public int find(int p) {
while (a[p] != p)
p = a[p];
return p;
}
public void union(int w, int v) {
int idw = find(w);
int idv = find(v);
if (idw == idv)
return;
if (sz[idw] < sz[idv]) {
a[idw] = idv;
sz[idv] += sz[idw];
} else {
a[idv] = idw;
sz[idw] += sz[idv];
}
count--;
}
public boolean connected(int w, int v) {
return find(w) == find(v);
}
}
kruskal+邻接表实现,WA原因不明
P1163
import java.util.ArrayList;
import java.util.Scanner;
public class Exercise1163 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
ArrayList<Integer> arr = foo(n);
if (arr.size() == 1)
System.out.println(arr.get(0));
else {
for (int i = 0; i < arr.size() - 1; i++)
System.out.print(arr.get(i) + " ");
System.out.println(arr.get(arr.size() - 1));
}
}
}
private static ArrayList<Integer> foo(int n) {
ArrayList<Integer> arr = new ArrayList<Integer>();
if (n <= 11) {
arr.add(-1);
return arr;
}
arr.add(11);
for (int i = 21; i < n; i = i + 10)
if (isPrime(i))
arr.add(i);
return arr;
}
private static boolean isPrime(int i) {
int m = (int) Math.sqrt(i);
if (i % 2 == 0)
return false;
for (int j = 3; j <= m; j = j + 2)
if (i % j == 0)
return false;
return true;
}
}
P1399
import java.io.BufferedInputStream;
import java.io.StreamTokenizer;
import java.util.TreeSet;
public class Exercise1399_2 {
public static void main(String[] args) throws Exception {
StreamTokenizer sc = new StreamTokenizer(new BufferedInputStream(
System.in));
while (sc.nextToken() != StreamTokenizer.TT_EOF) {
boolean flag = false;
int N = (int) sc.nval;
sc.nextToken();
double C = sc.nval;
TreeSet<Jewellery> arr = new TreeSet<Jewellery>();
for (int i = 0; i < N; i++) {
sc.nextToken();
double wig = sc.nval;
sc.nextToken();
double val = sc.nval;
arr.add(new Jewellery(wig, val));
}
double sumWeight = 0;
double sumValue = 0;
Jewellery j = null;
while (!arr.isEmpty()) {
j = arr.pollFirst();
double iWeight = j.getWeight();
if (sumWeight + iWeight >= C) {
flag = true;
break;
}
sumWeight += iWeight;
sumValue += j.getValue();
}
if (flag) {
double jValue = (C - sumWeight) * j.getPerValue();
sumValue = sumValue + jValue;
}
System.out.println((int) (sumValue + 0.5));
}
}
}
class Jewellery implements Comparable {
private double weight;
private double value;
private double perValue;
public Jewellery(double weight, double value) {
this.weight = weight;
this.value = value;
this.perValue = value / weight;
}
@Override
public int compareTo(Object o) {
Jewellery jew = (Jewellery) o;
if (this.perValue <= jew.perValue)
return 1;
else
return -1;
}
public double getWeight() {
return weight;
}
public double getValue() {
return value;
}
public double getPerValue() {
return perValue;
}
}
注意当盗贼袋子未装满的情况
P1488
public class Exercise1488 {
public static void main(String[] args)
{
System.out.print(10*30+" ");
int sum =0;
int j=1;
for(int i =0;i<30;i++)
{
sum+=j;
j=j<<1;
}
System.out.print(sum);
}
}
P1419
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Exercise1419 {
public static void main(String[] args)
{
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext())
{
int n=sc.nextInt();
ArrayList<String> arr = new ArrayList<String>();
sc.nextLine();
while(n>0)
{
arr.add(sc.nextLine());
n--;
}
Collections.sort(arr,new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.toLowerCase().compareTo(o2.toLowerCase());
}
});
for(String s:arr)
System.out.println(s);
}
}
}
import java.util.Scanner;
public class Exercise1489 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int[][] a = new int[2][3];
int[][] b = new int[3][2];
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
a[i][j] = sc.nextInt();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 2; j++)
b[i][j] = sc.nextInt();
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 3; k++)
c[i][j] += a[i][k] * b[k][j];
System.out.println(c[0][0] + " " + c[0][1] + " ");
System.out.println(c[1][0] + " " + c[1][1] + " ");
}
}
}
坑爹的发现这个OJ不能把初始化放在while外面,否则WA