2 条题解

  • 0
    @ 2024-4-6 18:00:08

    能用标准库写的东西绝不手搓

    这题水起来是这样过的(C++11)

    /*                     _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                           O\ = /O
                       ____/`---'\____
                     .   ' \\| |// `.
                      / \\||| : |||// \
                    / _||||| -:- |||||- \
                      | | \\\ - /// | |
                    | \_| ''\---/'' | |
                     \ .-\__ `-` ___/-. /
                  ___`. .' /--.--\ `. . __
               ."" '< `.___\_<|>_/___.' >'"".
              | | : `- \`.;`\ _ /`;.`/ - ` : | |
                \ \ `-. \_ __\ /__ _/ .-` / /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='                      */
    #include <bits/stdc++.h>
    
    using namespace std;
    using i128 [[maybe_unused]] = __int128;
    
    std::istream &operator>>(std::istream &is, i128 &value) {
        value = 0;
        bool isNegative = false;
        int c = is.get();
        while (c < '0' or c > '9') {
            if (c == '-')
                isNegative = true;
            c = is.get();
        }
        while (c >= '0' and c <= '9') {
            value = (value << 3) + (value << 1) + (c ^ '0');
            c = is.get();
        }
        if (isNegative)
            value = -value;
        return is;
    }
    
    std::ostream &operator<<(std::ostream &os, i128 value) {
        if (value < 0) {
            value = ~value + 1;
            os.put('-');
        }
        static char sta[40];
        char top = 0;
        for (; value; value /= 10)
            sta[top++] = static_cast<char>(value % 10);
        for (; top--;)
            os.put(static_cast<char>(sta[top] ^ '0'));
        return os;
    }
    
    using i64 [[maybe_unused]] = long long;
    template<typename T, class comparer = greater<T>>
    using heap [[maybe_unused]] = priority_queue<T, vector<T>, comparer>;
    template<typename T1, typename T2>
    using hash_map [[maybe_unused]] = unordered_map<T1, T2>;
    template<typename T>
    using hash_set [[maybe_unused]] = unordered_set<T>;
    const char &ln = '\n';
    
    template<typename T, typename V>
    T as [[maybe_unused]](const V &val) { return static_cast<T>(val); }
    
    template<typename T, class Begin, class End>
    void println [[maybe_unused]](Begin begin, End end, const char *c = " ") {
        copy(begin, end, ostream_iterator<T>(cout, c));
        cout.put('\n');
    }
    
    signed main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        time_t utc;
        cin >> utc;
        // 微秒转秒,忽略不计了
        utc /= 1000;
        char timeString[size("hh:mm:ss")];
    // Windows的MSVC为了安全已经弃用gmtime函数,需要使用更安全的实现。但是评测机是Linux的,所以正常写也无妨
    #ifdef _WIN32
        unique_ptr<tm> p(new tm);
        gmtime_s(p.get(), &utc);
    #endif
        strftime(data(timeString), size(timeString), "%T",
    #ifdef _WIN32
                 p.get()
    #else
                gmtime(&utc)
    #endif
        );
        cout << timeString << ln;
        return 0;
    }
    

    看上去很长,其实主要是模板和预处理长,要去掉那些,代码就非常精简了。

    #include <bits/stdc++.h>
    using namespace std;
    signed main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        time_t utc = (cin >> utc, utc / 1000);  // 输入并微秒转秒,去除微妙
        char timeString[9];
        strftime(data(timeString), size(timeString), "%T",gmtime(&utc));
        cout << timeString << '\n';
        return 0;
    }
    
    • 0
      @ 2024-4-2 0:01:20

      【2021年省赛B组】试题F: 时间显示

      题解

      简单模拟题,语法补零, 由于不用考虑毫秒,所以可以去掉毫秒的精度。由于不用考虑年月日,所以只保留最后一天的时间即可。值的注意的是1s=1000s1s=1000s, 时间复杂度为O(1)O(1)

      提交代码

      • scanf printf 版
      #include<bits/stdc++.h>
      
      void solve()
      {
          long long x; scanf("%lld", &x);
          x /= 1000;
          x %= 24 * 60 * 60;
          printf("%02lld:%02lld:%02lld", x / 3600, (x % 3600) / 60, (x % 3600) % 60);
      }
      
      int main() 
      {
          int t = 1;
          while (t--) solve();
          return 0;
      }
      
      • cin cout 版
      #include<bits/stdc++.h>
      
      void solve()
      {
      	long long x; std::cin >> x;
          x /= 1000;
          x %= 24 * 60 * 60;
      	int hh = x / 3600;
      	int mm = (x % 3600) / 60;
      	int ss =  (x % 3600) % 60;
          std::cout << std::setfill('0') << std::setw(2) << hh << ":" << std::setw(2) << mm << ":" << std::setw(2) << ss << "\n";
      }
      int main() 
      {
          int t = 1;
          while (t--) solve();
          return 0;
      }
      
      • 1

      信息

      ID
      981
      时间
      1000ms
      内存
      256MiB
      难度
      5
      标签
      递交数
      24
      已通过
      11
      上传者