1. A+B
🍑 POJ1000 a+b
🍔 签到题
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(System.in);
int a=cin.nextInt(),b=cin.nextInt();
System.out.println(a+b);
}
}
2. 高精度
🍑 POJ1001 求高精度幂
输入
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
输出
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
🍔 BigDecimal
🍑 BigDecimal.stripTrailingZeros():移除尾部所有没有意义的 0
🍑 BigDecimal.toPlainString():原值转成字符串返回
🍑 BigDecimal.toString():有可能会返回科学计数法
🍑 String.replaceFirst(String regex, String replacement);
🍁 正则匹配前导零:^0*
import java.math.BigDecimal;
import java.util.*;
public class N1001高精度
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
// double a = sc.nextDouble();
// BigDecimal bd = new BigDecimal(a);
BigDecimal bd = sc.nextBigDecimal();
int b = sc.nextInt();
String value = bd.pow(b).stripTrailingZeros().toPlainString().replaceFirst("^0*", "");
System.out.println(value);
}
}
}
3. 487-3279
🍑 POJ1002 487-3279
输入
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
输出
310-1010 2
487-3279 4
888-4567 3
🍔 AC代码:字符串存电话号码
🥞 Treeset:自动按升序对元素进行排序
🥞
import java.util.*;
public class Main
{
public static void main(String[] args)
{
char[] key = new char[128];// 对 字母 映射成 对应的数字
key['A'] = key['B'] = key['C'] = '2';
key['D'] = key['E'] = key['F'] = '3';
key['G'] = key['H'] = key['I'] = '4';
key['J'] = key['K'] = key['L'] = '5';
key['M'] = key['N'] = key['O'] = '6';
key['P'] = key['R'] = key['S'] = '7';
key['T'] = key['U'] = key['V'] = '8';
key['W'] = key['X'] = key['Y'] = '9';
// HashMap<String,Integer> map = new HashMap();
//不知道为什么使用HashMap超时,TreeMap通过,可能散列函数对样例不太适用吧
TreeMap<String, Integer> map = new TreeMap();// TreeMap 默认排升序
String tmp[] = null;
Scanner sc = new Scanner(System.in);
// sc = new Scanner(new File("in.txt"));
int n = sc.nextInt();
boolean flag = false;
for (int i = 0; i < n; i++)
{
String ss = sc.next();
char[] arr = ss.toCharArray();// 转为数组处理
String num = "";
for (int j = 0; j < arr.length; j++)
{
if (arr[j] >= '0' && arr[j] <= '9')
{
num += arr[j];// 字符串拼接
} else if (arr[j] >= 'A' && arr[j] <= 'Z')
{
num += key[arr[j]];
}
}
if (map.containsKey(num))
{
flag = true;
map.put(num, map.get(num) + 1);
} else
{
map.put(num, 1);
}
}
if (!flag)
System.out.println("No duplicates.");
else
{
for (String k : map.keySet())
{
int value = map.get(k);
if (value > 1)
{
for (int i = 0; i < k.length(); i++)
{
System.out.print(k.charAt(i));
if (i == 2)
System.out.print("-");
}
System.out.println(" " + value);
}
}
}
}
}
🍑 没考虑高位为 0 的情况
😪 WA版本
import java.util.*;
import java.io.*;
public class Main
{
static int[] st = new int[256];
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException
{
// Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();//注意 scanf 不处理回车键(会留在缓冲区影响下一次输入)
int n = Integer.parseInt(in.readLine());
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();// key 表示电话号码,value 表示出现次数
st['A'] = st['B'] = st['C'] = 2;
st['D'] = st['E'] = st['F'] = 3;
st['G'] = st['H'] = st['I'] = 4;
st['J'] = st['K'] = st['L'] = 5;
st['M'] = st['N'] = st['O'] = 6;
st['P'] = st['R'] = st['S'] = 7;
st['U'] = st['V'] = st['T'] = 8;
st['W'] = st['X'] = st['Y'] = 9;
for (int i = 0; i < 10; i++)
{
st[i + '0'] = i;
}
boolean isDuplicate = false;
for (int i = 0; i < n; i++)
{
String s = in.readLine();
s = s.replace("-", "");
int len = s.length();
int num = 0;
// 处理号码
for (int j = 0; j < len; j++)
{
char c = s.charAt(j);
if (c != '-')
num = num * 10 + st[c];
}
// int t = map.getOrDefault(num, 0);
int t = map.get(num) == null ? 0 : map.get(num);
if (t > 0)
isDuplicate = true;// 只要有一个号码出现一次重复
map.put(num, t + 1);// 记录号码的次数
}
if (!isDuplicate)
{
System.out.println("No duplicates.");
} else
{
for (int k : map.keySet())
{
int value = map.get(k);
if (value > 1)
{
// out.write(k / 10000 + "-" + k % 10000 + " " + value + "\n");// k%10000 会出现 0 的情况,不可行
System.out.printf("%03d-%04d %d", k / 10000, k % 10000, value);
}
}
}
out.flush();
}
}
4. 叠卡片
🍑 POJ 1003 Hangover
输入
1.00
3.71
0.04
5.19
0.00
输出
3 card(s)
61 card(s)
1 card(s)
273 card(s)
🍑 大体题意:
🍤 从上到下,第1张卡片能伸出 1/2 的长度,第2张卡片 1/3,第三张 1/4 ……
🍤 问:需要多少张卡片才能伸出 输入 的长度 n
🍤 每张卡片的长度为 1
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
double n = sc.nextDouble();
if (n == 0.00)
break;
double len = 0;
int i = 2;
int ans = 0;
while (len < n)
{
len += 1.0 / i;
i++;
}
System.out.printf("%d card(s)\n", i - 2);
}
}
}
5. 财务管理
🍑 POJ1004 Financial Management
输入
100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75
输出
$1581.42
🍑 输入12个数求平均数
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double sum = 0;
for (int i = 0; i < 12; i++)
sum += sc.nextDouble();
System.out.printf("$%.2f", sum / 12);//浮点数输出用 %f
}
}
6. 我想我需要一艘船屋
🍑 POJ 1005 I Think I Need a Houseboat
输入
2
1.0 1.0
25.0 0.0
输出
Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.
END OF OUTPUT.
🍑 Π:Math.PI
🍑 求出 坐标 到 原点 的 欧几里得距离 --> 半径 --> 面积 --> 年数(向上取整)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
double x = sc.nextDouble();
double y = sc.nextDouble();
double r = Math.sqrt(x * x + y * y);
double s = Math.PI * r * r / 2;
int year = (int) s / 50 + 1;
System.out.printf("Property %d: This property will begin eroding in year %d.\n", i, year);
}
System.out.println("END OF OUTPUT.");
}
}
7. 生理周期
🍑 POJ 1006 Biohythms
输入
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
输出
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
🍑 跳跃枚举
🍤 缓冲流输入输出
🍤 判断条件放 for 里边
🍤 输出格式注意空格(Presentation Error)
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
for (int j = 1;; j++)
{
String[] ss = in.readLine().split(" ");
int p = Integer.parseInt(ss[0]);
int e = Integer.parseInt(ss[1]);
int i = Integer.parseInt(ss[2]);
int d = Integer.parseInt(ss[3]);
if (p == -1 && e == -1 && i == -1 && d == -1)
break;
int k = d + 1;
for (; (k - p) % 23 != 0; k++)
;
for (; (k - e) % 28 != 0; k += 23)
;
for (; (k - i) % 33 != 0; k += 23 * 28)
;
out.write("Case " + j + ": the next triple peak occurs in " + (k - d) + " days.\n");
}
out.flush();
}
}
🍑 中国剩余定理
🙈 线性同余方程、扩展欧几里得求逆元 (挖个坑)
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int M = 21252;
int cnt = 1;
while (true)
{
String[] ss = in.readLine().split(" ");
int p = Integer.parseInt(ss[0]);
int e = Integer.parseInt(ss[1]);
int i = Integer.parseInt(ss[2]);
int d = Integer.parseInt(ss[3]);
if (p == -1 && e == -1 && i == -1 && d == -1)
break;
int res = (5544 * p + 14421 * e + 1288 * i) % M;
res -= d;
if (res <= 0)
res = (res + M - 1) % M + 1;
out.write("Case " + cnt + ": the next triple peak occurs in " + res + " days.\n");
}
out.flush();
}
}
8. DNA 排序
🍑 POJ 1007 DNA Sorting
🍑 HDOJ 1379 DNA Sorting
👨🏫 区别:杭电是多组输入
输入
1
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
输出
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
🍑 按 逆序数 进行 稳定排序
🥞 归并排序求逆序数
🍑 HDOJ AC啦
import java.util.Scanner;
public class Main
{
static int l;
static char[] a;
static char[] tmp;
static boolean[] st;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0)
{
l = sc.nextInt();
int n = sc.nextInt();
String[] ss = new String[n];
long[] cnt = new long[n];// 记录每个字符串的逆序数
for (int i = 0; i < n; i++)
{
ss[i] = sc.next();
}
for (int i = 0; i < n; i++)
{
a = new char[l];
tmp = new char[l];
a = ss[i].toCharArray();
long t = mergeSort(0, l - 1);
cnt[i] = t;
}
// 按逆序对升序排序
int[] mk = new int[n];// 按升序存 DNA 数组下标
int k = 0;
st = new boolean[n];
while (k < n)
{
int min = -1;
for (int i = 0; i < n; i++)
{
if (!st[i] && (min == -1 || cnt[i] < cnt[min]))
min = i;
}
mk[k++] = min;
st[min] = true;
}
for (int i = 0; i < n; i++)
{
System.out.println(ss[mk[i]]);
}
}
}
// 输入区间的左右边界,返回该区间的逆序数
private static long mergeSort(int l, int r)
{
if (l == r)
return 0;
int mid = l + r >> 1;
// 分治求解
long ans = mergeSort(l, mid) + mergeSort(mid + 1, r);
int i = l;// 左区间指针
int j = mid + 1;// 右区间指针
int k = 0;
while (i <= mid && j <= r)
{
if (a[i] <= a[j])
tmp[k++] = a[i++];
else
{
tmp[k++] = a[j++];
ans += mid - i + 1;// 左区间剩余的元素都比当前元素大,更新结果
}
}
// 处理掉剩余的元素
while (i <= mid)
tmp[k++] = a[i++];
while (j <= r)
tmp[k++] = a[j++];
k = 0;
for (i = l; i <= r; i++)
a[i] = tmp[k++];
return ans;
}
}
😡 POJ RE啦
import java.util.Scanner;
public class N1007DNA排序
{
static int l;
static char[] a;
static char[] tmp;
static boolean[] st;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
l = sc.nextInt();
int n = sc.nextInt();
String[] ss = new String[n];
long[] cnt = new long[n];// 记录每个字符串的逆序数
for (int i = 0; i < n; i++)
{
ss[i] = sc.next();
}
for (int i = 0; i < n; i++)
{
a = new char[l];
tmp = new char[l];
a = ss[i].toCharArray();
long t = mergeSort(0, l - 1);
cnt[i] = t;
}
// 按逆序对升序排序
int[] mk = new int[n];// 按升序存 DNA 数组下标
int k = 0;
st = new boolean[n];
while (k < n)
{
int min = -1;
for (int i = 0; i < n; i++)
{
if (!st[i] && (min == -1 || cnt[i] < cnt[min]))
min = i;
}
mk[k++] = min;
st[min] = true;
}
for (int i = 0; i < n; i++)
{
System.out.println(ss[mk[i]]);
}
}
// 输入区间的左右边界,返回该区间的逆序数
private static long mergeSort(int l, int r)
{
if (l == r)
return 0;
int mid = l + r >> 1;
// 分治求解
long ans = mergeSort(l, mid) + mergeSort(mid + 1, r);
int i = l;// 左区间指针
int j = mid + 1;// 右区间指针
int k = 0;
while (i <= mid && j <= r)
{
if (a[i] <= a[j])
tmp[k++] = a[i++];
else
{
tmp[k++] = a[j++];
ans += mid - i + 1;// 左区间剩余的元素都比当前元素大,更新结果
}
}
// 处理掉剩余的元素
while (i <= mid)
tmp[k++] = a[i++];
while (j <= r)
tmp[k++] = a[j++];
k = 0;
for (i = l; i <= r; i++)
a[i] = tmp[k++];
return ans;
}
}