东华复试OJ二刷复盘16
进阶23设 F(N) 表示正整数 1 到正整数 N 中,数字 1,2 总共出现了多少次。例如 N 10 时:1, 2, 3, 4, 5, 6, 7, 8, 9, 10 这 10 个数中,数字 1 出现了两次,数字 2 出现了 1 次,所以数字 1, 2 总共出现了 3 次,因此 F (10) 3。现在给你正整数 N ,请你求出 F(N) 的值。由于 F(N) 可能很大,你仅需输出 F(N) 除以 20123 的余数。用遍历的形式实现复杂度过高。标准答案为动态规划#include bits/stdc.h using namespace std; int ans0; void Count12(vectorintcur){ for(auto it:cur){ if(it 1 || it2 )ans; } } void AddOne(vectorintcur){ int jin0; cur[0]; for(auto it:cur){ itjin; jinit/10; it%10; if(jin0)return; } } bool IsContinue(vectorintcur,strings1){//是否继续 for(int i0;is1.size();i){ if(cur[i] ! (s1[i] - 0))return true; } return false; } int main(){ string s1;cins1; reverse(s1.begin(),s1.end()); vectorint cur(s1.size(),0); cur[0]1; while(IsContinue(cur,s1)){ Count12(cur); AddOne(cur);//自增 } Count12(cur);//相等时退出循环再记录一次 coutansendl; system(pause); }进阶24个锁上面看起来有 N 个数字,它们排成一排,并且每个数字都在 0 到 2 之间。你发现你可以通过锁上的机关来交换相邻两个数字的顺序。这个锁在上面存在某连续四个数字是“2012”的时候会自动打开。现在,你需要计算一下,你至少需要进行多少次交换操作才能打开这把锁?如果无论如何都没有希望打开这把锁,输出 -1。广度优先遍历。除了遍历的情况还需要注意写上不需要遍历和遍历得不到结果的初始边界情况。#include bits/stdc.h using namespace std; string s; struct node{ string curS; int step0; }; int ans0; unordered_setstring visited; void bfs(queuenodeq,stringtarget){ while(!q.empty()){//循环入队出队 node cur q.front();q.pop(); //边界条件检查 if(cur.curS.find(target) ! -1){ ans cur.step;return; } //否则继续广度遍历 for(int i0;icur.curS.size()-1;i){ node nextN cur; swap(nextN.curS[i],nextN.curS[i1]);//遍历与右侧元素交换 nextN.step; if(visited.find(nextN.curS) visited.end()){ visited.insert(nextN.curS); q.push(nextN); } else continue; } } } int main(){ int len;cinlens; string target 2012; if(s.find(target) ! -1){ coutansendl;return 0; } queuenode q; node newNode; newNode.curS s; newNode.step 0; q.push(newNode); bfs(q,target); if(ans!0)coutansendl; else cout-1endl; system(pause); }基础123大小为N的棋盘游戏包括N个白棋子N个黑棋子还有有2N1个格子的木盒子。这里是3-棋盘游戏的解包括初始状态中间状态和目标状态WWW BBBWW WBBBWWBW BBWWBWB BWWB BWBW BWBWBWBWBWBBW WBWBBWBW WBBWBWBWBWBWB WBWB BWWB BWBWWBB WBWWBBBW WWBBB WWW在这个游戏里有两种移动方法是允许的1. 你可以把一个棋子移到与它相邻的空格2. 你可以把一个棋子跳过一个(仅一个)与它不同色的棋子到达空格。请编一个程序解大小为N的棋盘游戏(1 N 12)。要求用最少的移动步数实现。错误1string cur q.pop() 出队不会返回队首元素而是需要front来访问pop做删除。错误2parent.find(news) parent.end()如果使用parent[ans]来比较会因为[] 访问会自动创建一个空字符串键值对所以用find查键下面回溯也是同理。#include bits/stdc.h using namespace std; unordered_mapstring, string parent; string target; void InsertParent(stringnews,stringps,queuestringq){ if(parent.find(news) parent.end()){ q.push(news); parent.insert({news,ps}); } } void BFS(queuestringq){ while(!q.empty()){ string cur q.front();q.pop(); //边界 if(cur target)return; //通过map来回溯步骤 //入队遍历 int ZeroIndex cur.find( ); if(ZeroIndex!0){ string newS cur; swap(newS[ZeroIndex-1],newS[ZeroIndex]); InsertParent(newS,cur,q); } if(ZeroIndex!cur.size()-1){ string newS cur; swap(newS[ZeroIndex1],newS[ZeroIndex]); InsertParent(newS,cur,q); } if(ZeroIndex2){ string newS cur;//异色 if(newS[ZeroIndex-2] ! newS[ZeroIndex-1]){ swap(newS[ZeroIndex],newS[ZeroIndex-2]); InsertParent(newS,cur,q); } } if(ZeroIndexcur.size()-3){ string newS cur;//异色 if(newS[ZeroIndex2] ! newS[ZeroIndex1]){ swap(newS[ZeroIndex],newS[ZeroIndex2]); InsertParent(newS,cur,q); } } } } int main(){ int len;cinlen; string s; for(int i0;i2*len1;i){ if(ilen){ s.push_back(W); target.push_back(B); } else if(ilen){ s.push_back( ); target.push_back( ); } else{ s.push_back(B); target.push_back(W); } } string start s; // couttargetendl; // coutsendl; queuestring q; q.push(s); BFS(q); vectorint ansSpace; string ans target; while(ans ! s) { ansSpace.push_back(ans.find( )1); ans parent[ans]; } //parent[ans]用 [] 访问会自动创建一个空字符串键值对 //导致 parent 多出一个无用条目导致死循环或错误。 reverse(ansSpace.begin(),ansSpace.end()); for(auto it:ansSpace){ coutit ; } system(pause); }Federated learning is a distributed machine learning approach that aims to train models while preserving user data privacy. In traditional machine learning methods, data is usually centralized on servers for training. However, in many real-world scenarios, data is private and sensitive, making it difficult to share directly. Federated learning addresses this problem by training models locally on devices and only uploading model parameters or gradient information to a central server. In this way, collaborative learning can be achieved without exposing raw data. Federated learning has attracted increasing attention in fields such as mobile computing, medical data analysis, and financial risk control.联邦学习是一个分布式机器学习方法它旨在保护用户数据隐私的情况下训练模型。在传统的机器学习方法用于训练的数据通常是集中在服务器中。然而在许多现实情况下数据是敏感且隐私的这使得直接共享数据是困难的。联邦学习通过在本地设备上训练模型并仅上传模型参数或梯度信息来解决这个问题。通过这个方式能在不暴露原始数据的情况下实现联合学习。联邦学习已经在一些领域吸引越来越多的关注例如移动计算医疗数据分析金融风险控制。Federated learning 联邦学习 preserving 保护/保存 scenarios 场景/情景
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2438186.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!