1 条题解

  • 0
    @ 2024-12-24 10:06:06

    C :

    #include <stdio.h>
    #include <string.h>
    
    struct Poj
    {
        int num;
        char name[20];
    } poj[100000];
    
    int main()
    {
        int l, i, t, n, j;
        char s[10], str[20];
        int flag;
    
        scanf("%d", &t);
    
        while (t--)
        {
            flag = l = 0; n = 1000;
            while(scanf("%s", s)&&strcmp("End", s)!=0)
            {
                scanf("%s", str);
                if(strcmp("Delete", s)==0)
                {
                    for(i=0; i<l; i++)
                    {
                        if(!strcmp(str, poj[i].name))
                        {
                            for(j=i; j<l-1; j++)
                            {
                                poj[j] = poj[j+1];
                            }
                            l--;
                            break;
                        }
                    }
                }
                else
                {
                    strcpy(poj[l].name, str);
                    poj[l].num = n++;
                    l++;
                }
            }
            for(i=0; i<l; i++)
            {
               flag = 1;
               if(i<l-1)
               {
                    printf("%d ", poj[i].num);
                }
                else
                    printf("%d\n", poj[i].num);
            }
            for(i=0; i<l; i++)
            {
               if(i<l-1)
               {
                    printf("%s ", poj[i].name);
                }
                else
                    printf("%s\n", poj[i].name);
            }
    
            if(!flag)
                printf("\n\n");
        }
    
        return 0;
    }
    
    

    C++ :

    #include <set>
    #include <map>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <vector>
    #include <cstdio>
    #include <bitset>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define REPI(i, s, e)   for(int i = (s); i <= (e); i ++)
    #define REPD(i, e, s)   for(int i = (e); i >= (s); i --)
    
    const int DELTA = 1000;
    const int MAXN  = 30 + 10;
    const int MAXM  = 100000 + 10;
    
    int idx;
    
    char cmd[MAXN];
    char name[MAXM][MAXN];
    
    
    void Input(void) {
            idx = 0;
            while( scanf("%s", cmd) ) {
                    if( !strcmp(cmd, "End") ) {
                            break;
                    }
                    if( !strcmp(cmd, "Add") ) {
                            scanf("%s", name[idx ++]);
                    }
                    else if( !strcmp(cmd, "Delete") ) {
                            scanf("%s", cmd);
                            REPI(i, 0, idx-1) {
                                    if( !strcmp(cmd, name[i]) ) {
                                            strcpy(name[i], "NULL");
                                            break;
                                    }
                            }
                    }
            }
    }
    
    void Process(void) {
            int flag;
    
            flag = 0;
            REPI(i, 0, idx-1) {
                    if( !strcmp(name[i], "NULL") ) {
                            continue;
                    }
                    if( flag ++ ) {
                            printf(" ");
                    }
                    printf("%d", i+DELTA);
            }
            printf("\n");
    
            flag = 0;
            REPI(i, 0, idx-1) {
                    if( !strcmp(name[i], "NULL") ) {
                            continue;
                    }
                    if( flag ++ ) {
                            printf(" ");
                    }
                    printf("%s", name[i]);
            }
            printf("\n");
    }
    
    int main(int argc, char const *argv[])
    {
    #ifndef ONLINE_JUDGE
            freopen("test.in", "r", stdin);
    #endif
            int cas;
            scanf("%d", &cas);
            REPI(k, 1, cas) {
                    Input();
                    Process();
            }
            return 0;
    }
    

    Java :

    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		int n = scanner.nextInt();
    		while (n-- > 0) {
    			String titles = "";
    			String title = "";
    			while (true) {
    				String inputLine = scanner.nextLine();
    				if (inputLine.startsWith("End")) {
    					break;
    				} else {
    					if (inputLine.startsWith("Add")) {
    						title = inputLine.replaceFirst("Add", "");
    						title = title.trim();
    						titles += "&&&" + title;
    					} else if (inputLine.startsWith("Delete")) {
    						title = inputLine.replaceFirst("Delete", "");
    						title = title.trim();
    						titles = titles.replaceFirst(title, "#DELETED#");
    					}
    				}
    			}
    			
    			titles = titles.replaceFirst("&&&", "");
    
    			boolean is1st = true;
    			boolean isEmpty = true;
    			
    			int startNumber = 1000;
    			String[] titleList = titles.split("&&&");
    			int length = titleList.length;
    			for (int i = 0; i < length; i++, startNumber++) {
    				title = titleList[i].trim();
    
    				if (title.contains("#DELETED#")) {
    					continue;
    				} else {
    					isEmpty = false;
    					if (is1st) System.out.print(startNumber);
    					else System.out.print(" " + startNumber);
    					is1st = false;
    				}
    			}
    			System.out.println();
    			
    			is1st = true;
    			isEmpty = true;
    			for (int i = 0; i < length; i++) {
    				title = titleList[i].trim();
    				if (title.contains("#DELETED#")) {
    					continue;
    				} else {
    					isEmpty = false;
    					if (is1st) System.out.print(title);
    					else System.out.print(" " + title);
    					is1st = false;
    				}
    			}
    			System.out.println();
    		}
    	}
    	
    }
    
    
    • 1

    信息

    ID
    1940
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者