SOJ 1050. Numbers & Letters

题目大意:给出5个整数和4种运算(加法,减法,乘法和除法),选取其中的部分数作任意运算,使得值等于目标数或者最接近目标数。其中,除法只能整除,即商必须是整数。

解题思路:每次选取任意两个数,进行可行的运算,获得结果并且和目标数作比较。最后得出答案。

代码如下:

  1 #include <iostream>
  2 //#include <ctime>
  3 #include <vector>
  4 #include <climits>
  5 using namespace std;
  6
  7 const int maxn = 5;
  8 int target;
  9 int approximation;
 10 int n;
 11
 12 bool test(const int &res) {
 13     bool flag = false;;
 14     if (res == target) {
 15         approximation = res;
 16         flag = true;
 17     } else if (res < target) {
 18         approximation = approximation > res ? approximation : res;
 19     }
 20     return flag;
 21 }
 22
 23 int add(const int & a, const int & b) {
 24     return a + b;
 25 }
 26
 27 int sub(const int & a, const int & b) {
 28     return a - b;
 29 }
 30
 31 int mul(const int & a, const int & b) {
 32     return a * b;
 33 }
 34
 35 int div(const int & a, const int & b, bool &flag) {
 36     int res = 0;
 37
 38     if (a < b) {
 39         if (a != 0 && b % a == 0) {
 40             res = b / a;
 41             flag = true;
 42         } else {
 43             flag = false;
 44         }
 45     } else {
 46         if (b != 0 && a % b == 0) {
 47             res = a / b;
 48             flag = true;
 49         } else {
 50             flag = false;
 51         }
 52     }
 53
 54     return res;
 55 }
 56
 57 void deal(const vector<int> & nums) {
 58     if (approximation == target) return;
 59     if (nums.size() < 2) return;
 60
 61     for (int i = 0; i < nums.size(); i++) {
 62         for (int j = i + 1; j < nums.size(); j++) {
 63             vector<int> nnums;
 64             nnums.insert(nnums.end(), nums.begin(), nums.begin() + i);
 65             nnums.insert(nnums.end(), nums.begin() + i + 1, nums.begin() + j);
 66             nnums.insert(nnums.end(), nums.begin() + j + 1, nums.end());
 67
 68             int res;
 69             res = add(nums[i], nums[j]);
 70             if (test(res)) return;
 71             nnums.push_back(res);
 72             deal(nnums);
 73
 74             nnums.pop_back();
 75
 76             res = sub(nums[i], nums[j]);
 77             if (test(res)) return;
 78             nnums.push_back(res);
 79             deal(nnums);
 80
 81             nnums.pop_back();
 82
 83             res = sub(nums[j], nums[i]);
 84             if (test(res)) return;
 85             nnums.push_back(res);
 86             deal(nnums);
 87
 88             nnums.pop_back();
 89
 90             res = mul(nums[i], nums[j]);
 91             if (test(res)) return;
 92             nnums.push_back(res);
 93             deal(nnums);
 94
 95             nnums.pop_back();
 96
 97             bool flag;
 98             res = div(nums[i], nums[j], flag);
 99             if (flag == false) continue;
100             if (test(res)) return;
101             nnums.push_back(res);
102             deal(nnums);
103
104             nnums.pop_back();
105
106
107         }
108     }
109 }
110
111 int main() {
112     cin >> n;
113     while (n--) {
114         vector<int> nums;
115         int temp;
116         for (int i = 0; i < maxn; i++) {
117             cin >> temp;
118             nums.push_back(temp);
119         }
120         cin >> target;
121         approximation = -INT_MAX;
122
123         //double start = clock();
124         deal(nums);
125         //double end = clock();
126         //cout << (end - start) / CLOCKS_PER_SEC << endl;
127
128         cout << approximation << endl;
129
130     }
131     return 0;
132 }
时间: 2024-10-14 10:57:37

SOJ 1050. Numbers & Letters的相关文章

Sicily 1050. Numbers &amp; Letters

1050. Numbers & Letters Constraints Time Limit: 3 secs, Memory Limit: 32 MB Description In the early 80's, a popular TV show on Dutch television was 'Cijfers en Letters' (Numbers and Letters). This game consisted of two game elements, in which the ma

sicily 1050 深度优先搜索解题

1050. Numbers & Letters Constraints Time Limit: 3 secs, Memory Limit: 32 MB Description In the early 80’s, a popular TV show on Dutch television was ‘Cijfers en Letters’ (Numbers and Letters). This game consisted of two game elements, in which the ma

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

[转]8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

docker搭建wordpress

闲话少说,奉上干货 Author : woodman Version: 1.0 实验环境 系统:CentOS Linux release 7.2.1511 内核:Linux C7 3.10.0-327.el7.x86_64 软件源 163+epel yum -y install wget vim cd /etc/yum.repos.d/ rm -f ./* wget http://mirrors.163.com/.help/CentOS7-Base-163.repo rpm -ivh  http

走进ReactiveCocoa的世界

在学习ReactiveCocoa之前,先学习一下概念 ReactiveCocoa 是一套开源的基于Cocoa的FRP框架 .FRP的全称是Functional Reactive Programming,中文译作函数式响应式编程,是RP(Reactive Programm,响应式编程)的FP(Functional Programming,函数式编程)实现.说起来很拗口.太多的细节不多讨论,我们先关注下FRP的FP特征. 函数式编程 函数式编程,简单来说,就是多使用匿名函数,将逻辑处理过程,以一系列

使用ReactiveCocoa实现iOS平台响应式编程

使用ReactiveCocoa实现iOS平台响应式编程 ReactiveCocoa和响应式编程 在说ReactiveCocoa之前,先要介绍一下FRP(Functional Reactive Programming,响应式编程),在维基百科中有这样一个例子介绍: 在命令式编程环境中,a = b + c 表示将表达式的结果赋给a,而之后改变b或c的值不会影响a.但在响应式编程中,a的值会随着b或c的更新而更新. Excel就是响应式编程的一个例子.单元格可以包含字面值或类似"=B1+C1″的公式,

李炎恢PHP笔记

Print Echo print 输出 转型 $sum=0; $total=(float)$sum Isset()   unset()判断一个变量是否存在 $echo intval$sum 浮点型 Define(“total”,100); 常量一旦定义就不可更改 判断句 <?php If ($userAge>14){ Echo’内容’:} ?> <?php If ($userAge>14){ Echo’内容’:} Else{echo ‘wufawutian’}; ?>