P01 世纪末的星期
package Code2013;
import java.util.Calendar;
/**
*
* 世纪末的星期
* @author vccyb
*
*/
public class P001 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
for(int year=1999;year<10000;year+=100){
//一个世纪一个世纪的去翻|
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH,11); //11表示设置的是12月
calendar.set(Calendar.DAY_OF_MONTH, 31);
System.out.println(year+" "+calendar.get(Calendar.DAY_OF_WEEK));
if(calendar.get(Calendar.DAY_OF_WEEK)==1){
System.out.println(year);
break;
}
}
}
}
1999 6
2099 5
2199 3
2299 1
2299
所以答案就是 2299
java组中用日期api,calendar比较多
P02 振兴中华
只能往右走和往下走
package Code2013;
/**
*
*
* 深度搜索
* dfs
*
* @author vccyb
*
*/
public class P002 {
//重复
//变化
//边界
static int dfs(int i, int j){
if(i==3||j==4) return 1;
return dfs(i+1,j) + dfs(i,j+1);
}
public static void main(String[] args) {
int ans = dfs(0,0);
System.out.println(ans);
}
}
35
P03 梅森素数
package Code2013;
import java.math.BigInteger;
/**
*
* 梅森素数
* @author vccyb
*
*/
public class P003 {
public static void main(String[] args) {
BigInteger x = BigInteger.valueOf(2).pow(11213).subtract(BigInteger.ONE);
//2^11213 - 1
String line = x.toString();
String ans = line.substring(line.length()-100, line.length());
System.out.println(line.length());
System.out.println(ans.length());
System.out.println(ans);
}
}
大数类 BigInteger
最后一百位
a.length()-100, a.length()
ans:
3376
100
8586718527586602439602335283513944980064327030278104224144971883680541689784796267391476087696392191
P04 颠倒的价格
package Code2013;
import java.util.ArrayList;
/**
*
*
* 颠倒
* @author vccyb
*
*/
public class P004 {
/**
*
* 枚举4位数,简单筛选
*颠倒做差,200多的放入一个集合,800多的放入一个集合
*遍历两个集合,检查是否相加为558
*输出结果
* @param args
*/
public static void main(String[] args) {
ArrayList<Price> a1 = new ArrayList<Price>();
ArrayList<Price> a2 = new ArrayList<Price>();
// System.out.println(reverse("1958"));
// System.out.println(reverse("5456"));
for(int i=1000;i<10000;i++){
//颠倒?
String s = ""+i;
if(s.contains("3")||s.contains("4")||s.contains("7"))continue;
String re_s = reverse(s);
int i1 = Integer.parseInt(re_s);
int plus = i1 - i; //
// System.out.println(s+" "+re_s);
// System.out.println(i1+" "+plus);
if(plus<-200&&plus>-300) a1.add(new Price(i, plus));
if(plus<900&&plus>800) a2.add(new Price(i, plus));
}
for(Price p1:a1){
for(Price p2:a2){
if(p1.plus+p2.plus==558){
System.out.println(p1.p+" "+p1.plus);
System.out.println(p2.p+" "+p2.plus);
}
}
}
}
private static String reverse(String s){
//
char[] ans = new char[s.length()];
for(int i=s.length()-1,j=0;i>=0;j++,i--){
char c = s.charAt(j);
if(c=='6')ans[i]='9';
else if(c=='9')ans[i]='6';
else ans[i]=c;
}
return new String(ans);
}
private static class Price{
int p; //原价
int plus;//颠倒价格-原价
public Price(int p, int plus){
this.p = p;
this.plus = plus;
}
}
}
ans 9088
P07 错误票据
package Code2013;
import java.io.*;
import java.util.*;
public class P007 {
/**
* 错误票据
*
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
int N = Integer.parseInt(br.readLine());
for(int i=0;i<N;i++){
String[] line = br.readLine().split(" ");
for(int j=0;j<line.length;j++){
list.add(Integer.parseInt(line[j]));
}
}
//排序
Collections.sort(list); //注意:list的排序是用collections类
int a=0,b=0; //a断号,b重号
for(int i=1;i<list.size();i++){
if(list.get(i)-list.get(i-1)==2){
a = list.get(i)-1;
}
if(list.get(i).equals(list.get(i-1))){
b = list.get(i);
}
}
System.out.println(a+" "+b);
}
}
P08 带分数
全排列是很重要的
static int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public static void qpl(int k) {
if (k == arr.length) {
check();
return;
}
for (int i = k; i < arr.length; i++) {
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
qpl(k + 1);
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
package Code2013;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class P008 {
static int[] arr = {1,2,3,4,5,6,7,8,9};
static int n;
static int ans=0;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
qpl(0);
System.out.println(ans);
}
static void qpl(int x){
if(x==arr.length){
// System.out.println("全排列结果:");
// for(int i=0;i<arr.length;i++){
// System.out.print(arr[i]);
// }
// System.out.println();
check();
return;
}
for(int i=x;i<arr.length;i++){
int t = arr[i];
arr[i]=arr[x];
arr[x] = t;
qpl(x+1);
//回溯
t = arr[i];
arr[i]=arr[x];
arr[x] = t;
}
}
static void check(){
for(int i=0;i<arr.length-2;i++){
int num1 = f(0,i);
if(num1<n){
for(int j=i+1;j<arr.length-1;j++){
int num2 = f(i+1,j);
int num3 = f(j+1,arr.length-1);
if(num1+num2/num3==n&&num3%num2==0){
ans++;
}
}
}
}
}
static int f(int left,int right){
int sum = 0;
for(int i=left;i<=right;i++){
sum = sum*10 + arr[i];
}
return sum;
}
}