全部评论
第一题一样 public static void main(String[] args){
Scanner in = new Scanner(System.in);
String[] A = in.nextLine().split(" ");
String[] B = in.nextLine().split(" ");
int target = in.nextInt();
int[] sons = new int[A.length];
int[] parents = new int[B.length];
for (int i=0;i<A.length;i++){
sons[i] = Integer.valueOf(A[i]);
parents[i] = Integer.valueOf(B[i]);
}
Queue<Integer> queue = new LinkedList<>();
queue.offer(target);
int sum = 0;
while (!queue.isEmpty()){
int par = queue.poll();
for (int i=0;i<parents.length;i++){
if (parents[i] == par){
queue.offer(sons[i]);
sum++;
}
}
}
if (sum == 0) {
for (int i=0;i<sons.length;i++){
if (sons[i] == target){
sum++;
}
}
System.out.println(sum);
} else {
System.out.println(sum + 1);
}
}
第二题,随便写的渣渣代码 def judge(a): b=[i for i in range(1,a+1)] flag=0 while len(b)>=5: c=b[4] b=b[5:]+b[:4] flag+=1 if c==a: return flag if len(b)==4: c=b[0] b=b[1:] flag+=1 if c==a: return flag if len(b)==3: c=b[1] b=b[2:]+b[:1] flag+=1 if c==a: return flag if len(b)==2: c=b[0] b=b[1:] flag+=1 if c==a: return flag if len(b)==1: flag+=1 return flag
第二题,用java写了一下,测试案例都能过,不知道最终能不能过。。 import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class TestCode1 { public static int luckyNumber(int n) { Queue<Integer> queue = new LinkedList<>(); for (int i = 1; i <= n ; i++) { queue.offer(i); } int num = 0; while (!queue.isEmpty()) { for (int i = 0; i < 5; i++) { Integer element = queue.poll(); if (i == 4) { num++; if (element == n) { return num; } }else { queue.offer(element); } } } return 0; } }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入总人数:");
int totalNum = scanner.nextInt();
int count=0;
int countNum=5;
// 初始化人数
List<Integer> start = new ArrayList<Integer>();
for (int i = 1; i <= totalNum; i++) {
start.add(i);
}
//从第K个开始计数
int k = 0;
while (start.size() >0) {
k = k + countNum;
//幸运者的索引位置
k = k % (start.size()) - 1;
// 判断是否到队尾,如果k=-1,则在队尾,输出,否则出队后循环继续。
if (k < 0) {
start.remove(start.size() - 1);
count++;
System.out.println(count);
return;
} else {
count++;
start.remove(k);
}
}
}
第二题,拿自己以前写的约瑟夫环的代码改了改 def josephus(n, m):
p = 0
people = list(range(1, n+1))
out_list = []
i = 0
while True:
p = (p + (m-1)) % len(people)
out_list.append(people[p])
i += 1
if people[p] == people[len(people) - 1]:
print(i)
break
del people[p]
n = int(input())
m = 5
josephus(n, m)
#include <iostream> #include <vector> #include<queue> //#include"changyongsuanfa.h" using namespace std; int main(void) { int temp; vector<int > num; vector<int > num1; vector<int > num2; while (cin >> temp) num.push_back(temp); queue<int > mm; mm.push(num[num.size() - 1]); num.pop_back(); int count = 1; for (int i = 0; i < num.size(); i++) { if (i < num.size()/2) num1.push_back(num[i]); else num2.push_back(num[i]); } while (!mm.empty()) { int cnt = mm.front(); mm.pop(); for (int i = 0; i < num2.size(); i++) { if (num2[i] == cnt) { count++; mm.push(num1[i]); } } } cout << count; return 0; }
package test2; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import javax.management.Query; public class Main3 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String[] str1 = scn.nextLine().split(" "); String[] str2 = scn.nextLine().split(" "); String ss = scn.nextLine(); int k = Integer.parseInt(ss); int[] arr1 = new int[str1.length]; int[] arr2 = new int[str2.length]; int tag=0; for(int i=0;i<arr1.length;i++) { arr1[i] = Integer.parseInt(str1[i]); arr2[i] = Integer.parseInt(str2[i]); if(k==arr1[i]||k==arr2[i]) tag = 1; } HashMap<Integer, ArrayList<Integer>> map = new HashMap<>(); for(int i=0;i<arr2.length;i++) { if(map.containsKey(arr2[i])) { ArrayList<Integer> list = map.get(arr2[i]); list.add(arr1[i]); map.replace(arr2[i], list); }else { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(arr1[i]); map.put(arr2[i], list); } } HashSet<Integer> set = new HashSet<Integer>(); Queue<Integer> queue = new LinkedList<Integer>(); queue.offer(k); set.add(k); if(tag==0) { System.out.println(0); return; } int res = 1; while(!queue.isEmpty()) { Integer a = queue.poll(); // System.out.println(a); ArrayList<Integer> list = map.get(a); if(list!=null) { for(Integer b:list) { if(!set.contains(b)) { set.add(b); queue.offer(b); res++; } } } } System.out.println(res); } }
第二题用二维数组保存n个数中第i个数成为幸运数的轮次,然后n+1就考虑第一次后可以变为n个数中第n-5-1个数成为幸运数的轮次的问题,不知道内存会不会超
package study; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str1 = scanner.nextLine(); String[] arr1=str1.split(" "); String str2 = scanner.nextLine(); String[] arr2=str2.split(" "); String str = scanner.nextLine(); String temp; Queue<String> queue = new LinkedList<String>(); queue.offer(str); int n = 1; while(queue.size()>0) { temp = queue.poll(); for (int i = 0; i < arr2.length; i++) { if(temp.equals(arr2[i])) { n=n+1; queue.offer(arr1[i]); } } } System.out.println(n); } } 第一题测试代码都过了,但是ac一直为0很纳闷
第二题网上有类似的https://blog.csdn.net/jerechen/article/details/79137406
同求
有答案么
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享