• 2022年《数据结构试验》上机考试一(计科2103,2105班+数据2101,2102班)题解


     这个波一OJ我真是***

    一直给我交之前的main.cpp,我新换上去一直交不上去,一直wa

    2208: 该谁发球了?

    这是个小思维

    大于10以后看的是%2结果

    小于10看的是%4的结果

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. const int N=1e6+10;
    6. signed main(){
    7. int n,m;
    8. while(cin>>n>>m){
    9. if(abs(n-m)==2&&max(n,m)>=11){
    10. cout<<"Game Over\n";
    11. }
    12. else if(n>=10&&m>=10){
    13. if((n+m)&1==0){
    14. cout<<"A\n";
    15. }
    16. else{
    17. cout<<"B\n";
    18. }
    19. }
    20. else{
    21. if((n+m)%4>=2){
    22. cout<<"B\n";
    23. }
    24. else{
    25. cout<<"A\n";
    26. }
    27. }
    28. }
    29. }

    2375: 判断三角形的形状

    模拟即可

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. const int N=1e6+10;
    6. signed main(){
    7. int a,b,c;
    8. while(cin>>a>>b>>c){
    9. if(a==b&&b==c){
    10. cout<<"DB\n";
    11. }
    12. else if(a+b
    13. cout<<"ERROR\n";
    14. }
    15. else if(a==b || b==c || a==c){
    16. cout<<"DY\n";
    17. }
    18. else if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a){
    19. cout<<"ZJ\n";
    20. }
    21. else{
    22. cout<<"PT\n";
    23. }
    24. }
    25. }

    5873: 3.5.2 悲剧文本

    虽然也是模拟

    但这题用list写会好写很多

    因为list可以指定插入,通过begin和end调整位置

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. const int N=1e6+10;
    6. signed main(){
    7. string s;
    8. while(cin>>s){
    9. list<int> li;
    10. auto it=li.begin();
    11. for(auto t:s){
    12. if(t==']'){
    13. it=li.end();
    14. }
    15. else if(t=='['){
    16. it=li.begin();
    17. }
    18. else{
    19. li.insert(it,t);
    20. }
    21. }
    22. for(auto t:li){
    23. cout<<(char)t;
    24. }
    25. cout<<'\n';
    26. }
    27. }

    5867: 4.4.3 矩阵连乘

    这道题是之前四则运算的一个简化版本

    用栈存括号来辅助计算

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. #define pll pair
    6. const int N=1e6+10;
    7. pll q[N];
    8. int n,m;
    9. signed main(){
    10. cin>>n;
    11. for(int i=1;i<=n;++i){
    12. char x;
    13. cin>>x;
    14. cin>>q[x].first>>q[x].second;
    15. }
    16. string s;
    17. while(cin>>s){
    18. int f=0;
    19. int res=0;
    20. stack stk;
    21. for(auto i:s){
    22. if(isalpha(i)) stk.push(q[i]);
    23. else if(i==')'){
    24. auto k2=stk.top();
    25. stk.pop();
    26. auto k1=stk.top();
    27. stk.pop();
    28. if(k1.second!=k2.first){
    29. f=1;
    30. break;
    31. }
    32. res+=k1.first*k1.second*k2.second;
    33. stk.push({k1.first,k2.second});
    34. }
    35. }
    36. if(f) cout<<"error\n";
    37. else cout<'\n';
    38. }
    39. }

    5874: 4.4.4 打印队列

    第i次被打印的一定是第i大的数

    排序之后用队列模拟

    是直接输出,不是就pop

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. #define pll pair
    6. const int N=1e6+10;
    7. pll q[N];
    8. int n,m;
    9. void solve(){
    10. queue<int> q;
    11. vector<int> a,b;
    12. int k=0;
    13. cin>>n>>m;
    14. for(int j=0;j
    15. int x;
    16. cin>>x;
    17. a.push_back(x);
    18. b.push_back(x);
    19. q.push(j);
    20. }
    21. sort(b.begin(),b.end(),greater<int>());
    22. int w=0;
    23. int max=0;
    24. while(q.size()){
    25. max=b[w];
    26. int t=q.front();
    27. if(a[t]
    28. q.pop();
    29. q.push(t);
    30. }
    31. else{
    32. if(t==m){
    33. cout<<++k<<'\n';
    34. break;
    35. }
    36. else{
    37. q.pop();
    38. k+=1;
    39. w+=1;
    40. }
    41. }
    42. }
    43. }
    44. signed main(){
    45. int T;
    46. cin>>T;
    47. while(T--){
    48. solve();
    49. }
    50. }

    5907: 5.3.5.3 树

    几乎是作业原题了

    通过中序和前序建树

    (不懂这个可以搜一下,很多教程)

    然后dfs

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. #define pll pair
    6. const int N=1e6+10;
    7. int n,lc[N],rc[N];
    8. int minsum,minv;
    9. int ino[N],pos[N];
    10. int create(int L,int R,int m){
    11. if(m<=0) return 0;
    12. int root=pos[R+m-1];
    13. int len=0;
    14. while(ino[L+len]!=root){
    15. len+=1;
    16. }
    17. lc[root]=create(L,R,len);
    18. rc[root]=create(L+len+1,R+len,m-len-1);
    19. return root;
    20. }
    21. bool readline(int *a){
    22. string line;
    23. if(!getline(cin,line))
    24. return false;
    25. stringstream s(line);
    26. n=0;
    27. int x;
    28. while(s>>x){
    29. a[n++]=x;
    30. }
    31. return n>0;
    32. }
    33. void dfs(int v,int sum){
    34. sum+=v;
    35. if(lc[v]==0&&rc[v]==0){
    36. if(sum
    37. minv=v;
    38. minsum=sum;
    39. }
    40. }
    41. if(lc[v]){
    42. dfs(lc[v],sum);
    43. }
    44. if(rc[v]){
    45. dfs(rc[v],sum);
    46. }
    47. }
    48. void work(){
    49. create(0,0,n);
    50. minsum=1000000000+7;
    51. dfs(pos[n-1],0);
    52. cout<'\n';
    53. }
    54. signed main(){
    55. while(readline(ino)){
    56. readline(pos);
    57. work();
    58. }
    59. }

    5917: 5.4.2 信息熵

    题面花里胡哨的

    这题哈夫曼树板子题

    每次取两个最小的,合并在塞回去

    直到剩下一个

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. using namespace std;
    5. #define pll pair
    6. const int N=1e6+10;
    7. int a[100];
    8. string s;
    9. void work(){
    10. priority_queue<int,vector<int>,greater<int> > q;
    11. if(s=="END") exit(0);
    12. for(int i=0;i<=99;++i) a[i]=0;
    13. for(int i=0;i<=s.size()-1;++i){
    14. if(s[i]=='_'){
    15. a[26]+=1;
    16. }
    17. else{
    18. a[s[i]-'A']++;
    19. }
    20. }
    21. for(int i=0;i<27;++i){
    22. if(a[i]!=0){
    23. q.push(a[i]);
    24. }
    25. }
    26. int sum=0;
    27. while(q.size()>=2){
    28. int x1=q.top();
    29. q.pop();
    30. int x2=q.top();
    31. q.pop();
    32. q.push(x1+x2);
    33. sum+=(x1+x2);
    34. }
    35. if(!sum) sum=s.size();
    36. cout<size()*8<<" "<" ";
    37. cout<setprecision(1)<<(double)(s.size()*8)/(double)sum<<'\n';
    38. }
    39. signed main(){
    40. while(cin>>s){
    41. work();
    42. }
    43. }

  • 相关阅读:
    【面试题精讲】Object类的常见方法有哪些?
    优优嗨聚集团:OTC药品能否纳入报销成为各方关注焦点,对OTC医疗有何影响
    Python开发环境及常用Web框架
    垃圾收集器与内存分配策略
    软件协会第01次活动第05次任务布置:爱心代码+演奏歌曲+typora使用pandoc导出+github注册登录+函数练习+写csdn文章
    Qt编译zlib完成文件压缩解压(Ubuntu18.04)
    GB/T28181流媒体相关协议详解
    麒麟桌面系统CVE-2024-1086漏洞修复
    MariaDB简介
    设计模式之美——依赖反转原则
  • 原文地址:https://blog.csdn.net/m0_61735576/article/details/127905324