代码均为自做
题目一
给定一个数n,将这个数的各位顺序颠倒,成为逆序数m。例如1234的逆序数是4321 。
如果m是n的k倍(k为整数),那么输出nk=m
输入:
1089
输出:
10899=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};
*/