标签:
注:编译环境为Visual Studio 2012,答案仅供参考。
91.按照指定规则对输入的字符串进行处理。
详细描述:
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,则对他们所代表的16进制的数进行BIT倒序的操作,并转换为相应的大写字符。如字符为‘4’,为0100b,则翻转后为0010b,也就是2。转换后的字符为‘2’; 如字符为‘7’,为0111b,则翻转后为1110b,也就是e。转换后的字符为大写‘E’。
举例:输入str1为”dec”,str2为”fab”,合并为“decfab”,分别对“dca”和“efb”进行排序,排序后为“abcedf”,转换后为“5D37BF”.
#include<iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int convertNum(int n){
int sum = 0;
vector<int> v;
while (n != 0)
{
v.push_back(n%2);
n /= 2;
}
for (unsigned i = 0; i < v.size(); i++)
{
sum += v[i] * (int)pow(2,v.size()-1-i);
}
return sum;
}
char convert(char c){
int n;
if (c >= ‘0‘ && c <= ‘9‘)
{
n = c - ‘0‘;
}
else if (c >= ‘a‘ && c <= ‘f‘)
{
n = c - ‘a‘ + 10;
}
else if (c >= ‘A‘ && c <= ‘F‘)
{
n = c - ‘A‘ + 10;
}
else
{
return c;
}
n = convertNum(n);
if (n < 10)
{
return n + ‘0‘;
}
else
{
return n - 10 + ‘A‘;
}
}
void copySub(string &s, const string &tmp, int flag){
for (unsigned i = 0; i < tmp.size(); i++)
{
s[i*2+flag] = tmp[i];
}
}
void deal(const string &s1, const string &s2, string &output){
string s = s1 + s2, tmp1, tmp2;
for (unsigned i = 0; i < s.size(); i++)
{
i % 2 == 0 ? tmp1.push_back(s[i]) : tmp2.push_back(s[i]);
}
sort(tmp1.begin(),tmp1.end());
sort(tmp2.begin(),tmp2.end());
copySub(s,tmp1,0);
copySub(s,tmp2,1);
for (unsigned i = 0; i < s.size(); i++)
{
output.push_back(convert(s[i]));
}
}
int main()
{
string s1,s2,output;
cin >> s1 >> s2;
deal(s1,s2,output);
cout << output << endl;
return 0;
}
92.控制输入一个字符串,然后将字符串里面的单词按照原排列顺序的逆序输出,如输入为:i am a boy.则输出为:boy a am i.
#include<iostream>
#include <string>
using namespace std;
void convert(string &input){
auto begin = input.begin();
while (begin != input.end())
{
if (*begin == ‘ ‘)
{
begin++;
continue;
}
auto ahead = begin + 1;
while (ahead != input.end() && *ahead != ‘ ‘)
{
ahead++;
}
reverse(begin,ahead);
begin = ahead;
}
reverse(input.begin(),input.end());
}
int main()
{
string s;
getline(cin,s);
convert(s);
cout << s << endl;
return 0;
}
93.求两个数的最小公倍数。
输入:2 3
输出:6
#include<iostream>
using namespace std;
int greatestDivisor(int n, int m){
int n1 = n > m ? n : m;
int n2 = n + m - n1;
while (n1 % n2 != 0)
{
int div = n1 % n2;
n1 = n2;
n2 = div;
}
return n2;
}
int leastMutiple(int n, int m){
int div = greatestDivisor(n,m);
return m * n / div;
}
int main()
{
int n, m;
cin >> n >> m;
cout << leastMutiple(n,m) << endl;
return 0;
}
94.中国有句俗语叫“三天打鱼两天晒网”,给定起始日期为1990年1月1号,输入终止日期,计算终止日期是在打渔还是晒网,打渔输出Fishing,晒网输出Netting。
输入:1990 01 04
输出:Netting
#include <iostream>
#include <string>
using namespace std;
int MonthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
bool isLeapYear(int year){
if (year % 100 == 0)
{
return year % 400 == 0;
}
return year % 4 == 0;
}
bool isFishing(const string &yearStr, const string &monthStr, const string &dayStr){
int year = stoi(yearStr), month = stoi(monthStr), day = stoi(dayStr), sum = -1;
for (int i = 1990; i < year; i++)
{
sum += isLeapYear(i) ? 366 : 365;
}
for (int i = 1; i < month; i++)
{
sum += MonthDays[i-1];
}
if (month > 2 && isLeapYear(year)) sum++;
sum += day;
if (sum % 5 < 3)
{
return true;
}
return false;
}
int main()
{
string s1,s2,s3;
cin >> s1 >> s2 >> s3;
cout << (isFishing(s1,s2,s3) ? "Fishing" : "Netting") << endl;
return 0;
}
95.公司设备管理
公司设备管理系统,某公司有许多员工和设备,每个员工有一个8位数字组成的ID,每台设备有个小于等于20位由数字和字母组成的设备号。
每个员工可以拥有多台设备,但是每台设备只能分配给一个员工使用。
输入四个数a,b,c,d
a:给这个系统添加多少台设备
b:从这个系统中删除多少台设备
c:添加设备和员工的对应关系
d:删除设备和员工的对应关系
之后是由这4个参数控制的输入序列
a行设备编号,若添加失败则输出”add computers error:”+设备号;成功不输出
b行删除设备的编号,若删除失败则输出”remove computers error:”+设备号;成功不输出
c行要分配给员工的设备,若添加失败则输出“assign computer error:”+设备号+“_”+员工号;成功不输出
d行要删除分配给员工的设备,若删除失败则输出”remover computer from worker error:”+设备号+“_”+员工号;成功不输出
最后要求输出总共的设备数目和排序输出员工和设备的对应关系(员工号+”_”+设备号)(先按照员工号进行排序,若员工有多台设备,则再按照设备号进行排序)。
如输入
3 2 2 1
SN001
SN002
SN003
SN003
SN004
SN002 12345678
SN001 12345678
SN001 12345678
输出:
remove computers error:SN004
1
12345678_SN002
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Employee
{
int id;
vector<string> equipments;
};
int indexOfEmployee(const vector<Employee> &v, int id){
for (unsigned i = 0; i < v.size(); i++)
{
if (v[i].id == id)
{
return i;
}
}
return -1;
}
int indexOfEquipment(const vector<string> &v, const string &s){
for (unsigned i = 0; i < v.size(); i++)
{
if (v[i] == s)
{
return i;
}
}
return -1;
}
void startAssign(){
int a, b, c, d;
cin >> a >> b >> c >> d;
vector<string> eq;
vector<Employee> ey;
string tmp;
int tmpN;
while (a-- > 0)
{
cin >> tmp;
if (tmp.size() > 20)
{
cout << "add computers error:" << tmp << endl;
continue;
}
eq.push_back(tmp);
}
while (b-- > 0)
{
cin >> tmp;
int index = indexOfEquipment(eq,tmp);
if (index == -1)
{
cout << "remove computers error:" << tmp << endl;
continue;
}
eq.erase(eq.begin()+index);
}
while (c-- > 0)
{
cin >> tmp >> tmpN;
if (indexOfEquipment(eq,tmp) == -1 || tmp.size() > 20)
{
cout << "assign computer error:" << tmp << "_" << tmpN << endl;
continue;
}
int index = indexOfEmployee(ey,tmpN);
if (index == -1)
{
Employee e;
vector<string> ee;
ee.push_back(tmp);
e.id = tmpN;
e.equipments = ee;
ey.push_back(e);
continue;
}
ey[index].equipments.push_back(tmp);
}
while (d-- > 0)
{
cin >> tmp >> tmpN;
int index = indexOfEmployee(ey,tmpN);
if (index == -1)
{
cout << "emover computer from worker error:" << tmp << "_" << tmpN << endl;
continue;
}
Employee &e = ey[index];
int eIndex = indexOfEquipment(e.equipments,tmp);
if (eIndex == -1)
{
cout << "emover computer from worker error:" << tmp << "_" << tmpN << endl;
continue;
}
e.equipments.erase(e.equipments.begin()+eIndex);
eq.erase(eq.begin()+indexOfEquipment(eq,tmp));
}
cout << eq.size() << endl;
sort(ey.begin(),ey.end(),[](const Employee &e1, const Employee &e2){
return e1.id < e2.id;
});
for (unsigned i = 0; i < ey.size(); i++)
{
Employee e = ey[i];
vector<string> ee = e.equipments;
sort(ee.begin(),ee.end());
for (unsigned j = 0; j < ee.size(); j++)
{
cout << e.id << "_" << ee[j] << endl;
}
}
}
int main()
{
startAssign();
return 0;
}
标签:
原文地址:http://blog.csdn.net/sps900608/article/details/52240187