科大讯飞 7.31: 100% 0 100% 100%
## 题目一:钱币,找最少数量问题
有1元、5元、10元、50元、100元不同面额的钱; 第一行输入不同面额的钱对应的数量,第二行输入需要支付的价格; 需要满足支付价格且使用钱的数量最少;
输出满足的最小数量,没有符合的输出-1
```cpp
#include<iostream>
using namespace std;
int main() {
int num[5];//每个面额的个数
for (int i = 0; i < 5; i++) {
cin >> num[i];
}
int need;
cin >> need;//需要支付的钱数
int n[5] = { 0 };
int all = 0;
n[4] = need / 100;//需要这么多个百
if (n[4] <= num[4] && n[4])//面额100够多
all += n[4];
else if (n[4]) {
all += num[4];//面额100不够多,all in
n[3] = 2 * (n[4] - num[4]);//面额50
num[4] = 0;
if (n[3] <= num[3])//面额50够多
{
all += n[3];
num[3] -= n[3];
}
else {
all += num[3];
n[2] = 5 * (n[3] - num[3]);//面额10
num[3] = 0;
if (n[2] <= num[2]) {
all += n[2];
num[2] -= n[2];
}
else {
all += num[2];
n[1] = 2 * (n[2] - num[2]);//面额5
num[2] = 0;
if (n[1] <= num[1]) {//面额5够多
all += n[1];
num[1] -= n[1];
}
else {//面额5不够
all += num[1];
n[0] = 5 * (n[1] - num[1]);//面额1
num[1] = 0;
if (n[0] <= num[0]) {//面额1够多
all += n[0];
num[0] -= n[0];
}
else {
all = -1;
cout << all;
return 0;
}
}
}
}
}
n[2] = (need % 100) / 10;
if (n[2] >= 5 && num[3]) {
all++;
n[2] -= 5;
}
if (n[2] <= num[2])//面额10
{
all += n[2];
}
else {
all += num[2];
n[1] = 2 * (n[2] - num[2]);//面额5
if (n[1] <= num[1])
{
all += n[1];
num[1] -= n[1];
}
else {
all += num[1];
n[0] = 5 * (n[1] - num[1]);//面额1
n[1] = 0;
if (n[0] <= num[0]) {
all += n[0];
num[0] -= n[0];
}
else {
all = -1;
cout << all; return 0;
}
}
}
n[0] = need % 10;
if (n[0] >= 5 && num[1]) {
all++;
n[0] -= 5;
}
if (n[0] <= num[0])//
all += n[0];
else
all = -1;
cout << all;
return 0;
}
```
## 题目二: 排序问题
给出一个数字序列,需要输出排序过程
```cpp
//类似快排的,但是没整出来
```
## 题目三:两矩形相交问题
给出8个数据,前四个数据代表第一个矩形的对角线上的点坐标,后四个数据代表第二个矩形的对角线上的点坐标
判断两矩形能否相交,能则输出1,否则输出0
```cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
int n[4],n2[4];
for (int i = 0; i < 4; i++) {
cin >> n[i];
}
for (int i = 0; i < 4; i++) {
cin >> n2[i];
}
int x1[2],y1[2];
int x2[2], y2[2];
x1[0] = n[0] < n[2] ? n[0]: n[2];//x最小
x1[1]= n[0] > n[2] ? n[0] : n[2];//x最大
y1[0] = n[1] < n[3] ? n[1] : n[3];//y最小
y1[1] = n[1] > n[3] ? n[1] : n[3];//y最大
x2[0] = n2[0] < n2[2] ? n2[0] : n2[2];//x最小
x2[1] = n2[0] > n2[2] ? n2[0] : n2[2];//x最大
y2[0] = n2[1] < n2[3] ? n2[1] : n2[3];//y最小
y2[1] = n2[1] > n2[3] ? n2[1] : n2[3];//y最大
bool flag=false;
for (int i = 0; i < 2; i++) {
if (x2[i] >= x1[0] && x2[i] <= x1[1]) {
for (int j = 0; j < 2; j++) {
if (y2[j] >= y1[0] && y2[j] <= y1[1])
flag = true;
}
}
}
if (flag) {
cout << "1";
}
else {
cout << "0";
}
return 0;
}
```
题目四:输入任意字符串,从字符串中提取整数
```cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
getline(cin, str);////遇回车结束,可以读入空格等除回车之外的其他字符
int i = 0;
while (str[i] != '\0') {
if (str[i]>='0'&&str[i] <= '9') {
cout << str[i];
}
else if (str[i] == '-' && (str[i + 1] >= '0'&&str[i + 1] <= '9')) {
cout << "-";
}
i++;
}
return 0;
}
```
#科大讯飞##笔试题目#
有1元、5元、10元、50元、100元不同面额的钱; 第一行输入不同面额的钱对应的数量,第二行输入需要支付的价格; 需要满足支付价格且使用钱的数量最少;
输出满足的最小数量,没有符合的输出-1
```cpp
#include<iostream>
using namespace std;
int main() {
int num[5];//每个面额的个数
for (int i = 0; i < 5; i++) {
cin >> num[i];
}
int need;
cin >> need;//需要支付的钱数
int n[5] = { 0 };
int all = 0;
n[4] = need / 100;//需要这么多个百
if (n[4] <= num[4] && n[4])//面额100够多
all += n[4];
else if (n[4]) {
all += num[4];//面额100不够多,all in
n[3] = 2 * (n[4] - num[4]);//面额50
num[4] = 0;
if (n[3] <= num[3])//面额50够多
{
all += n[3];
num[3] -= n[3];
}
else {
all += num[3];
n[2] = 5 * (n[3] - num[3]);//面额10
num[3] = 0;
if (n[2] <= num[2]) {
all += n[2];
num[2] -= n[2];
}
else {
all += num[2];
n[1] = 2 * (n[2] - num[2]);//面额5
num[2] = 0;
if (n[1] <= num[1]) {//面额5够多
all += n[1];
num[1] -= n[1];
}
else {//面额5不够
all += num[1];
n[0] = 5 * (n[1] - num[1]);//面额1
num[1] = 0;
if (n[0] <= num[0]) {//面额1够多
all += n[0];
num[0] -= n[0];
}
else {
all = -1;
cout << all;
return 0;
}
}
}
}
}
n[2] = (need % 100) / 10;
if (n[2] >= 5 && num[3]) {
all++;
n[2] -= 5;
}
if (n[2] <= num[2])//面额10
{
all += n[2];
}
else {
all += num[2];
n[1] = 2 * (n[2] - num[2]);//面额5
if (n[1] <= num[1])
{
all += n[1];
num[1] -= n[1];
}
else {
all += num[1];
n[0] = 5 * (n[1] - num[1]);//面额1
n[1] = 0;
if (n[0] <= num[0]) {
all += n[0];
num[0] -= n[0];
}
else {
all = -1;
cout << all; return 0;
}
}
}
n[0] = need % 10;
if (n[0] >= 5 && num[1]) {
all++;
n[0] -= 5;
}
if (n[0] <= num[0])//
all += n[0];
else
all = -1;
cout << all;
return 0;
}
```
## 题目二: 排序问题
给出一个数字序列,需要输出排序过程
```cpp
//类似快排的,但是没整出来
```
## 题目三:两矩形相交问题
给出8个数据,前四个数据代表第一个矩形的对角线上的点坐标,后四个数据代表第二个矩形的对角线上的点坐标
判断两矩形能否相交,能则输出1,否则输出0
```cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
int n[4],n2[4];
for (int i = 0; i < 4; i++) {
cin >> n[i];
}
for (int i = 0; i < 4; i++) {
cin >> n2[i];
}
int x1[2],y1[2];
int x2[2], y2[2];
x1[0] = n[0] < n[2] ? n[0]: n[2];//x最小
x1[1]= n[0] > n[2] ? n[0] : n[2];//x最大
y1[0] = n[1] < n[3] ? n[1] : n[3];//y最小
y1[1] = n[1] > n[3] ? n[1] : n[3];//y最大
x2[0] = n2[0] < n2[2] ? n2[0] : n2[2];//x最小
x2[1] = n2[0] > n2[2] ? n2[0] : n2[2];//x最大
y2[0] = n2[1] < n2[3] ? n2[1] : n2[3];//y最小
y2[1] = n2[1] > n2[3] ? n2[1] : n2[3];//y最大
bool flag=false;
for (int i = 0; i < 2; i++) {
if (x2[i] >= x1[0] && x2[i] <= x1[1]) {
for (int j = 0; j < 2; j++) {
if (y2[j] >= y1[0] && y2[j] <= y1[1])
flag = true;
}
}
}
if (flag) {
cout << "1";
}
else {
cout << "0";
}
return 0;
}
```
题目四:输入任意字符串,从字符串中提取整数
```cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
getline(cin, str);////遇回车结束,可以读入空格等除回车之外的其他字符
int i = 0;
while (str[i] != '\0') {
if (str[i]>='0'&&str[i] <= '9') {
cout << str[i];
}
else if (str[i] == '-' && (str[i + 1] >= '0'&&str[i + 1] <= '9')) {
cout << "-";
}
i++;
}
return 0;
}
```
