为什么不对呢

游酷盛世笔试第三题

感觉已经使出了十八般武艺通过率还是0。。。

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
	  Scanner in = new Scanner(System.in);
	  int n = in.nextInt();
	  int[] vals = new int[n+1];
	  for(int i=1; i<=n; i++){
		  vals[i] = in.nextInt();
	  }
	  Map<Integer, Set<Integer>> mp = new HashMap<>();
	  for(int i=0; i<n-1; i++){
		  int t1 = in.nextInt();
		  int t2 = in.nextInt();
		  if(mp.get(t1)==null){
			  Set<Integer> st = new HashSet<>();
			  st.add(t2);
			  mp.put(t1, st);
		  }else{
			  Set<Integer> st = mp.get(t1);
			  st.add(t2);
		  }

		  if(mp.get(t2)==null){
			  Set<Integer> st = new HashSet<>();
			  st.add(t1);
			  mp.put(t2, st);
		  }else{
			  Set<Integer> st = mp.get(t2);
			  st.add(t1);
		  }
	  }

	  Deque<Integer> que = new LinkedList<>();
	  que.offerLast(1);
	  while(!que.isEmpty()){
		  int t = que.pollFirst();
		  if(mp.get(t)!=null){
			  for(int x:mp.get(t)){
				  Set<Integer> st = mp.get(x);
				  if(st!=null){
					  st.remove(t);
				  }
				  que.offerLast(x);
			  }
		  }
	  }

	  int q = in.nextInt();
	  while(q-->0){
		  int x = in.nextInt(), y = in.nextInt();
		  dost(vals, mp, x, y);
	  }

	  long[] res = new long[n+1];
	  int[] cnt = new int[n+1];
	  Arrays.fill(res, -1);
	  countMul(res, cnt, vals, mp, 1);
	  StringBuilder sb = new StringBuilder();
	  for(int i=1; i<n; i++){
		  // System.out.println(vals[i]+","+res[i]);
		  sb.append(cnt[i]);
		  sb.append(" ");
	  }
	  // System.out.println(vals[n]+","+res[n]);
	  sb.append(cnt[n]);

	  System.out.println(sb.toString());

  }

  public static int countZero(long[] a, int x){
	  String s = Long.toString(a[x]);
	  int sz = s.length();

	  int res = 0;
	  for(int i=0; i<sz; i++){
		  if(s.charAt(sz-i-1)=='0'){
			  res++;
			  a[x]/=10;
		  }else{
			  break;
		  }
	  }
	  return res;
  }

  public static void countMul(long[] res, int[] cnt, int[] vals, Map<Integer, Set<Integer>> mp, int x){
	  res[x] = vals[x];
	  cnt[x] += countZero(res, x);

	  if(mp.get(x)!=null){
		  for(int t:mp.get(x)){
			  if(res[t]==-1){
				  countMul(res, cnt, vals, mp, t);
			  }
			  res[x] *= res[t];
			  cnt[x] += countZero(res, x)+cnt[t];
		  }
	  }
  }

  public static void dost(int[] vals, Map<Integer, Set<Integer>> mp, int x, int y){
	  vals[x] *= y;
	  if(mp.get(x)!=null){
		  for(int t:mp.get(x)){
			  dost(vals, mp, t, y);
		  }
	  }
	  return;
  }
}

#技术岗笔试题求解##笔试#
全部评论
我在每个节点存了因数2、5的数量,然后dfs统计。过了80%
点赞 回复 分享
发布于 05-15 14:45 湖北
a1.8/3,挂啦
点赞 回复 分享
发布于 04-25 11:57 天津
哎呀,笔试题真的有点让人头大呢,特别是像游酷盛世这样的题目。看起来你已经很努力了,但是通过率还是0,这确实有点让人沮丧。不过别灰心,有时候难题就是要多思考、多尝试才能找到解决的办法哦! 关于你提到的第三题,我虽然不能直接帮你解答,但是可以陪你一起探讨。如果你愿意的话,我们可以私信聊聊,或许我能给你提供一些解题的思路或者小技巧呢~ 点击我的头像,我们可以开始私信聊天哦! 对了,你有没有想过,这道题的关键点可能在哪里?我们可以从题目要求或者给出的数据结构入手思考。你觉得呢?🐮🍀
点赞 回复 分享
发布于 04-17 21:07 AI生成

相关推荐

评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客企业服务