记录编号 530399 评测结果 WWWWWWWWWW
题目名称 [HEOI 2015]定价 最终得分 0
用户昵称 GravatarTabing010102 是否通过 未通过
代码语言 C++ 运行时间 0.004 s
提交时间 2019-04-20 18:17:32 内存使用 1.96 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>

FILE *fin, *fout;

int T;

int convert(int n, int *s, int len=-1) {
  int cnt=0, t=n;
  while(t) { cnt++; t/=10; }
  int retr = cnt;
  if(len != -1) cnt = len;
  while(n) {
    s[--cnt] = n%10;
    n /= 10;
  }
  return retr;
}

void work() {
  int L, R;
  fscanf(fin, "%d%d", &L, &R);
  if(L == R) { printf("%d\n", L); return; }
  int sR[15], sL[15];
  memset(sR, 0, sizeof(sR));
  memset(sL, 0, sizeof(sL));
  int len = convert(R, sR);
  int len2 = convert(L, sL, len);
  int cnt = len;
  while(!(sL[cnt-1]==0&&sR[cnt-1]==0) && sR[cnt-1]<=sL[cnt-1]) cnt--;//末尾填充0
  int pR = cnt;
  cnt = -1;
  while(sL[cnt+1] == 0) cnt++;
  int pL = cnt;
  /*printf("pL=%d, pR=%d\n", pL, pR);
  printf("sL= ");
  for(int i = 0; i < len; i++) printf("%d", sL[i]);
  printf("\n");
  printf("sR= ");
  for(int i = 0; i < len; i++) printf("%d", sR[i]);
  printf("\n");*/
  
  
  if(len == len2) {
    int cnt = len;
    while(sR[cnt-1]<=sL[cnt-1]) cnt--;//末尾填充0
    int pR = cnt;
    cnt = pL;
    while(sL[cnt+1] == sR[cnt+1]) cnt++;
    for(int i = pL+1; i <= cnt; i++) fprintf(fout, "%d", sL[i]);
    if(sL[cnt+1]<5 && sR[cnt+1]>=5) fprintf(fout, "5");
    else {
      //printf("cnt=%d, pR=%d\n", cnt, pR);
      if(cnt+1 == len-1) fprintf(fout, "%d", sL[cnt+1]);
      else if(cnt+1 == pR-1) fprintf(fout, "%d", sL[cnt+1]);
      else fprintf(fout, "%d", sL[cnt+1]+1);
    }
    for(int i = cnt+2; i < len; i++) fprintf(fout, "0");
    fprintf(fout, "\n");
  } else {
    /*if(pL > -1) if(sL[pL+1] >= sR[pL+1])
    if(pL+1 == pR) {
      if(sR[pL] >= 5) fprintf(fout, "5");
      else fprintf(fout, "%d", sL[pL]+1);
      for(int i = 0; i < len-pR; i++) fprintf(fout, "0");
      fprintf(fout, "\n");
      return;
    } else fprintf(fout, "%d", sL[pL]+1);
    for(int i = pL+1; i <= pR-2; i++) fprintf(fout, "%d", sL[i]);
    if(sL[pR-1] == 0) fprintf(fout, "0");
    else if(sL[pR-1]<=5 && sR[pR-1]>=5) fprintf(fout, "5");
    else fprintf(fout, "%d", sL[pR-1]);
    for(int i = 0; i < len-pR; i++) fprintf(fout, "0");
    fprintf(fout, "\n");*/
    int p0L = len;
    while(sL[p0L-1] == 0) p0L--;
    if(len-len2 >= 2) {
      if(sL[pL+1]<5 || (sL[pL+1]==5&&pL+1==p0L-1)) {
        fprintf(fout, "5");
        for(int i = pL+2; i < len; i++) fprintf(fout, "0");
        fprintf(fout, "\n");
      } else {
        fprintf(fout, "5");
        for(int i = pL+1; i < len; i++) fprintf(fout, "0");
        fprintf(fout, "\n");
      }
    } else {
      if(sL[pL+1]<5 || (sL[pL+1]==5&&pL+1==p0L-1)) {
        fprintf(fout, "5");
        for(int i = pL+2; i < len; i++) fprintf(fout, "0");
        fprintf(fout, "\n");
      } else if(sR[0] >= 5) {
        fprintf(fout, "5");
        for(int i = 1; i < len; i++) fprintf(fout, "0");
        fprintf(fout, "\n");
      } else {
        fprintf(fout, "%d", sL[pL+1]+1);
        for(int i = pL+2; i < len; i++) fprintf(fout, "0");
        fprintf(fout, "\n");
      }
    }
  }
}

int main() {
  fin = fopen("absurd.in", "r");
  fout = fopen("absurd.out", "w");
  //fin = stdin; fout = stdout;
  fscanf(fin, "%d", &T);
  while(T--) work();
  return 0;
}