本文主要是介绍codeforces 399A. Pages(模拟题,细心点就能AC),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、http://codeforces.com/problemset/problem/399/A
2、题目
User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are n pages numbered by integers from 1 to n. Assume that somebody is on the p-th page now. The navigation will look like this:
When someone clicks the button "<<" he is redirected to page 1, and when someone clicks the button ">>" he is redirected to page n. Of course if someone clicks on a number, he is redirected to the corresponding page.
There are some conditions in the navigation:
- If page 1 is in the navigation, the button "<<" must not be printed.
- If page n is in the navigation, the button ">>" must not be printed.
- If the page number is smaller than 1 or greater than n, it must not be printed.
You can see some examples of the navigations. Make a program that prints the navigation.
The first and the only line contains three integers n, p, k (3 ≤ n ≤ 100; 1 ≤ p ≤ n; 1 ≤ k ≤ n)
Print the proper navigation. Follow the format of the output from the test samples.
17 5 2
<< 3 4 (5) 6 7 >>
6 5 2
<< 3 4 (5) 6
6 1 2
(1) 2 3 >>
6 2 2
1 (2) 3 4 >>
9 6 3
<< 3 4 5 (6) 7 8 9
10 6 3
<< 3 4 5 (6) 7 8 9 >>
8 5 4
1 2 3 4 (5) 6 7 8
3、AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{int n,p,k;while(scanf("%d%d%d",&n,&p,&k)!=EOF){int s=p-k;int e=p+k;if(p==1){printf("(1) ");int flag=0;for(int i=2;i<=e;i++){if(i<n)printf("%d ",i);else if(i==n){printf("%d ",i);flag=1;break;}}if(flag==0){printf(">> \n");}elseprintf("\n");}else{if(s<=1){int flag=0;for(int i=1;i<=e;i++){if(i==p){printf("(%d) ",i);if(i==n)flag=1;}else if(i<n){printf("%d ",i);}else if(i==n){printf("%d ",i);flag=1;break;}}if(flag==0)printf(">> \n");elseprintf("\n");}else{int flag=0;printf("<< ");for(int i=s;i<=e;i++){if(i==p){printf("(%d) ",i);if(i==n)flag=1;}else if(i<n)printf("%d ",i);else if(i==n){printf("%d ",n);flag=1;//printf("*%d\n",flag);break;}}if(flag==0)printf(">> \n");elseprintf("\n");}}}return 0;
}
/*
100 100 17
*/
这篇关于codeforces 399A. Pages(模拟题,细心点就能AC)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!