算法题24-里面3道题

计算:
题目描述
求路径,DFS算法
输入描述
“.”表示通,“X”表示不通
输出描述

示例1:
输入
5
…X.X
XX…
输出
2
        

代码:

import java.util.Scanner;
public class Main {
	static int count=0;
	public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	while (in.hasNextLine()) {
        	String n0=in.nextLine();
    		int n=Integer.valueOf(n0);
    		String str1=in.nextLine();
    		String str2=in.nextLine();
    		int[][] flag=new int[2][str1.length()];
    		for(int i=0;i<2;i++) {
    			for(int j=0;j<str1.length();j++) {
    				flag[i][j]=0;
    			}
    		}
    		dfs(n,0,0,str1,str2,flag);
    		if(count>0) {
    			System.out.print(count);
    		}else {
    			System.out.print(-1);
    		}
    	}	
	}
	public static void dfs(int n1,int x,int y,String s1,String s2,int[][] fl) {
		if(x<0||y<0||x>=2||y>=n1) {
			return;
		}
		if(x==0?(s1.charAt(y)=='X'):(s2.charAt(y)=='X')) {
			return;
		}
		if(x==1&&y==n1-1) {
			count++;
		}
		fl[x][y]=1;
		dfs(n1,x,y+1,s1,s2,fl);
		dfs(n1,x+1,y+1,s1,s2,fl);
		dfs(n1,x-1,y+1,s1,s2,fl);
		fl[x][y]=0;
	}
}
/**
5
..X.X
XX...
2
**/

题目描述
求众数

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
	static int count=0;
	public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	while (in.hasNextInt()) {
        	int n=in.nextInt();
        	int x0=in.nextInt();
        	int[] arr=new int[n];
    		for(int i=0;i<n;i++) {
    			arr[i]=in.nextInt();
    		}
    		int res=zs(arr, n, x0);
    		System.out.print(res);  		
    	}	
	}
	public static int zs(int[] a,int nn,int xx) {
		if(nn==1)return a[0];
		Map<Integer,Integer> map=new HashMap<>();
		for(int i=0;i<nn;i++) {
			Integer v=map.get(a[i]);
			map.put(a[i],v==null?1:v+1);
			
		}
		//int nm=map.size();
		Collection<Integer> c=map.values();
		Object[] obj=c.toArray();
		Arrays.sort(obj);
		int maxv=(int) obj[obj.length-1];
		int key=0;
		for(Map.Entry<Integer, Integer> entry:map.entrySet()) {
			if(maxv==entry.getValue()) {
				key=entry.getKey();
			}
		}
		return key;			
	}	
}
/**
5 2
3 1 3 2 5

3**/

题目描述
求众数
输入描述
n,k,L,R
n是长度;
k数组所有数是k的倍数;
L和R之间
输出描述
输出对1e9+7取模

import java.util.Scanner;
public class Main {
	static int count=0;
	public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	while (in.hasNextInt()) {
        	int n=in.nextInt();
        	int k=in.nextInt();
        	int l=in.nextInt();
        	int r=in.nextInt();
        	int num=0;
        	int res=1;
        	int n1=1;
        	while(l<=n1*k&&n1*k<=r) {
        		n1++;
        		num++;
        	}
        	
        	while(n>0) {
        		res*=num;
        		n--;
        	}
        	int aa=(int) (res%(1e9+7));
        	System.out.print(aa);
    	}
	}
}
/*9 1 1 3
19683
1e9+7*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值