博客
关于我
华为社招笔试
阅读量:403 次
发布时间:2019-03-06

本文共 1870 字,大约阅读时间需要 6 分钟。

输入10个整数,从中选出3个,使得x^2+xy-y^2+z的值最小.

例子:

输入: 1 3 1 1 1 1 1 1 1 1 

输出:-4

 

//最初版本:比较傻的版本.

#include
#include
#include
using namespace std;int main(){ int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; while (cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >> a10) { int arr[10] = { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 }; vector
vecInt(arr, arr+10); vector
vecRes; for (int i = 0; i < vecInt.size(); i++) { for (int j = 0; j < vecInt.size(); j++) { for (size_t k = 0; k < vecInt.size(); k++) { if (i != j && j != k && k != i) { int x = vecInt[i]; int y = vecInt[j]; int z = vecInt[k]; int result = x*x+x*y-y*y+z; vecRes.push_back(result); } else { } } } } sort(vecRes.begin(), vecRes.end()); cout << vecRes[0]; } return 0;}

 

//对输入进行优化

#include
#include
#include
#include
using namespace std;int main(){ int num; vector
vecInt; while (cin >>num) //如果一行输入多个数,windows下Ctrl+Z会终止输入. { vecInt.push_back(num); } vector
vecRes; for (int i = 0; i < vecInt.size(); i++) { for (int j = 0; j < vecInt.size(); j++) { for (size_t k = 0; k < vecInt.size(); k++) { if (i != j && j != k && k != i) { int x = vecInt[i]; int y = vecInt[j]; int z = vecInt[k]; int result = x*x + x*y - y*y + z; vecRes.push_back(result); } else { } } } } sort(vecRes.begin(), vecRes.end()); cout << vecRes[0]; return 0;}

 

转载地址:http://ydbkz.baihongyu.com/

你可能感兴趣的文章
80个Python经典资料(教程+源码+工具)汇总——下载目录
查看>>
8个微信实用技巧,你知道多少?
查看>>
8点FFT的C语言实现
查看>>
950个织梦网dede模板源码
查看>>
: bad interpreter: 没有那个文件或目录
查看>>
@Cacheable@CacheEvict@CachePut
查看>>
2018信息平台专场招聘
查看>>
@ControllerAdvice+@ExceptionHandler全局处理Controller层异常 及其 原理
查看>>
@ControllerAdvice、@ExceptionHandler控制全局Controller异常
查看>>
@ControllerAdvice用法
查看>>
#VERDI# 关于Verdi使用的几个常用技巧整理
查看>>
@Resource注解的使用
查看>>
@ResponseBody 和 @RequestBody
查看>>
A + B 九度oj
查看>>
A DBA’s take on MSCA (Mobile supply chain applications)
查看>>
A DBA’s take on MSCA (Mobile supply chain applications)
查看>>
A20地址线
查看>>
abaqus质量缩放系数取值_ABAQUS的质量缩放
查看>>
Access restriction: The type FileURLConnection is not accessible due to restriction
查看>>
Accessibility
查看>>