好吧,不得不承认自己还是不够好,因为一个特例我竟然找了4个小时的debug,唉,果然陷进去了。
如题:
Crawling in process...Crawling failedTime Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu
Description
One Day, KIDx developed a beautiful pagination for ACdream. Now, KIDx wants you to make another one.
The are n pages in total.
The current page is cur.
The max distance to current page you can display is d.
Here are some rules:
- The cur page button is disabled.
- If cur page is the first page, the button "<<" should be disabled.
- If cur page is the last page, the button ">>" should be disabled.
- If the button "x" is disabled, print "[x]"; else print "(x)".
- You should not display the "..." button when there is no hidden page.
You can assume that the button "..." is always disabled.
Input
Ease case contains three integers n, cur, d.
1 ≤ n ≤ 100.
1 ≤ cur ≤ n.
0 ≤ d ≤ n.
Output
Sample Input
10 5 2 10 1 2
Sample Output
Case #1: (<<)[...](3)(4)[5](6)(7)[...](>>) Case #2: [<<][1](2)(3)[...](>>)
Hint
Case 1:
Case 2:
就是类似于格式转换的东西,我的代码写的比较水,就是一种种分情况去写。
在这里,我只想说,不要忘了 1 1 1 的那组特例,那是输出 [<<][1][>>]的,我变成了[<<][1](>>) ; 所以导致一直WA,我还一直在检查下面的,还以为下面的代码错误。
#include<stdio.h>
int main(){
int n,cur,d,i,j,k,l,m=1;
while(scanf("%d%d%d",&n,&cur,&d)!=EOF){
printf("Case #%d: ",m++);
//太可恶了,竟然少判断了一组样例;
if(cur == 1 && cur == n) {
printf("[<<][1][>>]\n");
continue;
}
if(cur==1){
printf("[<<][1]");
if(1+d<n){
for(i=1;i<=d;i++)
printf("(%d)",1+i);
printf("[...](>>)");
}
else {
for(i=cur+1;i<=n;i++)
printf("(%d)",i);
printf("(>>)");
}
}
else if(cur!=1 && cur!=n){
if(cur-1-d>0){
printf("(<<)[...]");
for(i=1;i<=d;i++)
printf("(%d)",cur-1-d+i);
}
else{
printf("(<<)");
for(i=1;i<cur;i++)
printf("(%d)",i);
}
printf("[%d]",cur);
if(n-cur<=d){
for(i=cur+1;i<=n;i++)
printf("(%d)",i);
printf("(>>)");
}
else {
for(i=1;i<=d;i++)
printf("(%d)",cur+i);
printf("[...](>>)");
}
}
else if(cur==n){
if(cur-1-d>0){
printf("(<<)[...]");
for(i=0;i<d;i++)
printf("(%d)",cur-d+i);
}
else{
printf("(<<)");
for(i=1;i<cur;i++)
printf("(%d)",i);
}
printf("[%d]",cur);
printf("[>>]");
}
printf("\n");
}
}
从网上学习的大神的代码(思路是差不多的,但是比我要简洁):
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n,cur,d,tp,cnt = 0;
//freopen("in.txt","r",stdin);
while(~scanf("%d %d %d",&n,&cur,&d))
{
printf("Case #%d: ",++cnt);
if(cur != 1) printf("(<<)");
else printf("[<<]");
if(cur - d > 1) printf("[...]");
if(cur - d < 1) tp = 1;
else tp = cur - d;
for(int i = tp; i < cur && i >= 1; i++)
printf("(%d)",i);
printf("[%d]",cur);
for(int i = cur + 1; i <= cur + d && i <= n; i++)
printf("(%d)",i);
if(cur + d < n) printf("[...]");
if(cur != n) printf("(>>)");
else printf("[>>]");
puts("");
}
return 0;
}