1.明明的随机数
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
TreeSet<Integer> set = new TreeSet();
for(int i=0;i<n;i++)
set.add(sc.nextInt());
//Object a[] = set.toArray();
//Arrays.sort(a);
for(Integer i : set)
System.out.println(i);
}
}
}
2.字符串分隔
import java.util.*;
public class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str1 = sc.nextLine();
int len1 = str1.length();
if (len1!= 0)
{
if (len1 % 8 < 8&&len1%8!=0) {
for (int i = 0; i < 8 - len1 % 8; i++)
str1 = str1 + "0";
}
len1 = str1.length();
for (int i = 0; i < len1 / 8; i++) {
String s = str1.substring(i * 8, 8 * (i + 1));
System.out.println(s);
}
}
}
}
}
3.进制转换
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String s = sc.nextLine();
s = s.substring(2);
System.out.println(Integer.valueOf(s,16));
}
}
}
4.质数因子
//一个合数必定可以因式分解为两个质数。
//此题相当于因式分解,因式分解后的因子都是质数。所以从2开始,一个个试即可,2,3,4,4=2*2
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Long n = sc.nextLong();
System.out.println(getResult(n));
}
}
public static String getResult(long ulDataInput){
String str = new String();
int index = 2;
while(ulDataInput>1){
if(ulDataInput%index==0){
str+=index+" ";
ulDataInput = ulDataInput/index;
}
else
index++;
}
return str;
}
}
5.取近似值
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextDouble()){
double a = sc.nextDouble();
int b = new Double(a).intValue();
if(a-b>=0.5)
System.out.println(b+1);
else
System.out.println(b);
}
}
}
6字符串最后一个单词长度
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String str = sc.nextLine();
System.out.println(str.length()-1-str.lastIndexOf(" "));
}
}
}
7.计算字符个数
8.合并表记录
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
TreeMap<Integer,Integer> map = new TreeMap();
while((n--)>0){
int index = sc.nextInt();
int value = sc.nextInt();
if(map.containsKey(index)){
map.put(index, map.get(index)+value);
}
else
map.put(index, value);
}
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key+" "+value);
}
}
}
9.提取不重复的整数
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
StringBuffer s = new StringBuffer(sc.nextLine());
s.reverse();
char a[] = s.toString().toCharArray();
ArrayList<Character> list = new ArrayList();
for(int i=0;i<a.length;i++){
if(!list.contains(a[i]))
list.add(a[i]);
}
int n=0;
for(int i=0;i<list.size();i++){
n = n*10+(list.get(i)-'0');
}
System.out.println(n);
}
}
}
10.字符个数统计
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String s = sc.nextLine();
ArrayList<Character> list = new ArrayList();
char a[] = s.toCharArray();
for(int i=0;i<a.length;i++){
if((int)a[i]>=0&&(int)a[i]<=127){
if(!list.contains(a[i]))
list.add(a[i]);
}
}
System.out.println(list.size());
}
}
}
11. 字串的连接最长路径查找
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
int n = Integer.parseInt(sc.nextLine());
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = sc.nextLine();
}
//冒泡排序
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++){
if(str[j].compareTo(str[j+1])>0)
{
String temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
for(int i=0;i<n;i++){
System.out.println(str[i]);
}
}
}
}
12.求int型数据在内存中存储时1的个数
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int count = 0;
while(n!=0){
count++;
n = n&(n-1);//把从右到左第一个1变成0
}
System.out.println(count);
}
}
}
13.购物单
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();// 总钱数
int m = sc.nextInt();// 物品数
int c[] = new int[100];// 物品价格
int len = m;
int p[] = new int[m];// 物品重要度
int w[] = new int[100];// 物品价值=价格*重要度
int q[] = new int[m];// 主从关系
for (int i = 0; i < m; i++) {
c[i] = sc.nextInt();
p[i] = sc.nextInt();
w[i] = c[i] * p[i];
q[i] = sc.nextInt();
}
ArrayList<ArrayList<Integer>> list = new ArrayList();// list储存list1
//分组
for (int i = 0; i < m; i++) {
if (q[i] == 0) {
ArrayList<Integer> list1 = new ArrayList();// list1储存一组物品
list1.add(i);// 物品i为主件
int flag = 0;
for (int j = 0; j < m; j++) {
if (q[j] == i + 1) {
c[j] = c[j] + c[i];
w[j] = w[j] + w[i];
flag++;
list1.add(j);// 物品j为物品i的附件;
}
}
if(flag==2){//物品i有两件从属,则还有一个组员为同时选3件物品
c[len] = c[list1.get(list1.size()-1)]+c[list1.get(list1.size()-2)]-c[i];
w[len] = w[list1.get(list1.size()-1)]+w[list1.get(list1.size()-2)]-w[i];
list1.add(len);
len++;
}
list.add(list1);
}
}
//没有要求把钱正好花完,初始化全为0
int f[] = new int[n+1];
//分组背包
for(int k=0;k<list.size();k++){
for(int v=n;v>=0;v--)
for(int j=0;j<list.get(k).size();j++){
int i = list.get(k).get(j);
if(v>=c[i])
f[v] = Math.max(f[v], f[v-c[i]]+w[i]);
}
}
if(f[n]>0)
System.out.println(f[n]);
}
}
}
14.坐标移动
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
String[] s = str.split(";");
int len = s.length;
int x = 0, y = 0;
boolean flag = true;
for (int i = 0; i < len; i++) {
if (s[i].length() < 2)
flag = false;
else {
for (int j = 1; j < s[i].length(); j++) {
if (s[i].charAt(j) < '0' || s[i].charAt(j) > '9') {
flag = false;
break;
}
}
}
if (flag) {
char t = s[i].charAt(0);
switch (t) {
case 'A':
x -= Integer.parseInt(s[i].substring(1));
break;
case 'D':
x += Integer.parseInt(s[i].substring(1));
break;
case 'W':
y += Integer.parseInt(s[i].substring(1));
break;
case 'S':
y -= Integer.parseInt(s[i].substring(1));
break;
}
}
flag = true;//
}
System.out.println(x+","+y);
}
}
}
15.简单错误记录x
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Map<String, Integer> map=new LinkedHashMap<String, Integer>();
while(sc.hasNext()){
String str=sc.next();
int linenum=sc.nextInt();
String[] arr=str.split("\\\\"); //根据\切割
String s=arr[arr.length-1];
if(s.length()>16) //截取
s=s.substring(s.length()-16);
String key=s+" "+linenum;
int value=1;
if(map.containsKey(key))
map.put(key, map.get(key)+1);
else {
map.put(key, value);
}
}
int count=0;
for(String string:map.keySet()){
count++;
if(count>(map.keySet().size()-8)) //输出最后八个记录
System.out.println(string+" "+map.get(string));
}
}
}
16,密码验证合格程序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
boolean flag = false;
String str = in.nextLine();
char c[] = str.toCharArray();
if (str.length() > 8) {
int count[] = new int[4];
for (int i = 0; i < str.length(); i++) {
if (c[i] >= '0' && c[i] <= '9')
count[0] = 1;
else if (c[i] >= 'a' && c[i] <= 'z')
count[1] = 1;
else if (c[i] >= 'A' && c[i] <= 'Z')
count[2] = 1;
else
count[3] = 1;
}
if (count[0] + count[1] + count[2] + count[3] >= 3) {
if (isPassword(str)) {
System.out.println("OK");
flag = true;
}
}
}
if (!flag)
System.out.println("NG");
}
}
public static boolean isPassword(String str) {
for (int i = 0; i < str.length() - 2; i++) {
String s = str.substring(i, i + 3);
if (str.substring(i + 3).contains(s))
return false;
}
return true;
}
}
17.简单的密码破解
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.next();
StringBuffer s = new StringBuffer();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(c>='A'&c<'Z')
s.append((char)(c+1+32));//大写变小写加32
else if(c=='Z')
s.append('a');
else if(c>='a'&&c<='o')
s.append((c-'a')/3+2);
else if(c>='p'&&c<='s')
s.append(7);
else if(c>='t'&&c<='v')
s.append(8);
else if(c>='w'&&c<='z')
s.append(9);
else
s.append(c);
}
System.out.println(s);
}
}
}
18汽水瓶
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = 10;//最多包含10个数据
while(sc.hasNext()){
int n = sc.nextInt(); //小张手上有n个空汽水瓶
if(n==0)
break;
int max = 0;//最多可以换多少瓶汽水喝
while(n>2){
int p = n/3; //换p瓶汽水喝
n = n%3;
max = max+p;
n = n+p; //换完后的空瓶数
}
if(n==2)
System.out.println(max+1);
else
System.out.println(max);
}
}
}
19删除字符串中出现次数最少的字符
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
StringBuffer str = new StringBuffer(sc.nextLine());
HashMap<Character,Integer> map = new HashMap();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(map.containsKey(c))
map.put(c,map.get(c)+1);
else
map.put(c,1);
}
int minTime = map.get(str.charAt(0));
Collection values = map.values();
Iterator it = values.iterator();
while(it.hasNext()){
int value = (Integer)it.next();
if(minTime>value)
minTime = value;
}
for(int i=0;i<str.length();){
char c = str.charAt(i);
if(minTime==map.get(c)){
str.deleteCharAt(i);
}
else
i++;
}
System.out.println(str);
}
}
}
20.数据分类处理
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Long I = sc.nextLong();
ArrayList<Integer> list = new ArrayList();
TreeSet<Integer> set = new TreeSet();
for(int i=0;i<I;i++){
list.add(sc.nextInt());
}
Long R = sc.nextLong();
for(int i=0;i<R;i++){
set.add(sc.nextInt());
}
StringBuffer s = new StringBuffer();
Iterator it = set.iterator();
int count = 0;
while(it.hasNext()){
int r =(Integer) it.next();
ArrayList list1 = new ArrayList();
for(int j=0;j<list.size();j++){
int i = list.get(j);
if((i+"").contains((r+""))){
list1.add(j);
list1.add(i);
}
}
if(list1.size()>0){
s.append(r+" ");
s.append(list1.size()/2+" ");
count = count + 2;
for(int j=0;j<list1.size();j++){
s.append(list1.get(j)+" ");
count = count +1;
}
}
}
s.insert(0,(count+" "));
s.deleteCharAt(s.length()-1);
System.out.println(s);
}
}
}
21.字符串排序
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
int len = str.length();
String s = "";
ArrayList<Integer> list = new ArrayList();// 记录非英文字母字符的位置
for (int i = 0; i < len; i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
s = s + str.charAt(i);
} else
list.add(i);
}
StringBuffer result = new StringBuffer(compare(s));
for (int i = 0; i < list.size(); i++) {
result.insert(list.get(i), str.charAt(list.get(i)) + "");
}
System.out.println(result);
}
}
public static String compare(String s) {
char c[] = s.toCharArray();
for (int i = 0; i < c.length - 1; i++)
for (int j = 0; j < c.length - i - 1; j++) {
if ((c[j] + "").compareToIgnoreCase(c[j + 1] + "") > 0) {
char t = c[j];
c[j] = c[j + 1];
c[j + 1] = t;
}
}
return String.valueOf(c);
}
}
22。查找兄弟单词
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
String s[] = new String[n];
for(int i=0;i<n;i++){
s[i] = sc.next();
}
String str = sc.next();//待查找单词
int m = sc.nextInt();//查找第m个兄弟单词
ArrayList<String> list = new ArrayList();
for(int i=0;i<n;i++){
if(isBrother(s[i],str)&&(!s[i].equals(str))){
list.add(s[i]);
}
}
Collections.sort(list);
System.out.println(list.size()); //输出:2 换行 bca 。 而不能 2 bca
if(m-1>=0&&m<=list.size())
System.out.println(list.get(m-1));
}
}
public static boolean isBrother(String s1,String s2){
if(s1.length() == s2.length()){
for(int i=0;i<s1.length();i++){
s2 = s2.replaceFirst(s1.charAt(i)+"", "");
}
if(s2.length()==0)
return true;
else
return false;
}
else
return false;
}
}
23.字符串加解密
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str1 = sc.nextLine();
String str2 = sc.nextLine();
encrypt(str1);
unEncrypt(str2);
System.out.println(encrypt(str1));
System.out.println(unEncrypt(str2));
}
}
public static String encrypt(String aucPassword) {
String aucResult="";
for(int i=0;i<aucPassword.length();i++){
char c = aucPassword.charAt(i);
if(c>='a'&&c<='y'){
aucResult+=(char)(c-31)+"";
}
else if(c=='z')
aucResult+="A";
else if(c>='A'&&c<='Y'){
aucResult+=(char)(c+33)+"";
}
else if(c=='Z')
aucResult+="a";
else if(c>='0'&&c<='8'){
aucResult+=(char)(c+1)+"";
}
else if(c=='9')
aucResult+=0+"";
}
return aucResult;
}
public static String unEncrypt(String password) {
String result = "";
for(int i=0;i<password.length();i++){
char c = password.charAt(i);
if(c>='b'&&c<='z'){
result+=(char)(c-33)+"";
}
else if(c=='a')
result+="Z";
else if(c>='B'&&c<='Z'){
result+=(char)(c+31)+"";
}
else if(c=='A')
result+="z";
else if(c>='1'&&c<='9'){
result+=(char)(c-1)+"";
}
else if(c=='0')
result+="9";
}
return result;
}
}
24.单词倒排
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
if(str.length()>0){
//将非字母换成空格
for(int i=0;i<str.length();i++){
char t = str.charAt(i);
if(t<'A'||(t>'Z'&&t<'a')||t>'z'){
str = str.replace(t+""," ");
}
}
if(str.charAt(0)==' ')
str = str.replaceFirst(" ", ""); //第一个是空格则去掉
str = str.replace(" ", " ");//把两个空格换成一个空格
String s[] = str.split(" ");
StringBuffer result = new StringBuffer();
for(int i=0;i<s.length-1;i++){
StringBuffer temp = new StringBuffer(s[i]);
temp.reverse();
result.append(temp+" ");
}
result.append(new StringBuffer(s[s.length-1]).reverse());
System.out.println(result.reverse());
}
}
}
}
25.统计每个月兔子
import java.util.*;
//斐波拉契数列
//a(n)=a(n-1)+a(n-2);前一项上个月兔子,后一项生的新兔子
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int month = sc.nextInt();
if(month<=0)
System.out.println(0);
else
System.out.println(getTocalCount(month));
}
}
/*
public static int getTocalCount(int mothCount){
if(mothCount<=2)
return 1;
else
return getTocalCount(mothCount-1)+getTocalCount(mothCount-2);
}
*/
/*
public static int getTocalCount(int n){
if(n<1)
return 0;
if(n==1||n==2)
return 1;
int cur = 1;
int temp = 0;
int pre = 1;
for(int i=3;i<=n;i++){
temp = cur;
cur = cur + pre;
pre = temp;
}
return cur;
}
*/
public static int[][] matrixPower(int[][] matrix,int p){
int m = matrix.length;
int n = matrix[0].length;
int[][] res = new int[m][n];
int[][] temp = matrix;
for(;p!=0;p=p/2){
if(p%2!=0){
res = muliMatrix(res,temp);
}
temp = muliMatrix(temp,temp);
}
return res;
}
public static int[][] muliMatrix(int[][] m1,int[][] m2){
int m = m1.length;
int n = m2[0].length;
int[][] result = new int[m][n];
for(int a=0;a<m;a++){
for(int b=0;b<n;b++){
for(int c=0;c<m2.length;c++){
result[a][b] = result[a][b]+m1[a][c]*m2[c][b];
}
}
}
return result;
}
public static int getTocalCount(int n){
if(n<1)
return 0;
if(n==1||n==2)
return 1;
int[][] base = {{1,1},{1,0}};
int[][] res = matrixPower(base,n-2);
return res[0][0] + res[1][0];
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
TreeSet<Integer> set = new TreeSet();
for(int i=0;i<n;i++)
set.add(sc.nextInt());
//Object a[] = set.toArray();
//Arrays.sort(a);
for(Integer i : set)
System.out.println(i);
}
}
}
2.字符串分隔
import java.util.*;
public class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str1 = sc.nextLine();
int len1 = str1.length();
if (len1!= 0)
{
if (len1 % 8 < 8&&len1%8!=0) {
for (int i = 0; i < 8 - len1 % 8; i++)
str1 = str1 + "0";
}
len1 = str1.length();
for (int i = 0; i < len1 / 8; i++) {
String s = str1.substring(i * 8, 8 * (i + 1));
System.out.println(s);
}
}
}
}
}
3.进制转换
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String s = sc.nextLine();
s = s.substring(2);
System.out.println(Integer.valueOf(s,16));
}
}
}
4.质数因子
//一个合数必定可以因式分解为两个质数。
//此题相当于因式分解,因式分解后的因子都是质数。所以从2开始,一个个试即可,2,3,4,4=2*2
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Long n = sc.nextLong();
System.out.println(getResult(n));
}
}
public static String getResult(long ulDataInput){
String str = new String();
int index = 2;
while(ulDataInput>1){
if(ulDataInput%index==0){
str+=index+" ";
ulDataInput = ulDataInput/index;
}
else
index++;
}
return str;
}
}
5.取近似值
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextDouble()){
double a = sc.nextDouble();
int b = new Double(a).intValue();
if(a-b>=0.5)
System.out.println(b+1);
else
System.out.println(b);
}
}
}
6字符串最后一个单词长度
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String str = sc.nextLine();
System.out.println(str.length()-1-str.lastIndexOf(" "));
}
}
}
7.计算字符个数
8.合并表记录
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
TreeMap<Integer,Integer> map = new TreeMap();
while((n--)>0){
int index = sc.nextInt();
int value = sc.nextInt();
if(map.containsKey(index)){
map.put(index, map.get(index)+value);
}
else
map.put(index, value);
}
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key+" "+value);
}
}
}
9.提取不重复的整数
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
StringBuffer s = new StringBuffer(sc.nextLine());
s.reverse();
char a[] = s.toString().toCharArray();
ArrayList<Character> list = new ArrayList();
for(int i=0;i<a.length;i++){
if(!list.contains(a[i]))
list.add(a[i]);
}
int n=0;
for(int i=0;i<list.size();i++){
n = n*10+(list.get(i)-'0');
}
System.out.println(n);
}
}
}
10.字符个数统计
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String s = sc.nextLine();
ArrayList<Character> list = new ArrayList();
char a[] = s.toCharArray();
for(int i=0;i<a.length;i++){
if((int)a[i]>=0&&(int)a[i]<=127){
if(!list.contains(a[i]))
list.add(a[i]);
}
}
System.out.println(list.size());
}
}
}
11. 字串的连接最长路径查找
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
int n = Integer.parseInt(sc.nextLine());
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = sc.nextLine();
}
//冒泡排序
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++){
if(str[j].compareTo(str[j+1])>0)
{
String temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
for(int i=0;i<n;i++){
System.out.println(str[i]);
}
}
}
}
12.求int型数据在内存中存储时1的个数
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int count = 0;
while(n!=0){
count++;
n = n&(n-1);//把从右到左第一个1变成0
}
System.out.println(count);
}
}
}
13.购物单
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();// 总钱数
int m = sc.nextInt();// 物品数
int c[] = new int[100];// 物品价格
int len = m;
int p[] = new int[m];// 物品重要度
int w[] = new int[100];// 物品价值=价格*重要度
int q[] = new int[m];// 主从关系
for (int i = 0; i < m; i++) {
c[i] = sc.nextInt();
p[i] = sc.nextInt();
w[i] = c[i] * p[i];
q[i] = sc.nextInt();
}
ArrayList<ArrayList<Integer>> list = new ArrayList();// list储存list1
//分组
for (int i = 0; i < m; i++) {
if (q[i] == 0) {
ArrayList<Integer> list1 = new ArrayList();// list1储存一组物品
list1.add(i);// 物品i为主件
int flag = 0;
for (int j = 0; j < m; j++) {
if (q[j] == i + 1) {
c[j] = c[j] + c[i];
w[j] = w[j] + w[i];
flag++;
list1.add(j);// 物品j为物品i的附件;
}
}
if(flag==2){//物品i有两件从属,则还有一个组员为同时选3件物品
c[len] = c[list1.get(list1.size()-1)]+c[list1.get(list1.size()-2)]-c[i];
w[len] = w[list1.get(list1.size()-1)]+w[list1.get(list1.size()-2)]-w[i];
list1.add(len);
len++;
}
list.add(list1);
}
}
//没有要求把钱正好花完,初始化全为0
int f[] = new int[n+1];
//分组背包
for(int k=0;k<list.size();k++){
for(int v=n;v>=0;v--)
for(int j=0;j<list.get(k).size();j++){
int i = list.get(k).get(j);
if(v>=c[i])
f[v] = Math.max(f[v], f[v-c[i]]+w[i]);
}
}
if(f[n]>0)
System.out.println(f[n]);
}
}
}
14.坐标移动
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
String[] s = str.split(";");
int len = s.length;
int x = 0, y = 0;
boolean flag = true;
for (int i = 0; i < len; i++) {
if (s[i].length() < 2)
flag = false;
else {
for (int j = 1; j < s[i].length(); j++) {
if (s[i].charAt(j) < '0' || s[i].charAt(j) > '9') {
flag = false;
break;
}
}
}
if (flag) {
char t = s[i].charAt(0);
switch (t) {
case 'A':
x -= Integer.parseInt(s[i].substring(1));
break;
case 'D':
x += Integer.parseInt(s[i].substring(1));
break;
case 'W':
y += Integer.parseInt(s[i].substring(1));
break;
case 'S':
y -= Integer.parseInt(s[i].substring(1));
break;
}
}
flag = true;//
}
System.out.println(x+","+y);
}
}
}
15.简单错误记录x
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Map<String, Integer> map=new LinkedHashMap<String, Integer>();
while(sc.hasNext()){
String str=sc.next();
int linenum=sc.nextInt();
String[] arr=str.split("\\\\"); //根据\切割
String s=arr[arr.length-1];
if(s.length()>16) //截取
s=s.substring(s.length()-16);
String key=s+" "+linenum;
int value=1;
if(map.containsKey(key))
map.put(key, map.get(key)+1);
else {
map.put(key, value);
}
}
int count=0;
for(String string:map.keySet()){
count++;
if(count>(map.keySet().size()-8)) //输出最后八个记录
System.out.println(string+" "+map.get(string));
}
}
}
16,密码验证合格程序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
boolean flag = false;
String str = in.nextLine();
char c[] = str.toCharArray();
if (str.length() > 8) {
int count[] = new int[4];
for (int i = 0; i < str.length(); i++) {
if (c[i] >= '0' && c[i] <= '9')
count[0] = 1;
else if (c[i] >= 'a' && c[i] <= 'z')
count[1] = 1;
else if (c[i] >= 'A' && c[i] <= 'Z')
count[2] = 1;
else
count[3] = 1;
}
if (count[0] + count[1] + count[2] + count[3] >= 3) {
if (isPassword(str)) {
System.out.println("OK");
flag = true;
}
}
}
if (!flag)
System.out.println("NG");
}
}
public static boolean isPassword(String str) {
for (int i = 0; i < str.length() - 2; i++) {
String s = str.substring(i, i + 3);
if (str.substring(i + 3).contains(s))
return false;
}
return true;
}
}
17.简单的密码破解
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.next();
StringBuffer s = new StringBuffer();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(c>='A'&c<'Z')
s.append((char)(c+1+32));//大写变小写加32
else if(c=='Z')
s.append('a');
else if(c>='a'&&c<='o')
s.append((c-'a')/3+2);
else if(c>='p'&&c<='s')
s.append(7);
else if(c>='t'&&c<='v')
s.append(8);
else if(c>='w'&&c<='z')
s.append(9);
else
s.append(c);
}
System.out.println(s);
}
}
}
18汽水瓶
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = 10;//最多包含10个数据
while(sc.hasNext()){
int n = sc.nextInt(); //小张手上有n个空汽水瓶
if(n==0)
break;
int max = 0;//最多可以换多少瓶汽水喝
while(n>2){
int p = n/3; //换p瓶汽水喝
n = n%3;
max = max+p;
n = n+p; //换完后的空瓶数
}
if(n==2)
System.out.println(max+1);
else
System.out.println(max);
}
}
}
19删除字符串中出现次数最少的字符
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
StringBuffer str = new StringBuffer(sc.nextLine());
HashMap<Character,Integer> map = new HashMap();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(map.containsKey(c))
map.put(c,map.get(c)+1);
else
map.put(c,1);
}
int minTime = map.get(str.charAt(0));
Collection values = map.values();
Iterator it = values.iterator();
while(it.hasNext()){
int value = (Integer)it.next();
if(minTime>value)
minTime = value;
}
for(int i=0;i<str.length();){
char c = str.charAt(i);
if(minTime==map.get(c)){
str.deleteCharAt(i);
}
else
i++;
}
System.out.println(str);
}
}
}
20.数据分类处理
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Long I = sc.nextLong();
ArrayList<Integer> list = new ArrayList();
TreeSet<Integer> set = new TreeSet();
for(int i=0;i<I;i++){
list.add(sc.nextInt());
}
Long R = sc.nextLong();
for(int i=0;i<R;i++){
set.add(sc.nextInt());
}
StringBuffer s = new StringBuffer();
Iterator it = set.iterator();
int count = 0;
while(it.hasNext()){
int r =(Integer) it.next();
ArrayList list1 = new ArrayList();
for(int j=0;j<list.size();j++){
int i = list.get(j);
if((i+"").contains((r+""))){
list1.add(j);
list1.add(i);
}
}
if(list1.size()>0){
s.append(r+" ");
s.append(list1.size()/2+" ");
count = count + 2;
for(int j=0;j<list1.size();j++){
s.append(list1.get(j)+" ");
count = count +1;
}
}
}
s.insert(0,(count+" "));
s.deleteCharAt(s.length()-1);
System.out.println(s);
}
}
}
21.字符串排序
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
int len = str.length();
String s = "";
ArrayList<Integer> list = new ArrayList();// 记录非英文字母字符的位置
for (int i = 0; i < len; i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
s = s + str.charAt(i);
} else
list.add(i);
}
StringBuffer result = new StringBuffer(compare(s));
for (int i = 0; i < list.size(); i++) {
result.insert(list.get(i), str.charAt(list.get(i)) + "");
}
System.out.println(result);
}
}
public static String compare(String s) {
char c[] = s.toCharArray();
for (int i = 0; i < c.length - 1; i++)
for (int j = 0; j < c.length - i - 1; j++) {
if ((c[j] + "").compareToIgnoreCase(c[j + 1] + "") > 0) {
char t = c[j];
c[j] = c[j + 1];
c[j + 1] = t;
}
}
return String.valueOf(c);
}
}
22。查找兄弟单词
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
String s[] = new String[n];
for(int i=0;i<n;i++){
s[i] = sc.next();
}
String str = sc.next();//待查找单词
int m = sc.nextInt();//查找第m个兄弟单词
ArrayList<String> list = new ArrayList();
for(int i=0;i<n;i++){
if(isBrother(s[i],str)&&(!s[i].equals(str))){
list.add(s[i]);
}
}
Collections.sort(list);
System.out.println(list.size()); //输出:2 换行 bca 。 而不能 2 bca
if(m-1>=0&&m<=list.size())
System.out.println(list.get(m-1));
}
}
public static boolean isBrother(String s1,String s2){
if(s1.length() == s2.length()){
for(int i=0;i<s1.length();i++){
s2 = s2.replaceFirst(s1.charAt(i)+"", "");
}
if(s2.length()==0)
return true;
else
return false;
}
else
return false;
}
}
23.字符串加解密
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str1 = sc.nextLine();
String str2 = sc.nextLine();
encrypt(str1);
unEncrypt(str2);
System.out.println(encrypt(str1));
System.out.println(unEncrypt(str2));
}
}
public static String encrypt(String aucPassword) {
String aucResult="";
for(int i=0;i<aucPassword.length();i++){
char c = aucPassword.charAt(i);
if(c>='a'&&c<='y'){
aucResult+=(char)(c-31)+"";
}
else if(c=='z')
aucResult+="A";
else if(c>='A'&&c<='Y'){
aucResult+=(char)(c+33)+"";
}
else if(c=='Z')
aucResult+="a";
else if(c>='0'&&c<='8'){
aucResult+=(char)(c+1)+"";
}
else if(c=='9')
aucResult+=0+"";
}
return aucResult;
}
public static String unEncrypt(String password) {
String result = "";
for(int i=0;i<password.length();i++){
char c = password.charAt(i);
if(c>='b'&&c<='z'){
result+=(char)(c-33)+"";
}
else if(c=='a')
result+="Z";
else if(c>='B'&&c<='Z'){
result+=(char)(c+31)+"";
}
else if(c=='A')
result+="z";
else if(c>='1'&&c<='9'){
result+=(char)(c-1)+"";
}
else if(c=='0')
result+="9";
}
return result;
}
}
24.单词倒排
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
if(str.length()>0){
//将非字母换成空格
for(int i=0;i<str.length();i++){
char t = str.charAt(i);
if(t<'A'||(t>'Z'&&t<'a')||t>'z'){
str = str.replace(t+""," ");
}
}
if(str.charAt(0)==' ')
str = str.replaceFirst(" ", ""); //第一个是空格则去掉
str = str.replace(" ", " ");//把两个空格换成一个空格
String s[] = str.split(" ");
StringBuffer result = new StringBuffer();
for(int i=0;i<s.length-1;i++){
StringBuffer temp = new StringBuffer(s[i]);
temp.reverse();
result.append(temp+" ");
}
result.append(new StringBuffer(s[s.length-1]).reverse());
System.out.println(result.reverse());
}
}
}
}
25.统计每个月兔子
import java.util.*;
//斐波拉契数列
//a(n)=a(n-1)+a(n-2);前一项上个月兔子,后一项生的新兔子
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int month = sc.nextInt();
if(month<=0)
System.out.println(0);
else
System.out.println(getTocalCount(month));
}
}
/*
public static int getTocalCount(int mothCount){
if(mothCount<=2)
return 1;
else
return getTocalCount(mothCount-1)+getTocalCount(mothCount-2);
}
*/
/*
public static int getTocalCount(int n){
if(n<1)
return 0;
if(n==1||n==2)
return 1;
int cur = 1;
int temp = 0;
int pre = 1;
for(int i=3;i<=n;i++){
temp = cur;
cur = cur + pre;
pre = temp;
}
return cur;
}
*/
public static int[][] matrixPower(int[][] matrix,int p){
int m = matrix.length;
int n = matrix[0].length;
int[][] res = new int[m][n];
int[][] temp = matrix;
for(;p!=0;p=p/2){
if(p%2!=0){
res = muliMatrix(res,temp);
}
temp = muliMatrix(temp,temp);
}
return res;
}
public static int[][] muliMatrix(int[][] m1,int[][] m2){
int m = m1.length;
int n = m2[0].length;
int[][] result = new int[m][n];
for(int a=0;a<m;a++){
for(int b=0;b<n;b++){
for(int c=0;c<m2.length;c++){
result[a][b] = result[a][b]+m1[a][c]*m2[c][b];
}
}
}
return result;
}
public static int getTocalCount(int n){
if(n<1)
return 0;
if(n==1||n==2)
return 1;
int[][] base = {{1,1},{1,0}};
int[][] res = matrixPower(base,n-2);
return res[0][0] + res[1][0];
}
}