Hangzhou Electric Oj brush title (2037)

This summer is not AC

Subject description:

"This summer without AC?"
"Yes." "What
are you doing?"
"Yeah watching the World Cup, stupid!"
"@ # $% ^ & *% ..."

Indeed, the World Cup to the fans of the festival also it came, estimated that many ACMer will put aside the computer, toward the television.
As fans, we want to see as much of the full game, of course, as a new era of good young people, you must also look at some other programs, such as news network (Never forget that concerned about national affairs), is 6 + 7, Super girls, and Wang Xiaoya of "happy dictionary" and so on, assuming you already know you like to watch the broadcast schedule of all TV programs, you'll arrange it? (Goal is to see as much of the full program)

Input

Input data comprising a plurality of test example, the first line of each test case only one integer n (n <= 100), represents the total number of programs you like, then n rows, each row comprising two data Ti_s, Ti_e (1 <= i <= n), represent the i-th program start and end times, in order to simplify the problem, each time with a positive integer. n = 0 represents the input end without processing.

Output

For each test case, the number of outputs can see the complete TV program, the output of each test case in a separate line.

Sample Input

12 
1 3 
3 4 
0 7 
3 8 
15 19 
15 20 
10 15 
8 18 
6 12 
5 10 
4 14 
2 9 
0

By the answer:

#include<stdio.h>                  //贪心算法 (也可以认为是动态规划) 
int main(){
    int n,i,j,count,temp;
    int ts[101],te[101];
    while(scanf("%d",&n)!=EOF){
    	if(n==0)break;            //n=0表示输入结束,不做处理。
    	for(i=0;i<n;i++){
    		scanf("%d",&ts[i]);
    		scanf("%d",&te[i]);
		}
		for(i=0;i<n;i++){         //将时间从小到大排序 
			for(j=i;j<n-1;j++){
				if(te[i]>te[j+1]){
					temp=ts[i];ts[i]=ts[j+1];ts[j+1]=temp;
				    temp=te[i];te[i]=te[j+1];te[j+1]=temp;
				}
			}
		}
        count=1;                  //依次添加数据到集合 
        int k=te[0];
        for(i=1;i<n;i++){         
        	if(k<=ts[i]){
        		k=te[i];
        		count++;
			}
		}
		printf("%d\n",count);
    }
    return 0;
}

 

发布了55 篇原创文章 · 获赞 0 · 访问量 1002

Guess you like

Origin blog.csdn.net/ZhangShaoYan111/article/details/104202436