zoukankan      html  css  js  c++  java
  • USACO Section1.3 Combination Lock 解题报告

        combo解题报告 —— icedream61 博客园(转载请注明出处)
    ------------------------------------------------------------------------------------------------------------------------------------------------
    【题目】
      一个三位密码锁,每位可能取值1~N。有两个解锁密码,都会给出。
      若尝试的密码与某个解锁密码相等或相近(每位值都至多差2),均可解开锁。
      例如:(1,2,3)(4,5,6)是两个解锁密码,那么(1,N,5)(2,4,8)均可解锁,而(1,5,6)则不可。
    【数据范围】
      1<=N<=100
    【输入样例】
      50
      1 2 3
      5 6 7
    【输出样例】
      249
    ------------------------------------------------------------------------------------------------------------------------------------------------
    【分析】
      直接枚举。
      需要注意,两个密码相等的判断容易出错(需要%N的情况下,下标并非从0开始;差2及以内均相等,此差2可能与%N同时发生)。
    ------------------------------------------------------------------------------------------------------------------------------------------------
    【总结】
      一遍AC。
      顺带用了下重载运算符与类内置函数的语法,有个小问题存疑:如果我把代码中add和inRange两个函数放到Key类里面,那么operator==里面便无法调用这两个函数。如有人懂,望不吝赐教~

    ------------------------------------------------------------------------------------------------------------------------------------------------

    【代码】

     1 /*
     2 ID: icedrea1
     3 PROB: combo
     4 LANG: C++
     5 */
     6 
     7 #include <iostream>
     8 #include <fstream>
     9 using namespace std;
    10 
    11 int N;
    12 
    13 int add(int x,int y) { return (x+y-1+N)%N+1; }
    14 bool inRange(int x,int y) { return x==y || add(x,1)==y || add(x,2)==y || add(x,-1)==y || add(x,-2)==y; }
    15 
    16 struct Key
    17 {
    18     int x,y,z;
    19     friend bool operator==(Key a,Key b) { return inRange(a.x,b.x) && inRange(a.y,b.y) && inRange(a.z,b.z); }
    20 };
    21 
    22 Key A,B;
    23 
    24 int main()
    25 {
    26     ifstream in("combo.in");
    27     ofstream out("combo.out");
    28 
    29     in>>N;
    30     in>>A.x>>A.y>>A.z;
    31     in>>B.x>>B.y>>B.z;
    32 
    33     int s=0;
    34     Key t;
    35     for(t.x=1;t.x<=N;++t.x)
    36         for(t.y=1;t.y<=N;++t.y)
    37             for(t.z=1;t.z<=N;++t.z) s+=(t==A||t==B);
    38     out<<s<<endl;
    39 
    40     in.close();
    41     out.close();
    42     return 0;
    43 }
  • 相关阅读:
    git did not exit cleanly (exit code 128)
    thinkphp导出csv文件,用表格输出excel
    php函数substr_replace中文乱码的替代解决方法
    javascript中的后退和刷新
    技术上有困惑,来看看“一万个小时”吧
    什么是 .live()
    为什么php+apache本地站点访问超级慢
    thinkphp学习笔记10—看不懂的路由规则
    4星|《大英博物馆中国简史》:中国古代代表性文物图解
    贾似道简单几招让凶暴的临安府太学生变温顺:4星|《宫崎市定人物论》
  • 原文地址:https://www.cnblogs.com/icedream61/p/4323274.html
Copyright © 2011-2022 走看看