【乱搞】【AOJ-191】删除数字

Description
对给定的N位高精度正整数,去掉其中的k个数字后,使剩下的数字构成的整数最大。
Input
输入第1行为一个整数L
后面L行的每一行包括一个长度为N的高精度正整数和需要去掉的数的个数k。(1 <= N <= 1000 , 1 <= k < N)
Output
输出每一行表示每个高精度正整数去掉相应的k个数字后构成的新的最大正整数。
Sample Input
2
12345 1
54321 2

 
Sample Output
2345
543
 
思路:用字符串储存数字,按顺序查找,若后一个元素比当前元素大,删除当前元素,若找不到此类元素,把最后一个元素删除
参考代码:(我是用链表做的,第一次用链表做题,删除数据方便)
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
  
typedef struct Node{ 
    char data; 
    Node *next; 
}Node; 
  
Node *creat()//构建链表 
{ 
    Node *p; 
    p=new Node; 
    p->next=NULL; 
    return p; 
} 
  
int del(Node *head)//删除数字 
{ 
    Node *p=head; 
    Node *s; 
    s=p; 
    p=p->next; 
    while(p->next!=NULL) 
    { 
        if((p->data)<(p->next->data)) 
        { 
            s->next=p->next; 
        } 
        p=p->next; 
        s=s->next; 
    } 
    s->next=NULL; 
    return 0; 
} 
  
int display(Node *head)//打印链表 
{ 
    Node *p=head; 
    while(p->next!=NULL) 
    { 
        p=p->next; 
        printf("%c",p->data); 
    } 
    puts(""); 
    return 0; 
} 
  
int del_p(Node *head)//释放内存 
{ 
    Node *p=head; 
    Node *s; 
    p=p->next; 
    while(p->next!=NULL) 
    { 
        s=p->next; 
        delete p; 
        p=s; 
    } 
    return 0; 
} 
  
char c[1111]; 
  
  
int main() 
{ 
    int l,k; 
    int i=0; 
    Node *p; 
    Node *head=new Node; 
    p=head; 
    scanf("%d",&l); 
    while(l--) 
    { 
        p=head; 
        i=0; 
        scanf("%s%d",c,&k); 
        if(strlen(c)<=k) 
        { 
            printf("
"); 
            continue; 
        } 
  
        while(c[i]!='') 
        { 
            p->next=creat(); 
            p=p->next; 
            p->data=c[i]; 
            i++; 
        } 
        while(k--) 
        { 
            del(head); 
        } 
        display(head); 
        del_p(head); 
    } 
    return 0; 
}
 
原文地址:https://www.cnblogs.com/ahu-shu/p/3478620.html