京东JAVA笔试两题

2017-09-10  本文已影响35人  icecrea
网络上找的图片

通过队列实现 queue.size获得对应的总数大小 进行相应的入队出队操作

public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                String s=sc.nextLine();
                function(s);
            }
    }

    private static void function(String s){
        Queue queue=new LinkedList<>();
        int count=1;
        char[] chars=s.toCharArray();
        queue.add(chars[0]);
        for(int i=1;i<chars.length;i++){
            if(chars[i]=='(')
                queue.add(chars[i]);
            else{
                count=count*queue.size();
                queue.poll();
            }
        }
        System.out.println(count);
    }
}
public class Main{


    public final static long MOD=1000000000+7;
    
    public static long gcd(long a,long b){//最大公约数
        return (a%b==0)?b:gcd(b,a%b);
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            long n=sc.nextLong();
            long ans=(long)1*n*(n*2-1)%MOD;
            Set<Integer> set=new HashSet<>();
            for(int i=2;i*i<=n;i++){
                if(set.contains(i))
                    continue;
                long tmp=i;
                int cnt=0;
                while(tmp<=n){
                    set.add((int)tmp);
                    tmp=tmp*i;
                    cnt++;
                }
                for(int k=1;k<=cnt;k++){
                    for(int j=k+1;j<=cnt;j++)
                        ans=(ans+n/(j/gcd(k,j))*(long)2)%MOD;
                }
            }
            System.out.println(ans);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读