当前位置:网站首页>12 至少是其他数字两倍的最大数
12 至少是其他数字两倍的最大数
2022-07-17 00:07:00 【DHU杨骅麟(紫外线过敏)】
12 至少是其他数字两倍的最大数
作者: Turbo时间限制: 1S章节: 课程设计
问题描述 :
给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。
请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1 。
示例 1:
输入:
4
3 6 1 0
输出:1
解释:6 是最大的整数,对于数组中的其他整数,6 至少是数组中其他元素的两倍。6 的下标是 1 ,所以返回 1 。
示例 2:
输入:
4
1 2 3 4
输出:-1
解释:4 没有超过 3 的两倍大,所以返回 -1 。
示例 3:
输入:
1
1
输出:0
解释:因为不存在其他数字,所以认为现有数字 1 至少是其他数字的两倍。
输入说明 :
输入两行:
第一行输入一个整数n表示数组nums的长度。
第二行输入n个整数表示数组的元素。
提示:
1 <= n <= 100
0 <= nums[i] <= 100
nums 中的最大元素是唯一的
输出说明 :
输出一个整数表示结果。
输入范例 :
5
5 4 3 2 1
输出范例 :
-1
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
int data = 0;
int sequence = 0;
};
bool cmp(student x, student y)
{
return x.data > y.data;
}
int main()
{
student arr[10001] ;
int n = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i].data;
arr[i].sequence = i;
}
sort(arr, arr + n, cmp);
if (arr[0].data > 2 * arr[1].data)
{
cout << arr[0].sequence;
}
else
{
cout << -1;
}
return 0;
}边栏推荐
- 2022.7.7 summary of some errors
- tp-watermark.js网页添加水印插件
- Colorful text advertising code, text advertising code beautification version, add text advertising tutorials to the website
- Nodejs cross domain CORS
- 微信推送-模版消息参数配置
- Single page application spa and multi page application MPa
- Node的数据库编程(MySQL),增删改查
- 替换URL中的特殊字符(%E2%80%8B)
- Basic use of promise
- Express的使用方法,路由的匹配与使用
猜你喜欢
随机推荐
红日安全靶场3
uni-app微信小程序——商城(4)——商家
router和keep-alive
XSS simple summary
页面布局——三栏布局解决方式
v-cloak与v-bind绑定class
let和var的区别
2022年暑假ACM热身练习2(总结)
Es optional chain
Quine injection of SQL injection
The difference between let and VaR
JS replaces a character in the string, and JS modifies the specified character in the string
How does the website count the number of visitors? How to install and use 51la?
uni-app微信小程序——商城(7)——商品详情
Express的使用方法,路由的匹配与使用
Page layout - three column layout solution
PCRE bypasses regular
Summary of XML external entity injection (xxE target recurrence)
05_ Review object defineProperty
Summary of Applied Cryptography









