倒计时11

打卡蓝桥杯倒计时…其实也是从今天开始学…不怕!冲鸭!
一、十六进制转八进制(RW)

/**
*补充StringBuilder与String的区别
*/
public static void main(String[] args) {
		String str="刘德华_java程序员_布鲁明顿";
		char[] str1=str.toCharArray();
		StringBuilder sb=new StringBuilder();
		sb.append(str1);
		sb.delete(8, 11);
		sb.insert(8, "高级攻城狮");
		sb.replace(4, 8, "ios");
		sb.reverse();
		String s1=sb.toString().toLowerCase();
		System.out.println(s1);
	}

import java.util.Scanner;

/**
 * 每个十六进制数长度不超过100000。这句话说明该十六进制数有可能是一个长度为100000的数,
 * 这不是long long 类型变量所能表示的,
 * 做大数运算的常用手段就是字符串变量,这里我们使用字符串变量来表示对应的二进制数,
 * */
public class j_t2 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		while(n>0){
			String t=sc.next();
			long beg=System.currentTimeMillis();
			StringBuilder temp=tobin(t);
//			System.out.println(temp.toString());
			System.out.println(toOr(temp.toString()));
			long end=System.currentTimeMillis();
			System.out.println(end-beg+" ****");
			
		}
	}
	/**
	 * 十六进制转成二进制
	 * @return 
	 * */
	public static StringBuilder tobin(String str){
		StringBuilder str2 = new StringBuilder();      //StringBuilder用来申明可变字符串
		for(int i=0;i<str.length();i++){
			char c = str.charAt(i);
			switch(c){
			case '0':
				str2.append("0000");break;
			case '1':
				str2.append("0001");break;
			case '2':
				str2.append("0010");break;
			case '3':
				str2.append("0011");break;
			case '4':
				str2.append("0100");break;
			case '5':
				str2.append("0101");break;
			case '6':
				str2.append("0110");break;
			case '7':
				str2.append("0111");break;
			case '8':
				str2.append("1000");break;
			case '9':
				str2.append("1001");break;
			case 'A':
				str2.append("1010");break;
			case 'B':
				str2.append("1011");break;
			case 'C':
				str2.append("1100");break;
			case 'D':
				str2.append("1101");break;
			case 'E':
				str2.append("1110");break;
			case 'F':
				str2.append("1111");break;
			default:break;
			}
		}
		return str2;
	}
	
	/**
	 * 二进制转八进制
	 * */
	public static int toOr(String temp){
		int res=0;;
		if(temp.length()%3==1){
			res=(int) ((temp.charAt(0)-'0')*Math.pow(10, (temp.length()-1)/3));
//			System.out.println(res);
			temp=temp.substring(1);
		}else if(temp.length()%3==2){
			res=(int) ((temp.charAt(0)-'0')*Math.pow(2, 1)+(temp.charAt(1)-'0'));
//			System.out.println(res);
			res=(int) (res*Math.pow(10, (temp.length()-1)/3));
			temp=temp.substring(2);
		}
		while(temp.length()/3>0){
			int t=temp.length();
			int tt=(int) ((temp.charAt(0)-'0')*Math.pow(2, 2)+(temp.charAt(1)-'0')*Math.pow(2, 1)+(temp.charAt(2)-'0'));
			tt=(int) (tt*Math.pow(10,(temp.length()-1)/3));
			int ts=Integer.parseInt(String .valueOf(res));
			int sum=tt+ts;
			res=sum;
//			System.out.println(res);
			temp=temp.substring(3);
		}
		return res;
	}
	
	
	/**
	 * 二进制转成十进制
	 * */
	public static int toOTen(String temp){
		int res=0;;
		if(temp.length()%3==1){
			res=(int) ((temp.charAt(0)-'0')*Math.pow(2, temp.length()-1));
//			System.out.println(res);
			temp=temp.substring(1);
		}else if(temp.length()%3==2){
			res=(int) ((temp.charAt(0)-'0')*Math.pow(2, temp.length()-1)+(temp.charAt(1)-'0')*Math.pow(2, temp.length()-2));
//			System.out.println(res);
			temp=temp.substring(2);
		}
		while(temp.length()/3>0){
			int t=temp.length();
			int tt=(int) ((temp.charAt(0)-'0')*Math.pow(2, t-1)+(temp.charAt(1)-'0')*Math.pow(2, t-2)+(temp.charAt(2)-'0')*Math.pow(2, t-3));
			
			int ts=Integer.parseInt(String .valueOf(res));
			int sum=tt+ts;
			res=sum;
//			System.out.println(res);
			temp=temp.substring(3);
		}
		return res;
	}
}

