不要什么都依赖数组
2017-07-21 10:44
183 查看
有些题目不用数组反而更简单 依次输入数据,每输入一个数据就对其进行操作,判断
There is a mysterious organization called Time-Space Administrative Bureau (TSAB) in the deep universe that we humans have not discovered yet. This year, the TSAB decided to elect an outstanding member from its elite troops. The elected guy will
be honored with the title of "Ace of Aces".
After voting, the TSAB received N valid tickets. On each ticket, there is a numberAi denoting the ID of a candidate. The candidate with the most tickets nominated will be elected as the "Ace of Aces". If there are two or
more candidates have the same number of nominations, no one will win.
Please write program to help TSAB determine who will be the "Ace of Aces".
Input
There are multiple test cases. The first line of input contains an integer
T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 1000). The next line containsN integers
Ai (1 <= Ai <= 1000).
Output
For each test case, output the ID of the candidate who will be honored with "Ace of Aces". If no one win the election, output "Nobody" (without quotes) instead.
Sample Input
Sample Output
题目大意:有N组数据, 每组数据有n个数代表n个评委 每个评委都有一次投票机会可以投给他们喜欢的那一号选手,然后每个评委投一次票, 第一组输入示例 2 2 2 1 1 代表有三个评委投了二号选手,两个评委投了1号选手 所以自然2号选手胜出,第二组 1号 2号选手分别得两票 所以无法选出优胜者
提示:
依次输入n个数 每输入一个就对其进行操作, 判断 用数组反而相当相当麻烦
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define PI 3.1415926
using namespace std;
int a[1001];
int N;
int main()
{
scanf("%d",&N);
while(N--)
{
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
int p = 0;
int q = 0;
int c;
while(n--)
{
int b;
scanf("%d",&b);
a[b]++;
if(a[b]>=p)
{
q=p;
p=a[b];
c=b;
}
}
if (q==p)
cout<<"Nobody"<<endl;
else
cout<< c <<endl;
}
return 0;
}
There is a mysterious organization called Time-Space Administrative Bureau (TSAB) in the deep universe that we humans have not discovered yet. This year, the TSAB decided to elect an outstanding member from its elite troops. The elected guy will
be honored with the title of "Ace of Aces".
After voting, the TSAB received N valid tickets. On each ticket, there is a numberAi denoting the ID of a candidate. The candidate with the most tickets nominated will be elected as the "Ace of Aces". If there are two or
more candidates have the same number of nominations, no one will win.
Please write program to help TSAB determine who will be the "Ace of Aces".
Input
There are multiple test cases. The first line of input contains an integer
T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 1000). The next line containsN integers
Ai (1 <= Ai <= 1000).
Output
For each test case, output the ID of the candidate who will be honored with "Ace of Aces". If no one win the election, output "Nobody" (without quotes) instead.
Sample Input
3 5 2 2 2 1 1 5 1 1 2 2 3 1 998
Sample Output
2 Nobody 998
题目大意:有N组数据, 每组数据有n个数代表n个评委 每个评委都有一次投票机会可以投给他们喜欢的那一号选手,然后每个评委投一次票, 第一组输入示例 2 2 2 1 1 代表有三个评委投了二号选手,两个评委投了1号选手 所以自然2号选手胜出,第二组 1号 2号选手分别得两票 所以无法选出优胜者
提示:
依次输入n个数 每输入一个就对其进行操作, 判断 用数组反而相当相当麻烦
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define PI 3.1415926
using namespace std;
int a[1001];
int N;
int main()
{
scanf("%d",&N);
while(N--)
{
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
int p = 0;
int q = 0;
int c;
while(n--)
{
int b;
scanf("%d",&b);
a[b]++;
if(a[b]>=p)
{
q=p;
p=a[b];
c=b;
}
}
if (q==p)
cout<<"Nobody"<<endl;
else
cout<< c <<endl;
}
return 0;
}
相关文章推荐
- C语言声明数组变量时,在什么情况下,可不指定数组大小
- 字符数组和字符串有什么区别:
- java中数组定义String a[]和String[] a有什么区别?
- 在Javascript中什么是伪数组?如何将伪数组转化为标准数组?考虑兼容性
- 基础不牢,就不要什么花架子
- 什么时候都不要延长迭代或Sprint
- 什么是依赖注入
- 什么是数组名?----一个让你吃惊的事实!
- 条款3:绝对不要以多态(polymorphically)方式处理数组
- 从头认识java-13.11 对比数组与泛型容器,观察类型擦除给泛型容器带来什么问题?
- 虽然写管理类软件非常依赖数据库,但是建议您不要过度依赖数据库,我们不要把思维只局限在存取数据库上
- 什么才是正确的javascript数组检测方式
- 虽然写管理类软件非常依赖数据库,但是建议您不要过度依赖数据库,我们不要把思维只局限在存取数据库上
- 什么时候能够不要积分下载
- 什么是 哈希表 HashMap 中数组的 size 为什么必须是 2 的整数次幂
- 宏和枚举有什么区别,定义数组时使用枚举和使用宏的区别
- 不要用for in语句对数组进行遍历
- 什么是Spring的依赖注入?
- 不要变得迟钝,努努力,什么都迎刃而解