5 条题解

  • 3
    @ 2024-3-1 10:09:55

    代码当然是越快速越好喽,所以我们偏不用栈Python的话直接hex()秒了

    print(hex(int(input()))[2:].upper())
    
    • 3
      @ 2023-3-6 18:41:05

      C++

      Florance

      #include <bits/stdc++.h>
      using namespace std;
      int main(){
          int n, x;
          string s;
          string a = "0123456789ABCDEF";//打表
          cin >> n;
          while(n != 0) { //转换
              x = n % 16;
              s = a[x] + s;
              n /= 16;
          }
          cout << s;
          return 0;
      }
      
      • 1
        @ 2023-5-9 16:01:32

        这题思路并不复杂,但我们需要思考:本题为什么能用栈来解决?

        因为题目要求用栈来解决

        因为取余倒序法中的倒序满足栈先进后出的特性,因此能使用栈

        这里使用列表模拟栈

        n = int(input())
        s = "0123456789ABCDEF"
        stack = []
        while n:
            stack.append(n%16)
            n//=16
        while len(stack):
            print(s[stack.pop()],end = "")
        
        • 1
          @ 2023-3-6 18:55:15

          Python

          思路和C++一样,但python不能直接输出

          Florance

          n = int(input())
          a = '0123456789ABCDEF' #打表
          s = [""] * 100
          b = 0
          while n != 0:
              x = n % 16
              s[b] = a[x]
              b += 1
              n = n // 16
          while b >= 0: #逆序输出
              print(s[b], end = '')
              b -= 1
          

          提交的答案是可以查看的,禁止直接抄题解,毕竟我的码风摆在这,题解只提供思路。有的人甚至连注释都不愿意删。。。

          • 0
            @ 2025-4-20 17:17:17

            a = '0123456789ABCDEF' n = int(input()) s="" while n>0: r=n%16 n=n//16 if r>9: x=chr(r+55) else: x=str(r) s=x+s
            print(s)
            小学生做法

            • 1

            信息

            ID
            814
            时间
            1000ms
            内存
            256MiB
            难度
            4
            标签
            递交数
            919
            已通过
            395
            上传者