24h購物| | PChome| 登入
2009-01-24 22:04:04| 人氣825| 回應2 | 上一篇 | 下一篇

2008 NPSC B. 外星人的訊息

推薦 0 收藏 0 轉貼0 訂閱站台

以下的知識,都是經過無數的更改,我的嘔心瀝血。

/************************************************************/

  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. #include<string.h>   
  4. main()   
  5. {   
  6.  int a,b,c,n,n1,longtemp=0;   
  7.  char ans[100],temp[4],temp1[100],temp2[100],ADD[4]="ADD",ADH[4]="ADH",ERA[4]="ERA",SUB[4]="SUB",SPA[4]="SPA",END[4]="END";   
  8.  while(gets(temp2))   
  9.   {   
  10.   longtemp=strlen(temp2);   
  11.   for(a=0;a<strlen(temp2);a++)   
  12.    {   
  13.     ans[a]=temp2[a];   
  14.    }   
  15.    while(scanf("%s",temp)==1&&strcmp(temp,END)!=0)   
  16.     {   
  17.      if(strcmp(temp,ADD)==0)/*代表在訊息後連接(字串A)*/  
  18.       {   
  19.        gets(temp1);   
  20.        for(b=1,a=longtemp;b<strlen(temp1);b++,a++)/*為什麼要由1開始貼呢?*/  
  21.         {   
  22.         ans[a]=temp1[b];   
  23.         }   
  24.        longtemp=longtemp+strlen(temp1)-1;   
  25.        for(a=0;a<longtemp;a++)   
  26.         printf("%c",ans[a]);   
  27.       }   
  28.      if(strcmp(temp,ADH)==0)/*代表在訊息前連接(字串B)*/  
  29.       {   
  30.        gets(temp1);   
  31.        for(a=longtemp-1,b=strlen(temp1)-1;a>=0;a--)/*把之前的往後儲存*/  
  32.         {   
  33.          ans[a+b]=ans[a];   
  34.         }   
  35.        for(a=1,b=0;a<strlen(temp1);a++,b++)/*將現在的儲存*/  
  36.         {   
  37.          ans[b]=temp1[a];   
  38.         }    
  39.         longtemp=longtemp+strlen(temp1)-1;   
  40.         for(a=0;a<longtemp;a++)   
  41.          printf("%c",ans[a]);   
  42.       }    
  43.      if(strcmp(temp,ERA)==0)/*代表刪去訊息從第(數字1)個字到第(數字2)個字,刪去後字串往前合併*/  
  44.       {   
  45.        scanf("%d %d",&n,&n1);   
  46.        for(a=n-1,b=n1;b<=longtemp;a++,b++)   
  47.         {   
  48.         ans[a]=ans[b];   
  49.         }   
  50.        longtemp=longtemp+(n-n1-1);   
  51.        for(a=0;a<longtemp;a++)    
  52.         printf("%c",ans[a]);   
  53.       }   
  54.      if(strcmp(temp,SUB)==0)/*代表擷取訊息從第(數字3)個字到第(數字4)個字,只保留擷取後的訊息*/  
  55.       {   
  56.        scanf("%d %d",&n,&n1);   
  57.        for(a=n-1,b=0;a<=n1;a++,b++)   
  58.         ans[b]=ans[a];   
  59.         longtemp=n1-n+1;   
  60.        for(a=0;a<longtemp;a++)   
  61.         printf("%c",ans[a]);   
  62.       }   
  63.       if(strcmp(temp,SPA)==0)/*代表在第(數字5)個字的地方插入一個底線 _,訊息順往後推一個字*/  
  64.       {   
  65.        scanf("%d",&n);   
  66.        for(a=longtemp-1;a>=n-1;a--)   
  67.         {   
  68.         ans[a+1]=ans[a];   
  69.         }   
  70.        ans[n-1]='_';   
  71.        longtemp++;   
  72.        for(a=0;a<longtemp;a++)   
  73.         printf("%c",ans[a]);    
  74.       }     
  75.      printf("\n");   
  76.     }   
  77.     break;   
  78.   }   
  79.  return 0;   

台長: 來源不明
人氣(825) | 回應(2)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: NPSC |
此分類下一篇:2007 NPSC F. 鬧鐘
此分類上一篇:2008 NPSC F. 優惠方案II

您好
不好意思..我的程式不知道為何跑測資時,前半段都可以,但是後半段都會出錯...有點奇怪@@可以幫我看一下嗎...
2010-02-25 01:16:51
您好
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char message[1000001];
char temp[1000001];

int main(void){
char what[10];
char cha;
int x,y;
freopen("pb.in","r",stdin);
freopen("pb.out","w",stdout);

gets(message);

while(1){
scanf("%s ",what);
if(strcmp(what,"END")==0) break;
else if(strcmp(what,"ADD")==0){
scanf("%s",temp);
for(int i=strlen(message),j=0;j<strlen(temp);i++,j++)
message[i]=temp[j];
}
else if(strcmp(what,"ADH")==0){
scanf(" ");
gets(temp);
for(int i=strlen(temp),j=0;j<strlen(message);i++,j++)
temp[i]=message[j];
strcpy(message,temp);
}
else if(strcmp(what,"ERA")==0){
scanf("%d %d",&x,&y);
for(int i=0,j=0;i<x-1;i++,j++)
temp[i]=message[j];
for(int i=x-1,j=y;j<strlen(message);i++,j++)
temp[i]=message[j];
strcpy(message,temp);
}
else if(strcmp(what,"SUB")==0){
scanf("%d %d",&x,&y);
for(int i=0,j=x-1;j<y;i++,j++)
temp[i]=message[j];
int k=strlen(temp);
for(int i=y-x+1;i<k;i++)
temp[i]='\0';
strcpy(message,temp);
}
else if(strcmp(what,"SPA")==0){
scanf("%d",&x);
for(int i=0;i<x-1;i++)
temp[i]=message[i];
for(int i=x-1;i<strlen(message);i++)
temp[i+1]=message[i];
temp[x-1]='_';
for(int i=strlen(message)+1;i<strlen(temp);i++)
temp[i]='\0';
strcpy(message,temp);
}
memset(temp,'\0',sizeof(temp));
puts(message);
}
return 0;
}
2010-02-25 01:17:14
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文