Ignatius and the Princess IV
来源:互联网 发布:nba2k14中文版下载软件 编辑:程序博客网 时间:2024/05/19 06:49
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1
351
先来一个比较水的做法,没想到就过了。
#include <bits/stdc++.h>using namespace std;const int MAXN = 1e6+7;int n,m;int num[MAXN];int main(){ while(~scanf("%d",&n)) { for(int i = 1 ; i <= n ; ++i) { scanf("%d",&num[i]); } sort(num+1,num+n+1); int ans = 0,sum = 1,pos; for(int i = 2 ; i <= n ; ++i) { if(num[i] == num[i-1]) { sum ++; } else { if(sum > ans) { ans = sum; pos = num[i-1]; } sum = 1; } } if(sum > ans) { ans = sum; pos = num[n]; } printf("%d\n",pos); } return 0;}
然后就是不开数组不排序的做法:
我们很容易的看出来,在一个序列中如果去掉2个不同的元素,那么原序列中的多元素,在新的序列中还是多元素,
哪里很容易了...#include <bits/stdc++.h>using namespace std;const int MAXN = 1e6+7;int n,m;int main(){ while(~scanf("%d",&n)) { int ans = 0,sum = 0; for(int i = 1 ; i <= n ; ++i) { scanf("%d",&m); if(!sum) { sum ++; ans = m; } else { if(m == ans)sum++; else sum --; } } printf("%d\n",ans); } return 0;}
阅读全文
1 0
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- 插入排序与希尔排序详解
- 键盘事件
- 整数排序
- pandas.DataFrame.fillna 的几种method
- 网路协议
- Ignatius and the Princess IV
- Android开发中的三种提示(Dialog,Toast,Snackbar)实现
- Java并发编程:Thread类的使用
- 在主方法中创建String型变量,实现字符变量的大小写转换,并将转换后的结果输出
- 右键菜单事件
- [笔记]: 树链剖分
- IO流的一些知识点
- uva10474
- 0.masm32-搭建环境和MessageBoxA弹框