2016北航机试

代码均为自做

题目一

给定一个数n,将这个数的各位顺序颠倒,成为逆序数m。例如1234的逆序数是4321 。
如果m是n的k倍(k为整数),那么输出nk=m
输入:
1089
输出:
1089
9=9801
如果m不是n的整数倍,那么输出n和m的逆序数
输入:
1234
输出:
1234 4321
输入:
23200
输出:
23200 00232
已知输入开头不包括多余的零

#include <iostream>

using namespace std;

int main()
{
    int n = 0, m = 0;
    char mm[100]; // 存倒过来的n
    cin >> n;
    int tmp = n, t;
    int len = 0;
    while (tmp != 0) {
        t = tmp%10; // 获取个位
        m = m*10 + t;
        mm[len++] = '0' + t;
        tmp /= 10;
    }
    if (m % n == 0) {
        cout << n << '*' << m / n << '=' << m << endl;
    }
    else {
        cout << n << ' ';
        for (int i = 0; i < len; i++) {
            cout << mm[i];
        }
        cout << endl;
    }
    return 0;
}
题目二

给一个c语言的enum定义语句,输出enum中规定的各项及其对应的数值。

输入:
enum BOOL{true,false};
输出
true 0
false 1
输入 :
enum date{JAN=1,FEB,MAR,APR,MAY,JUN,JULY,AUG,SEP,OCT,NOV,DEC,MON=1,TUE,WED,THU,FRI,SAT,SUN,found=1949};
输出:
JAN 1
FEB 2
MAR 3
APR 4
MAY 5
JUN 6
JULY 7
AUG 8
SEP 9
OCT 10
NOV 11
DEC 12
MON 1
TUE 2
WED 3
THU 4
FRI 5
SAT 6
SUN 7
found=1949

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{ // enum中的每一项及其值
    string name;
    int value;
    Node(string name, int value) : name(name), value(value) {}
};
int main()
{
    vector<Node> nodes;
    char ch;
    string str;
    int v;
    bool start = false;
    while ((ch = getchar()) != ';') { // 一个字符一个字符开始读
        if (ch == '{') { // 从此之后开始将要读enum中的元素
            start = true; // 标记为true,将开始读enum中的元素
            v = 0;
            str.clear();
            continue;
        }
        if (start == false) { // 还没开始读enum中的元素
            continue;
        }
        if (ch == '=') { // 如果遇到等号则给元素赋值
            v = 0;
            while ((ch = getchar()) && (ch != ',' && ch != '}')) { // 一直把值读完
                if (ch >= '0' && ch <= '9') { // 对value重新赋值
                    v *= 10;
                    v += ch - '0';
                }
            }
        }
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_') { // enum中的元素
            str.push_back(ch);
        }
        else if (ch == ',' || ch == '}') { // 读完一个元素
            if (str.size() == 0) continue; // " ... , } " 这种情况也是符合语法的,即,后没有元素了,加下来是}
            Node tmp = Node(str, v);
            nodes.push_back(tmp);
            v++;
            str.clear();
        }
    }
    vector<Node>::iterator it;
    for (it = nodes.begin(); it != nodes.end(); it++) {
        cout << it->name << ' ' << it->value << endl;
    }
    return 0;
}
/*
enum BOOL{true,false};
enum date{JAN=1,FEB,MAR,APR,MAY,JUN,JULY,AUG,SEP,OCT,NOV,DEC,MON=1,TUE,WED,THU,FRI,SAT,SUN,found=1949};
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值