首页 > ACM题库 > HDU-杭电 > hdu 2654 Become A Hero-模拟-[解题报告]C++
2014
02-12

hdu 2654 Become A Hero-模拟-[解题报告]C++

Become A Hero

问题描述 :

Lemon wants to be a hero since he was a child. Recently he is reading a book called “Where Is Hero From” written by ZTY. After reading the book, Lemon sends a letter to ZTY. Soon he recieves a reply.

Dear Lemon,
It is my way of success. Please caculate the algorithm, and secret is behind the answer. The algorithm follows:
Int Answer(Int n)
{
…….Count = 0;
…….For (I = 1; I <= n; I++)
…….{
…………..If (LCM(I, n) < n * I)
………………..Count++;
…….}
…….Return Count;
}
The LCM(m, n) is the lowest common multiple of m and n.
It is easy for you, isn’t it.
Please hurry up!
ZTY

What a good chance to be a hero. Lemon can not wait any longer. Please help Lemon get the answer as soon as possible.

输入:

First line contains an integer T(1 <= T <= 1000000) indicates the number of test case. Then T line follows, each line contains an integer n (1 <= n <= 2000000).

输出:

First line contains an integer T(1 <= T <= 1000000) indicates the number of test case. Then T line follows, each line contains an integer n (1 <= n <= 2000000).

样例输入:

1
1

样例输出:

0

#include <cstdio>
#include <cstdlib>
#include <cstring>
int front,rear;
int q[10005];
int main(){
	int cases,time,n,m,ok;
	scanf("%d",&cases);
	while(cases--){
		front = rear = 0;
		time = 0;
		scanf("%d%d",&n,&m);
		for(int i = 0; i < n; i++){
			scanf("%d",&q[i]);
			rear++;
		}
		while(front <= rear){
			int i;
			for(i = front; i < rear; i++)
				if(q[i] > q[front]){
					q[rear++] = q[front];
					break;
				}
			if(i >= rear){
				time += 1;
				if(front == m){
					printf("%d\n",time);
					break;
				}
				else
					front ++;
			}
			else if(front == m) 
				m = rear - 1;
			else 
				front++;
		}
	}
	return 0;
}

解题转自:http://blog.csdn.net/fengshiye948/article/details/8779135