二、特殊的回文数(WA)
问题描述
  123321是一个非常特殊的数,它从左边读和从右边读是一样的。
  输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入格式
  输入一行,包含一个正整数n。
输出格式
  按从小到大的顺序输出满足条件的整数,每个整数占一行。
样例输入
52
样例输出
899998
989989
998899
数据规模和约定
  1<=n<=54。

import java.util.Scanner;


public class j_t9 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		f(n);
		s(n);
	}
	/**
	 * 五位回文数
	 * */
	public static void f(int n){
		for(int i=1;i<=9;i++){
			for(int j=0;j<=9;j++){
				for(int k=0;k<=9;k++){
					if(n==(i*2)+(j*2)+k){
						System.out.println(i*10001+j*1010+k*100);
					}
				}
			}
		}
	}
	/**
	 * 六位回文数
	 * */
	public static void s(int n){
		for(int i=1;i<=9;i++){
			for(int j=0;j<=9;j++){
				for(int k=0;k<=9;k++){
					if(n==(i*2)+(j*2)+(k*2)){
						System.out.println(i*100001+j*10010+k*1100);
					}
				}
			}
		}
	}
}

三、杨辉三角形(WA和正确)
杨辉三角又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。  
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
下面给出了杨辉三角形的前4行:
  
1 
1 1 
1 2 1 
1 3 3 1
 
给出n,输出它的前n行。
1、这是错误的答案,关键在于数组溢出,我想用排列组合来做,但是在求n!的时候数组溢出了,后来发现如果想求n个选m个,可以直接用n*(n-1)*。。。(n-m+1)/m!,同时m与n-m相等,如果求 100个选98个,相当于100个选2个 。但是我在溢出(对于long,21行之后溢出)之后就没再做尝试,而是改用了二维数组

import java.util.Scanner;
/**
 * byte的取值范围为-128~127, short的取值范围为-32768~32767, int的取值范围为(-2147483648~2147483647),
 * long的取值范围为(-9223372036854774808~9223372036854774807)
 * 
 * 简记 byte 128 short 30000  (3+4个0)int 2000000000 (2+9个0) long(9+19个0)
 * */

/**
 * 每i层的数字 符合c i-1 0,c i-1 1,...,c i-1 i-1
 * */
public class j_t6_WA {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n=sc.nextInt();
		for(long i=0;i<n;){
			c(i);
			i++;
			if(i<n){
				System.out.println();
			}
		}
	}
	/**
	 * 选择C nm
	 * */
	public static void c(long n){
		if(n==0){
			System.out.print(1);
		}else{
			long b=jc(n);
			for(long i=0;i<n;i++){
				long a=jc(i);
				long c=jc(n-i);
				System.out.print(b/(a*c)+" ");
			}
			
			System.out.print(1);
		}
		
	}
	/**
	 * 进行阶乘计算
	 * */
	public static long jc(long n){
		if(n==0){
			return 1;
		}else{
			long mul=1;
			for(long i=2;i<=n;i++){
				mul*=i;
			}
			return mul;
		}
	}
	
}

2、正确答案

import java.util.Scanner;

public class j_t6 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		int[][] a = new int[n][n];
		
		//i用来控制行,j用来控制列
		for (int i = 0; i < n; i++) {
			for(int j=0; j<=i; j++){
				//两种情况,如果是两边的元素,值为1,如果是中间的元素,值位两数之和
				if(j==0 || i==j){
					a[i][j] = 1;
				}else{
					a[i][j] = a[i-1][j-1] + a[i-1][j];
				}
			}
		}
		
		for(int i=0; i<n; i++){
			for(int j=0; j<=i; j++){
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
	}
}


四、01字串(正确)
问题描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

00000
00001
00010
00011
00100

请按从小到大的顺序输出这32种01串。

本以为和全排列一样,没想到这么简单…打扰了…
给出了全排列的代码(万能!记住!)


public class t2 {
	public static void main(String[] args) {
		int []a={1,2,3};
//		qpl(a,0);
		c_01();
	}
	
