全部评论
发工资跟摆火柴哪个大佬可以提供一下思路啊
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int ans=0;
queue<int> que;
que.push(n);
while (!que.empty()){
int fron=que.front();
que.pop();
if ((fron+k)%2==0 && fron>k){
int lar=(fron+k)/2;
int sma=fron-lar;
que.push(lar);
que.push(sma);
}else{
ans++;
}
}
cout<<ans<<endl;
return 0;
} 第二题部队 第三题 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n;
cin>>n;
int indegree[100001];
memset(indegree,0,sizeof indegree);
bool vis[100010];
memset(vis,false,sizeof vis);
map<int,vector<int>>m;
for (int i=1;i<n;i++){
int u,v;
cin>>u>>v;
indegree[u]++;
indegree[v]++;
m[v].push_back(u);
m[u].push_back(v);
}
queue<int> que;
int count=0;
for (int i=1;i<=n;i++){
if (indegree[i]<=1){
que.push(i);
vis[i]=true;
count++;
}
}
int ans[100010];
int index=0;
while (!que.empty()){
index++;
int temp=0;
for (int i=1;i<=count;i++){
int fron=que.front();
que.pop();
ans[fron]=index;
for (int k=0;k<m[fron].size();k++){
indegree[m[fron][k]]--;
if (indegree[m[fron][k]]<=1 && vis[m[fron][k]]==false){
vis[m[fron][k]]=true;
que.push(m[fron][k]);
temp++;
}
}
}
count=temp;
}
for (int i=1;i<=n;i++){
if (i!=n)
cout<<ans[i]<<" ";
else cout<<ans[i]<<endl;
}
return 0;
}
就通过了买饮料
为啥我做的那套卷子没有编程题,只有四个问答题
士兵那一题,用递归实现: public class Main {
public static int count =0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//士兵人数
int k = scanner.nextInt();//人数差
fun(n, k);
System.out.println(count);
}
public static void fun(int n, int k) {
if ((n > k)
&& (n + k) % 2 == 0
&& (n - k) % 2 == 0) {
fun((n + k) / 2, k);
fun((n - k) / 2, k);
} else {
count++;
return;
}
}
}
各位大佬真的太强了,膜拜
部队那题就是一个层序遍历的过程,每次生成一个TreeNode,判断能不能再分就是判断(value-k)%2=?0,可以再分就new一个left一个right就行;代码如下: package baidu; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class TreeNode{ int val; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int result = 0; TreeNode root = new TreeNode(n); Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); while(!queue.isEmpty()) { int cnt = queue.size(); for(int i=0;i<cnt;i++) { TreeNode cur = queue.poll(); if(cur.val>k&&(cur.val-k)%2==0) { TreeNode left = new TreeNode((cur.val-k)/2); TreeNode right = new TreeNode((cur.val+k)/2); cur.left = left; cur.right = right; }else { result++; } if(cur.left!=null) queue.add(cur.left); if(cur.right!=null) queue.add(cur.right); } } System.out.println(result); } }
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a;
int b;
Map<Integer,Integer> map1= new HashMap<>();//度
Map<Integer, Integer> map2 = new HashMap<>();//边
for(int i=1;i<=n;i++) {
map1.put(i, 0);
}
for (int i = 0; i < n-1; i++) {
a = sc.nextInt();
int c1=map1.get(a);
map1.put(a, ++c1);
b=sc.nextInt();
map2.put(b, a);
}
System.out.println(map1);
int[] count = new int[n+1];
while (!map1.isEmpty()) {
Set<Map.Entry<Integer, Integer>> set = map1.entrySet();
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(set);
con(list, count, map2, map1);
}
for(int i=1;i<count.length;i++) {
if (i == count.length) {
System.out.print(count[i]);
}
else{
System.out.print(count[i]+" ");
}
}
}
public static void con(List<Map.Entry<Integer, Integer>> list,int [] count,Map<Integer,Integer> map2,Map<Integer,Integer> map1) {
for (Map.Entry entry : list) {
if((Integer)entry.getValue()==0){
count[(Integer)entry.getKey()]++;
map1.remove(entry.getKey());
if(map2.containsKey(entry.getKey())){
Integer edge=map2.get(entry.getKey());
int a = map1.get(edge);
if(a>0)
map1.put(edge, --a);
}
} else if ((Integer) entry.getValue() > 0) {
count[(Integer)entry.getKey()]++;
}
}
}
}
import java.util.Scanner;
public class Main {
public static int huaff( int n, int k){
int left = (n + k) / 2;
int sum = 0;
int right = left - k;
if (left - right != k || right <= 0 || left <= 0 || left + right != n){
return sum + 1;
}
return huaff(left, k) + huaff(right, k);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int sum = huaff(n, k);
System.out.println(sum);
}
} 只会第二题
第一题很简单啊。。倒是第二题有点懵
第一题百分之50
多少分算过呀
我不配
好难啊,真的难,编程看了看没思路
唉,有心无力
请问有大佬做出来的能附下代码吗
真的难
太难了,呜呜
相关推荐
06-04 09:27
门头沟学院 Java 点赞 评论 收藏
分享