版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1、1022 Digital Library (30 分)
题目大意:输入书本相关内容,对相关书籍进行查询,输出所有符合搜索条件的书籍编号。
一开始没想起来有find函数这种东西…find()返回查找字符的首地址。可用于此题的查找。
#include<cstdio>
#include<map>
#include<set>
#include<iostream>
using namespace std;
map<string, set<int> > title, writer, key, publish, year;
void search_(map<string, set<int> >& m, string& str) {
if (m.find(str) != m.end()) {
for (auto it = m[str].begin(); it != m[str].end(); it++) {
printf("%07d\n", *it);
}
}
else {
printf("Not Found\n");
}
}
这题使用map,以关键字来对应id从而满足查找需要,将不同的id储存到同一个关键字对应的set中,同时还巧妙运用了set自动排序的性质。
使用m.find(str) != m.end()来判断关键字是否存在,end()指向最后一个元素的后一位。
写出主函数,使用getline输入字符串,因书名、作者等字符串含空格,故不能使用cin;使用检测换行符结束关键字的录入;
int main() {
int n;
scanf("%d", &n);
int id;
string title_, writer_, key_, publish_, year_;
for (int i = 0; i < n; i++) {
scanf("%d\n", &id);
getline(cin, title_);
title[title_].insert(id);
getline(cin, writer_);
writer[writer_].insert(id);
while (cin >> key_) {
key[key_].insert(id);
char c = getchar();
if (c == '\n') break;
}
getline(cin, publish_);
publish[publish_].insert(id);
getline(cin, year_);
year[year_].insert(id);
}
int m, x;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d: ", &x);
string s;
getline(cin, s);
printf("%d: ", x);
cout << s << "\n";
if (x == 1)
search_(title, s);
if (x == 2)
search_(writer, s);
if (x == 3)
search_(key, s);
if (x == 4)
search_(publish, s);
if (x == 5)
search_(year, s);
}
return 0;
}
2、1023 Have Fun with Numbers (20 分)
一开始还以为此题在考查long long ,然而long long 最多只有19位…转而使用一个字符数组储存大数。
使用一个长度为10的数组来判别前后数中的各位数字数量有没有变化。
每位数字*2配合进位符完成乘法操作。
#include<iostream>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
int exist[10] = { 0 };
int main() {
char s[21];
scanf("%s", s);
int x = strlen(s);
int up = 0;
for (int i = x - 1; i >= 0; i--) {
int y = s[i] - '0';
exist[y]++;
y = y * 2 + up;
up = 0;
if (y >= 10) {
up = 1;
y = y - 10;
}
exist[y]--;
s[i] = y + '0';
}
int flag = 0;
for (int i = 0; i < 10; i++) {
if (exist[i] != 0) flag = 1;
}
if (flag || up) printf("No\n");
else printf("Yes\n");
if (up == 1)
printf("1");
for (int i = 0; i < x; i++) {
printf("%d", s[i] - '0');
}
}