注:编译环境为Visual Studio 2012,答案仅供参考。
86.合并输入的两个整形数组并去掉重复的数字按升序输出;
案例输入:3 1 2 5 -10 3 2
案例输出:-10 1 2 3 5
#include
#include
#include
using namespace std;
void combine(int *a1, int n1, int *a2, int n2, vector<int> &v){
vector<int> v1(a1,a1+n1);
vector<int> v2(a2,a2+n2);
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
auto b1 = v1.begin(), b2 = v2.begin();
while (b1 != v1.end() && b2 != v2.end())
{
if (*b1 == *b2)
{
v.push_back(*b1++);
b2++;
}
else if (*b1 < *b2)
{
v.push_back(*b1++);
}
else
{
v.push_back(*b2++);
}
}
while (b1 != v1.end())
{
v.push_back(*b1++);
}
while (b2 != v2.end())
{
v.push_back(*b2++);
}
}
int main()
{
int a1[] = {3,1,2,5}, a2[] = {-10,3,2};
vector<int> v;
combine(a1,4,a2,3,v);
for (unsigned i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
87.开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A; $%$
; YAD; 等。
下面是一个简单的例子 如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
处理过程:
起点(0,0)
+ A10 = (-10,0)
+ S20 = (-10,-20)
+ W10 = (-10,-10)
+ D30 = (20,-10)
+ x = 无效
+ A1A = 无效
+ B10A11 = 无效
+ 一个空 不影响
+ A10 = (10,-10)
结果 (10, -10)
样例输入:A10;S20;W10;D30;X;A1A;B10A11;;A10;
样例输出:10,-10
#include
#include
#include
using namespace std;
enum Direction
{
up = 0,
down = 1,
left = 2,
right =3
};
bool isRight(const string s, Direction &direction, int &distance){
if (s.size() < 2 || s.size() > 3)
{
return false;
}
switch (s[0])
{
case('W'):
direction = Direction::up;
break;
case('S'):
direction = Direction::down;
break;
case('A'):
direction = Direction::left;
break;
case('D'):
direction = Direction::right;
break;
default:
return false;
}
for (unsigned i = 1; i < s.size(); i++)
{
if (s[i] < '0' || s[i] > '9')
{
return false;
}
}
distance = stoi(string(s.begin()+1,s.end()));
return true;
}
void deal(int &x, int &y, vector<string> v){
for (unsigned i = 0; i < v.size(); i++)
{
Direction direction;
int distance;
if (!isRight(v[i],direction,distance))
{
continue;
}
switch (direction)
{
case Direction::up:
y += distance;
break;
case Direction::down:
y -= distance;
break;
case Direction::left:
x -= distance;
break;
case Direction::right:
x += distance;
break;
default:
break;
}
}
}
int main()
{
string s;
getline(cin,s);
auto begin = s.begin();
vector<string> v;
while (begin != s.end())
{
if (*begin == ';')
{
begin++;
continue;
}
auto ahead = begin + 1;
while (ahead != s.end() && *ahead !=';')
{
ahead++;
}
v.push_back(string(begin,ahead));
begin = ahead;
}
int x = 0, y = 0;
deal(x,y,v);
cout << x << "," << y << endl;
return 0;
}
88.汽水瓶
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
输入:输入文件最多包含10组测试数据,每个数据占一行,仅包含一个正整数n(1<=n<=100),表示小张手上的空汽水瓶数。n=0表示输入结束,你的程序不应当处理这一行。
输出:对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
样例输入:3 10 81 0
样例输出:1 5 40
#include
using namespace std;
void drink(int n, int &sum){
int tmp = n / 3;
sum += tmp;
tmp += n%3;
if (tmp >= 3)
{
drink(tmp,sum);
}
else if (tmp == 2)
{
sum++;
}
}
int main()
{
int n;
vector<int> v;
while (cin >> n && n != 0)
{
int sum = 0;
drink(n,sum);
v.push_back(sum);
}
for (unsigned i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
return 0;
}
89.输出一个字符串第一个只出现一次的字符,若没有只出现一次的字符则输出’.’:
例如:
输入 aabdgfd或aassnnbb
输出 b或者.
#include
#include
using namespace std;
char findOnce(const string &s){
int a[256];
for (int i = 0; i < 256; i++)
{
a[i] = 0;
}
for (unsigned i = 0; i < s.size(); i++)
{
a[s[i]]++;
}
for (unsigned i = 0; i < s.size(); i++)
{
if (a[s[i]] == 1)
{
return s[i];
}
}
return '.';
}
int main()
{
string s;
getline(cin,s);
cout << findOnce(s) << endl;
return 0;
}
90.自守数是指一个数的平方的尾数等于该数自身的自然数。例如:252= 625,762= 5776,93762= 87909376。请求出n以内的自守数的个数
#include
using namespace std;
bool isRight(int n){
int n2 = n * n;
while (n != 0)
{
if (n%10 != n2%10)
{
return false;
}
n /= 10;
n2 /= 10;
}
return true;
}
int countNum(int n){
int count = 0;
for (int i = 0; i < n; i++)
{
if (isRight(i))
{
count++;
}
}
return count;
}
int main()
{
int n;
cin >> n;
cout << countNum(n) << endl;
return 0;
}