A - Ignatius and the Princess IV
“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?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1
#include<bits/stdc++.h>
using namespace std;
int N[999999];
bool Less(int A,int B) {
return A>B;
}
int main() {
int n;
int i=0;
while(cin>>n) {
memset(N,0,sizeof(N));
i=0;
int mask=0;
int temp=(n+1)/2;
int num;
while(n--) {
cin>>num;
N[num]++;
if(N[num]>=temp) {
mask=num;
}
}
cout<<mask<<endl;
}
}
/*
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
*/
C - Humble Numbers
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, … shows the first 20 humble numbers.
Write a program to find and print the nth element in this sequence
Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line saying “The nth humble number is number.”. Depending on the value of n, the correct suffix “st”, “nd”, “rd”, or “th” for the ordinal number nth has to be used like it is shown in the sample output.
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long const maxn=2000000000;
int cmp(const void *a,const void *b)
{
return *(long long *)a>*(long long *)b?1:-1;
}
int cul(int a)
{
int counter=0;
long long temp=1;
while(temp<maxn)
{
temp=temp*a;
counter++;
}
return counter;
}
int main(int argc, const char * argv[]) {
long long a[999999];
int k2,k3,k5,k7,counter=0;
k2=cul(2);
k3=cul(3);
k5=cul(5);
k7=cul(7);
cout<<k2<<" "<<k3<<" "<<k5<<" "<<k7<<endl;// 31 20 14 12
for(int c2=0;c2<=k2;c2++)
{
for(int c3=0;c3<=k3;c3++)
{
for(int c5=0;c5<=k5;c5++)
{
for(int c7=0;c7<=k7;c7++)
{
long long temp;
temp=pow(2,c2)*pow(3,c3)*pow(5,c5)*pow(7,c7);
if(temp<maxn)
{
a[counter]=temp;
counter++;
}
}
}
}
}
counter++;
qsort(a,counter,sizeof(a[0]),cmp);
for(int i=0;i<counter;i++) if(a[i]>=0)cout<<a[i]<<",";
cout<<endl;
cout<<counter;
return 0;
}