zoukankan      html  css  js  c++  java
  • 1B_十进制和二十六进制的相互转化

    题目解析:核心的部分就在于进制的转化,懒得多说,就放代码。

    #include<iostream>
    #include<string>
    #include<queue>
    #include<cmath>
    #include<algorithm>
    #include<cstring> 
    #define ll long long
    using namespace std;
    void change_10to26(int s) {
        int len[100], cnt = 0; int temp;
        while (s) {
            temp = s % 26;
            if (temp == 0)temp = 26;//当temp为0的时候它就是Z
            len[cnt++] = temp;
            s = (s - temp) / 26;//为啥呢,比如如果输入,应该是A但不加这条就会出错.
        }
        for (int i = cnt - 1; i >= 0; i--)
            cout << (char)(len[i] + 'A' - 1);
    }
    void change_26to10(string s) {
        int res = 0;
        for (int i = 0; i < s.size(); i++) {
            res *= 26; res += s[i] - 64;
        }
        cout << res;
    }
    int main() {
        int n;
        cin >> n;
        int i;
        while (n--) {
            char k[100];
            char op[100];
            scanf("%s", k);
            int a, b;
            if (sscanf(k, "R%dC%d", &a, &b) == 2) {//sscanf函数,如果没做到这题我是真的不知道,多学学多看看,我太菜了
                change_10to26(b);
                cout << a << endl;
            }
            else {
                string s = "";
                for (i = 0; k[i] >= 'A'&&k[i] <= 'Z'; i++)
                    s += k[i];
                a = 0;
                for (int j = i; j < strlen(k); j++) {
                    a *= 10;
                    a += k[j] - '0';
                }
                cout << "R" << a << "C";
                change_26to10(s);
                cout << endl;
            }
        }
        return 0;
    }

    最后一句话,我太菜了

  • 相关阅读:
    4408: [Fjoi 2016]神秘数
    UOJ #35. 后缀排序[后缀数组详细整理]
    POJ 2887 Big String
    搜索过滤grep(win下为findstr)
    解决putty自动断开的问题
    > >> 将错误输出到文件
    环境变量
    端口被占用,查看并杀死占用端口的进程
    查找文件路径find
    【vim使用】
  • 原文地址:https://www.cnblogs.com/Ean1zhi/p/12007205.html
Copyright © 2011-2022 走看看