博客
关于我
华为社招笔试
阅读量:410 次
发布时间: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/

你可能感兴趣的文章
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT介绍及与其他协议的比较
查看>>
MQTT工作笔记0007---剩余长度
查看>>
MQTT工作笔记0008---服务质量
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>
Mqtt搭建代理服务器进行通信-浅析
查看>>
MS COCO数据集介绍
查看>>
MS Edge浏览器“STATUS_INVALID_IMAGE_HASH“兼容性问题
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS SQL查询库、表、列数据结构信息汇总
查看>>
MS UC 2013-0-Prepare Tool
查看>>
MSBuild 教程(2)
查看>>
msbuild发布web应用程序
查看>>
MSB与LSB
查看>>
MSCRM调用外部JS文件
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
MsEdgeTTS开源项目使用教程
查看>>
msf
查看>>