分析
四道编程题,其中三道难度一般,麻烦的是只能使用C++或JAVA,对Python党很不友好。
1.最少纸币数
从大到小遍历,然后计算整除数和该纸币的个数的小的那个,然后依次向下计算,如果最后剩余钱数的话说明不能完成交换
2.排序
没看明白究竟是啥排序方法,看着像快排但推了一遍感觉又不是
3.矩形相交判断
计算出每个矩形四个点的坐标,然后看这四个点是否在另一个矩形里面
4.字符串处理
- 注意需要对开头负号的处理
具体代码(不怎么会C++,请多担待: )
- 1
#include <iostream>
using namespace std;
int _main() {
int a,b,c,d,e;
int value;
int prices[] = {100,50,10,5,1};
while(cin >> a >> b >> c >> d >> e)// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
{
cin >> value;
int nums[] = {e,d,c,b,a};
int all_cnt = 0;
for ( int i = 0;i < 5;i++)
{
int cur_cnt = value/prices[i];
if(cur_cnt > nums[i])
cur_cnt = nums[i];
value -= cur_cnt*prices[i];
all_cnt += cur_cnt;
}
if(value == 0)
cout << all_cnt << endl;
else
cout << -1 << endl;
}
}
- 3
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int,int>> buildRectangle(int x1,int y1,int x2,int y2){
vector<pair<int,int>> rectangle;
rectangle.push_back(make_pair(x1,y1));
rectangle.push_back(make_pair(x2,y2));
rectangle.push_back(make_pair(x1,y2));
rectangle.push_back(make_pair(x2,y1));
return rectangle;
}
// 判断rect1的四个顶点是不是在rect2的范围内
int solver(vector<pair<int,int>> rect,int x1,int y1,int x2,int y2){
for (auto p : rect){
int x = p.first,y = p.second;
if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
return 1;
}
return 0;
}
int main3() {
int x1,y1,x2,y2,x3,y3,x4,y4;
while(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4)
{
vector<pair<int,int>> rect1 = buildRectangle(x1,y1,x2,y2);
vector<pair<int,int>> rect2 = buildRectangle(x3,y3,x4,y4);
if (solver(rect1,x3,y3,x4,y4) || solver(rect2,x1,y1,x2,y2))
cout << 1 << endl;
else
cout << 0 << endl;
}
}
- 4
#include <iostream>
using namespace std;
int main_4() {
string s;
string res;
while(cin >> s)
{
res = "";
for (auto c:s)
{
if (res.empty() and c == '-')
res += c;
if((res.empty() || res=="-") && c=='0')
continue;
if(48 <= (int)c && (int)c <= 57)
res += c;
}
cout << res;
}
}
全部评论
(7) 回帖