	/**
	 * 输出01串
	 * */
	public static void c_01(){
		for(int i = 0;i<=1;i++){
            for(int j=0;j<=1;j++){
                for(int k=0;k<=1;k++){
                    for(int p=0;p<=1;p++){
                        for(int z=0;z<=1;z++){
                        System.out.print(i);
                        System.out.print(j);
                        System.out.print(k);
                        System.out.print(p);
                        System.out.println(z);
                        }
                         
                    }
                }
            }
        }
	}
	
	
	
	/**
	 * 全排列
	 * 1234
	 * 1243
	 * 1324
	 * 1342
	 * ...
	 * */
	public static void qpl(int []a,int n){
		if(n==a.length){
			for(int i=0;i<n;i++){
				System.out.print(a[i]+" ");
			}
			System.out.println();
		}else{
			for(int i=n;i<a.length;i++){
				swap(a,i,n);
//				qpl(a,i+1);
				qpl(a,n+1);
				swap(a,i,n);
			}
		}
	}
	public static void swap(int []a,int i,int j){
		int t=a[i];
		a[i]=a[j];
		a[j]=t;
	}
}

五、审美课(正确和超时)
1.第一个超时1.546s,问题在于for循环用的太多

package sfxl;
/**
 * 1.4s
 * */
import java.util.Scanner;
public class t194_w {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int num[][]=new int[n][m];
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				num[i][j]=sc.nextInt();
			}
		}
		int []test=new int[m];
		int res=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(num[i][j]==0){
					test[j]=1;
				}else{
					test[j]=0;
				}
			}
			int temp=find(num, test);
			if(temp>res){
				res=temp;
			}
			
		}
		System.out.println(res);
		
	}
	/**
	 * 查找在num[][]里有多少个和test[]一样的
	 * */
	public static int find(int [][]num,int[]test){
		int nn=0;
		for(int i=0;i<num.length;i++){
			int flag=1;
			for(int j=0;j<num[0].length;j++){
				if(test[j]==num[i][j]){
				}else{
					flag=0;
				}
			}
			if(flag==1){
				nn++;
			}
		}
		return nn;
	}
}

2、第二个超时,问题不知道(1.031s),
但是学到了!!!可以将01转成十进制,再进行对比。输入数据比较多的情况下,可以换成读取字符流来获取数组。kk << 1kk左移一位,表示*2,因为乘法最后编译为二进制代码时,也是编译成移位操作,所以左移比较快点。map.containsKey(kk)判断集合中是否存在kk,如果存在,返回true,否则返回false。
上尖(Java运算符),意思是如果相对应位值相同,则结果为0,否则为1;例如001110=111。

流操作:(read = reader.read()) != -1判断是否到达文件结尾。Character.isDigit(read)判断字符是否是数字。

package sfxl;
/**
 * 1.046s
 * */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class t194_w2 {
	
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	
	public static void main(String[] args) throws IOException {
		
//		Scanner sc=new Scanner(System.in);
//		int n=sc.nextInt();
//		int m=sc.nextInt();
		
		int n=nextInt();
		int m=nextInt();
		
		int nu[]=new int[n];
		int inu[]=new int[n];
		int nn;
		for(int i=0;i<n;i++){
			nu[i]=0;
			for(int j=0;j<m;j++){
//				nn=sc.nextInt();
				nn=nextInt();
				nu[i]+=nn<<j;
//				inu[i]+=Math.abs((1-nn))*Math.pow(2, j);
				inu[i]+=Math.abs((1-nn))<<j;
			}
		}
		
		int []res=new int[n];
		int max=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(nu[j]==inu[i]){
					res[i]++;
				}
			}
			if(max<res[i]){
				max=res[i];
			}
		}
		System.out.println(max);
//		for(int i=0;i<n;i++){
//			System.out.print(res[i]+" ");
//		}
	}
	 private static int nextInt() throws IOException {
	        in.nextToken();
	        return (int) in.nval;
	    }
}

3.right(但是我很蒙…)

package sfxl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class t194 {
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static void main(String[] args) throws IOException {
        int n = nextInt(), m = nextInt(), count = 0, answer[][] = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                answer[i][j] = nextInt();
        
        
        int max = (1 << m) - 1,shi[] = new int[max + 1];  
//        System.out.println(max);
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int j = 0; j < m; j++) {
                sum = (sum << 1) + answer[i][j];
            }
            shi[sum]++;
        }
        for (int x = 0; x < shi.length; x++) {
            if (shi[x] != 0) {
                int y = x ^ max;
                count += shi[y] * shi[x];
            }
        }
        System.out.print(count / 2);
    }

    private static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值