2014年9月27日星期六

[UVa] 10106 - Product

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <iostream>

#define maxn 1000
using namespace std;

void print(int a[maxn]){
  int i=maxn-1;       // 要印的數字位置
  while(i>=0 && a[i]==0) i--; // 數字開頭的零都不印
  if(i<0)cout << '0';
  else while(i>=0) cout << a[i--]; 
}

// c = a + b;
void add(int a[maxn], int b[maxn], int c[maxn])
{
  for (int i=0; i<maxn; i++)   // 對應的位數相加
    c[i] = a[i] + b[i];
            
  for (int i=0; i<maxn-1; i++) // 一口氣進位
  {
    c[i+1] += c[i] / 10;    // 進位
    c[i] %= 10;             // 進位後餘下的數
  }
}

// c = a * b;
void mul(int a[maxn], int b[maxn], int c[maxn])
{
  for (int i=0; i<maxn; i++)
    c[i] = 0;
     
    for (int i=0; i<maxn; i++)
      for (int j=0; j<maxn; j++)
        if (i+j < maxn)
          c[i+j] += a[i] * b[j];
                    
    for (int i=0; i<maxn-1; i++) // 一口氣進位
    {
      c[i+1] += c[i] / 10;
      c[i] %= 10;
    }
}

char input[maxn]={'\0'};
int a[maxn],b[maxn],c[maxn];

int main(){
  freopen("input.txt","r",stdin);
  while(scanf("%s",input)!=EOF){
 int len = strlen(input);
    for(int i=0;i<len;i++)
   a[i]=input[len-i-1]-48;

 for(int i=0;i<maxn;i++)input[i]='\0';
 
 scanf("%s",input);
 len = strlen(input);
 for(int i=0;i<len;i++)
   b[i]=input[len-i-1]-48;
 
 mul(a,b,c);
 print(c);
    printf("\n"); 
 
 for(int i=0;i<maxn;i++)a[i]=0;
 for(int i=0;i<maxn;i++)b[i]=0;
  }
  return 0;
} 

2014年9月26日星期五

[UVa] 424 - Integer Inquiry

本題參考了演算法筆記的大數運算

簡單解決
 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void print(int a[200]){
  int i=200-1;       // 要印的數字位置
  while(i>=0 && a[i]==0) i--; // 數字開頭的零都不印
  if(i<0)cout << '0';
  else while(i>=0) cout << a[i--]; 
}
// c = a + b;
void add(int a[200], int b[200], int c[200])
{
  for (int i=0; i<200; i++)   // 對應的位數相加
    c[i] = a[i] + b[i];
            
  for (int i=0; i<200-1; i++) // 一口氣進位
  {
    c[i+1] += c[i] / 10;    // 進位
    c[i] %= 10;             // 進位後餘下的數
  }
}
char input[200]={'\0'};
int a[200],c[200];

int main(){
  
  while(1){
    gets(input);
 if(input[0]=='0'){
   print(c);
   printf("\n"); //本題需要留一個換行
   break;
 }
 int len = strlen(input);
    for(int i=0;i<len;i++){
   a[i]=input[len-i-1]-48;
 }
 add(a,c,c);
 for(int i=0;i<200;i++)a[i]=0;
  }
  return 0;
}
 

2014年9月21日星期日

[UVa] 10115 - Automatic Editing

 

http://www.cplusplus.com/reference/cstring/strstr/?kw=strstr



有兩個關於字串操作的函數相當有用,一個是strstr,比較str2是否在str1中,有的話傳回在str1的指標位址,如下: 



strstr

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );
Locate substring
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

The matching process does not include the terminating null-characters, but it stops there.

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}


This example searches for the "simple" substring in str and replaces that word for "sample".

Output:

This is a sample string



另外就是常用的strcpy,若source指定的位置在字串中間時,會從指定的位置開始拷貝到字串的結尾(\0)處。

strcpy

char * strcpy ( char * destination, const char * source );
Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.




#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> 
char str[300]={'\0'};
char find[10][100]={'\0'};
char replace[10][100]={'\0'};
char str1[300]={'\0'};


char str2[300]={'\0'};
char str3[300]={'\0'};

int main(){
  freopen("input.txt", "r", stdin);

  int n;
  while(1){
    scanf("%d",&n);
 getchar();
 //printf("n=%d\n",n);
    if(n==0)break;
 
 int tmp=n;
    while(n--){ //輸入所有find和replace-by
   //printf("%d",n);
   gets(find[tmp-n-1]);
   gets(replace[tmp-n-1]);
 }
 /*
 for(int i=0;i<tmp;i++){
   puts(find[i]);
   puts(replace[i]);
 }
 */
 gets(str); //輸入要檢驗的字串
 //puts(str);
 //getchar();
 //printf("test");
 for(int i=0;i<tmp;i++){
   if(strstr(str,find[i])!=NULL){
     char *pch = strstr(str,find[i]);
  int len = strlen(find[i]);
  strcpy(str1,pch+len);
  *pch = '\0';
  strcat(str,replace[i]);
  strcat(str,str1);
  i--;
  //puts(str);
   }
 
 }
 puts(str);
 
  }

  return 0;
}

2014年9月19日星期五

[Uva] 644 - Immediate Decodability


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> 
char codes[12][12]={'\0'};
int main(){
  
  freopen("input.txt", "r", stdin);


  int a=0;
  int set=1;
  int decodable[12]={0}; //儲存每一record和前面所有record比較,找不到字首相同,則為可編碼
  while(scanf("%s",codes[a])!=EOF){
    
 decodable[0]=1; //第一個record因為只能和自己比,故先設為1
 if(codes[a][0]!='9'){
  for(int i=0;i<a;i++){ 
    if(a==0){  //若為第一個狀況,則先跳出
     break;
    }
    int flag=0;
    while(1){
     //printf("%c %c\n",codes[i][flag],codes[a][flag]);
     if(codes[a][flag]==codes[i][flag]){  //若檢查為相同的字
    flag++; //旗標往下一位元跳
    decodable[a]=0; //暫定此record的可編碼為0 
     }
     //若其中一項record的下一字元為'\0'且可編碼依然為0,則跳出
     if((codes[a][flag]=='\0'||codes[i][flag]=='\0') && decodable[a]==0)
    break;
     //若在兩項record都還沒到'\0',且發現兩位元不同,則為可編碼
     if(codes[a][flag]!=codes[i][flag]&&codes[i][flag]!='\0'&&codes[a][flag]!='\0'){
    decodable[a]=1;
    break;
     }
    }
    if(decodable[a]==0)break;
  }
 }
 
 
 
 if(codes[a][0]=='9'){
   for(int i=0;i<a;i++){
     if(decodable[i]==0){ //若發現其中一項為0,則為不可編碼
    printf("Set %d is not immediately decodable\n",set);
    a=-1;
    break;
  }
  if(i==(a-1) && decodable[i]==1){ //若i已經到了第a-1項且依然為1
    a=-1;
    printf("Set %d is immediately decodable\n",set);
  }
   }
   set++;
   memset(decodable,0,sizeof(int)); //初始化
    }
 a++;
  }   

  
  return 0;
} 

2014年9月16日星期二

[UVa] 10815 - Andy's First Dictionary

這題我採用 Trienode的資料結構,直接做字典順序輸出
詳細請參閱 http://en.wikipedia.org/wiki/Trie 
 http://openhome.cc/Gossip/CGossip/
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> 

char input[300]={'\0'};
char words[6000][300]={'\0'};

//建構Tire字典樹
struct TrieNode{
  TrieNode* l[128]; //128種字元,皆為Trienode型別,以l陣列儲存
  int n; //計算字串出現次數,0則代表沒有這個字
  TrieNode() //Trienode函數
  {
    memset(l, 0, sizeof(TrieNode*) *128); //把l陣列裡的指標都設為0
    n = 0;
  }
}*root = new TrieNode(); //root為第一個TrieNode

//插入字串
void add(char* s)
{
 TrieNode* p = root; //p為指向root節點的指標,
 for (; *s; ++s) // *s= s指向的變數,若尚有字元,則++s
 {   //若l陣列中第s欄位的指標不存在,則新增一節點
  if ( !(p->l[*s]) ) p->l[*s] = new TrieNode();
  p = p->l[*s];
 }
 //p->n++;
 p->n=1; //1代表有此單次,因只要輸出一次,所以p->n設為1即可
}

char w[300+1];   // 300個字的字串
void traversal(TrieNode* p, char* s) //s指標指向w
{
 *s = '\0';  
 for (int i=0; i<(p->n); ++i)
  printf("%s\n",w);

 for (int i=0; i<128; ++i)
  if (p->l[i]) //若l[i]存在有指標
  {
   *s = i; //s指向的變數設為i
   traversal(p->l[i], s+1); //往下一個Trienode節點探詢,s指標往w的下一位前進
  }
}

//釋放記憶體空間
void release(TrieNode* p = root)
{
 for (int i=0; i<26; ++i)
  if (p->l[i])
   release(p->l[i]);
 delete p;
}



int main(){
  freopen("input.txt", "r", stdin);
  //printf("%d\n", sizeof(TrieNode*));
  while(fgets(input, sizeof(input), stdin) ){  /*讀到EOF會自動跳出*/
    //gets(input);
 
 int len=strlen(input); // len = input長度
 for(int i=0;i<len;i++){
   if(!isalpha(input[i])){
     input[i]=' ';   //去除非字母字元
      }
      //大寫換小寫
   if(input[i]>='A'&&input[i]<='Z')input[i]+=32;
    }
 
 char word[300]={'\0'};
 int start_read=0, flag=0;
 for(int i=0;i<len;i++){
   ////該字元為字母且前一個不是字元,或該字元為列首時,start_read=1
   if(isalpha(input[i]) && (!isalpha(input[i-1]) ||i==0) ){ 
     start_read=1;
  flag=0;
   }
   if(start_read==1){ //start_read維持在1時,字元存入word陣列
     word[flag]=input[i];
  flag++;
   }
   if(isalpha(input[i])&&!isalpha(input[i+1])){
     start_read=0;
  flag=0;
  //puts(word);
  add(word); //插入到Trie
  
  //清空陣列
  memset(word,'\0',sizeof(word));
  //for(int j=0;j<300;j++)word[j]='\0';
   }
   
 }
 //puts(input);
 //system("PAUSE");
  }
  
  traversal(root,w);
  
  release(root);
  return 0;
} 

2014年9月13日星期六

[UVa] 10878 - Decode the tape


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> 
int code[11]={64,32,16,8,4,2,1};
char input[20]={'\0'};
int main(){
  freopen("input.txt", "r", stdin);
  int end_count=0;
  while(1){
    int output=0;
    gets(input);
 //puts(input);
 if(input[0]=='_')end_count++;
 if(end_count==2)break;
    
 if(input[0]!='_'){
   if(input[2]=='o')output+=64;
   if(input[3]=='o')output+=32;
   if(input[4]=='o')output+=16;
   if(input[5]=='o')output+= 8;
   if(input[7]=='o')output+= 4;
   if(input[8]=='o')output+= 2;
   if(input[9]=='o')output+= 1;
   
   printf("%c",output);
 }
 
 
 //system("PAUSE");
  }
  return 0;

}

[UVa] 409 - Excuses, Excuses!


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> //isalpha
char keywords[30][100]={'\0'};
char line[30][100]={'\0'};
char new_line[30][100]={'\0'};
int count[30]={0};
int main(){
  freopen("input.txt", "r", stdin);

  int K,E;
  int max=0;
  int excuse=1;
  while(scanf("%d %d",&K,&E)!=EOF){
    
 getchar();
 
 //輸入關鍵字詞
    for(int i=0;i<K;i++){
   gets(keywords[i]);
 }
 //getchar();
 
 //輸入辯詞
 for(int i=0;i<E;i++){
   gets(line[i]);
   
   int len=strlen(line[i]); //獲得line長度
   for(int j=0;j<len;j++)
     new_line[i][j]=line[i][j]; //Copy一份至new_line
   for(int j=0;j<len;j++){ //消除new_line裡所有非字母符號
     if(!isalpha(new_line[i][j])){
     new_line[i][j]=' ';  
  }
  if(new_line[i][j]>='A'&&new_line[i][j]<='Z')new_line[i][j]+=32; //所有大寫換小寫
   }
   //puts(line[i]);
   //puts(new_line[i]);
 }
 
 for(int w=0;w<K;w++) //每個keyword都跑一次
  for(int i=0;i<E;i++){  //每個excuse都跑一次
   int match=0, start=0, flag=0,flag2=0;
   while(new_line[i][flag]!='\0'){
       //如果在excuse中檢查到和單字字首相同,且前一個字元不為字母時,start=1
       if(new_line[i][flag]==keywords[w][0]&&!isalpha(new_line[i][flag-1]))start=1;
    if(new_line[i][flag]==keywords[w][flag2]&&start==1){ //之後的檢查,在start=1的前提下進行
      match=1;  //依然和單字相同時,match維持為1
      flag++;
      flag2++;
    }else{
      start=0;  //一檢查到不是,則start=0、match=0,flag2回到單字起點
      match=0;
      flag++;
      flag2=0;
    }
    //如果檢查到單字字尾,且excuse中後一個字元不為字母,且match=1時,該excuse的單字計數+1
    if( (keywords[w][flag2]=='\0') && (!isalpha(new_line[i][flag])) && (match==1) ){
      count[i]++;
    }
   }
   
 }
 
 printf("Excuse Set #%d\n", excuse);
 excuse++;
 for(int i=0;i<E;i++){
   if(count[i]>=max)max=count[i];
 }
 for(int i=0;i<E;i++){
   if(count[i]==max)puts(line[i]);
 }
 
 //for(int i=0;i<E;i++)printf("%d ",count[i]);
 
 printf("\n");
 
 for(int i=0;i<30;i++)count[i]=0;
 max=0;
  }
  return 0;
}

2014年9月6日星期六

[UVa] 537 - Artificial Intelligence?


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char line[10000]={'\0'};

int main(){
  //freopen("input.txt", "r", stdin);

  int n;
  scanf("%d",&n);
  getchar();
  int cases=n;
  while(n--){
    printf("Problem #%d\n",cases-n); //輸出problem數
    gets(line);
 double P=0,U=0,I=0;
 int enableP=0, enableU=0, enableI=0; //宣告是否出現變數的檢查變數
    for(int i=0; i<strlen(line); i++){ 
   char buffer[1000];
   int j=0,flag;
   
   
   //搜尋到"P=___mW"
   for(int p=0;p<1000;p++)buffer[p]='\0';
   flag=0;
   if(line[i]=='P' && line[i+1]=='='){ 
     enableP=1;
     for(j=i+2; (line[j]<='9'&&line[j]>='0')||line[j]=='.' ; j++){
    buffer[flag]=line[j];
    flag++;
  }
  double tmp = atof(buffer); 
  if(line[j]=='m')tmp/=1000;
  if(line[j]=='k')tmp*=1000;
  if(line[j]=='M')tmp*=1000000;
  P = tmp;
   }
   
   //搜尋到"U=___kV"
   for(int p=0;p<1000;p++)buffer[p]='\0';
   flag=0;
   if(line[i]=='U' && line[i+1]=='='){ 
     enableU=1;
     for(j=i+2; (line[j]<='9'&&line[j]>='0')||line[j]=='.' ; j++){
    buffer[flag]=line[j];
    flag++;
  }
  double tmp = atof(buffer); 
  if(line[j]=='m')tmp/=1000;
  if(line[j]=='k')tmp*=1000;
  if(line[j]=='M')tmp*=1000000;
  U = tmp;
   }
   
   //搜尋到"I=___MA"
   for(int p=0;p<1000;p++)buffer[p]='\0';
   flag=0;
   if(line[i]=='I' && line[i+1]=='='){ 
     enableI=1;
     for(j=i+2; (line[j]<='9'&&line[j]>='0')||line[j]=='.' ; j++){
    buffer[flag]=line[j];
    flag++;
  }
  double tmp = atof(buffer);
  if(line[j]=='m')tmp/=1000;
  if(line[j]=='k')tmp*=1000;
  if(line[j]=='M')tmp*=1000000;
  I = tmp;
   }
   
 }
 //printf("P=%.2f  U=%.2f  I=%.2f \n", P, U, I);
 
 if(enableP==0)printf("P=%.2fW\n", U*I);
 if(enableU==0)printf("U=%.2fV\n", P/I);
 if(enableI==0)printf("I=%.2fA\n", P/U);
 //for(int i=0;line[i];i++)printf("%c",line[i]);
 
 printf("\n");
  
  }
  return 0;
} 

C++ 字串整數轉換

資料來源: Edison.X. Blog

需要先引入標頭檔
#include <stdlib.h>  or  #include <cstdlib>

1. atof:將字串轉為倍精度浮點數
double atof ( const char * str );
ex:
 char buffer[] = "2.675";
 double f = atof(buffer);

2. atoi:將字串轉為整數
 int atoi ( const char * str );
ex:
 char buffer[] = "23";
 int i = atoi(buffer);

3. atol:將字串轉為長整數
long int atol ( const char * str );
ex:
 char buffer[] = "23";
 long int i = atol(buffer);

4. strtod: 將字串轉為倍精度浮點數
double strtod ( const char * str, char ** endptr );
ex:
 char buffer[] = "1011.113";
 double a = strtod(buffer, NULL);

5. strtol:將字串視為任意進制,轉為長整數
long int strtol ( const char * str, char ** endptr, int base );
ex:
 char buffer[] = "1011";
 long a = strtol(buffer, NULL,2);
// 將 "1011" 視為2進制,轉完之後 a 即為 11

6. strtoul:將字串視為任意進制,轉為無號長整數
unsigned long int strtoul ( const char * str, char ** endptr, int base );
ex:
 char *buffer = "1011";
 unsigned long a = strtoul(buffer, NULL,2);
// 將 "1011" 視為2進制,轉完之後 a 即為 11

 7. itoa: 整數轉為任意進制字串 (非標準函式)
char* itoa(int value, char* string, int radix);
ex:
char buffer[200];
int value = 234;
itoa(value, buffer, 2); // 將 234 轉為存成2進制之字串

8. ltoa:長整數轉為任意進制字串 (非標準函式)
char* ltoa(long int value, char* string, int radix);
ex:
char buffer[200];
lnt value = 234;
ltoa(value, buffer, 2); // 將 234 轉為存成2進制之字串

9. strtod, strtol, strtoul 使用技巧
若有一字串裡面有二個浮點數,以空白為分隔符號
則可以下列方式取出
char buffer[] = "1.1   2.2";
char *pEnd ;
double d1, d2;
d1 = strtod(buffer, &pEnd);
d2 = strtod(pEnd, NULL);
若有若干浮點數,這些浮點數以空白為分隔符號
則可使用下列技巧依序取出浮點數
char buffer[] = "1.12 2.2 3.3 4.455 5.5123";
char *pStart = buffer;
char *pEnd = NULL;
double d;

while(1){
      d = strtod(pStart, &pEnd);
      if(pStart==pEnd) break;
      else pStart = pEnd;
      printf("%lf\n", d);
}
同理, strtol, strtoul 也是相同技巧。

10. 擅用 sprintf 與 sscanf
(10.1) sscanf - 字串轉整數、無號數、浮點數..
ex1 :
 char buffer[] = "-5, 10, 2.3, string";
 int i;
 unsigned u;
 double d;
 char buf2[200];
 sscanf(buffer, "%d, %u, %lf, %s", &i, &u, &d, &buf2);
 printf("%d, %u, %lf, %s\n", i, u, d, buf2);
ex2:  char demo[] = "1,2,3,4,5";
 char *ptr = demo;
 int a[5];
 int i=0, INT=0;
 while(sscanf(ptr, "%d,%s", &INT, ptr)==2) a[i++] = INT;
 a[i] = INT;
 for(i=0; i<5; i++) printf("%d ", a[i]);
(10.2) sprintf - 浮點數、整數、字串... 轉字串
ex: int a=0;
float b = 1.2;
char demo[] = "Test";
char dest[200];
sprintf(dest, "%d %f %s", a, b, demo);

2014年9月5日星期五

[UVa] 10361 - Automatic Poetry


久違的AC,注意換行符號不要存入s2~s5
輸出line2後再換行即可


#include <stdio.h>
#include <string.h>
char line1[10000]={'\0'};
char line2[10000]={'\0'};

int main(){
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  int n;
  scanf("%d",&n);
  getchar();
  while(n--){

    gets(line1);
    gets(line2);
    
    for(int i=0;i<strlen(line1);i++){
      if(line1[i]=='<' || line1[i]=='>'){}else printf("%c",line1[i]);
    }
    printf("\n");
    
    
    char s2[1000]={'\0'};
    char s3[1000]={'\0'};
    char s4[1000]={'\0'};
    char s5[1000]={'\0'};
    int flag=0,start_read=0,r=0,count=0;
    for(flag=0;flag<strlen(line1);flag++){
      if(start_read==1 && line1[flag]!='<' && line1[flag]!='>' && line1[flag]!='\n'){
        if(count==1)s2[r]=line1[flag];
        if(count==2)s3[r]=line1[flag];
        if(count==3)s4[r]=line1[flag];
        if(count==4)s5[r]=line1[flag];
        r++;
      }
      if(line1[flag]=='<'||line1[flag]=='>'){
        start_read=1;
        r=0;
        count++;
      }
    }
    
    int len2 = strlen(line2);
    for(flag=0;flag<len2;flag++){
      if(line2[flag]!='.')
        printf("%c", line2[flag]);
      else if(line2[flag]=='.'){
        for(int i=0;i<strlen(s4);i++)
          printf("%c",s4[i]);
        for(int i=0;i<strlen(s3);i++)
          printf("%c",s3[i]);
        for(int i=0;i<strlen(s2);i++)
          printf("%c",s2[i]);
        for(int i=0;i<strlen(s5);i++)
          printf("%c",s5[i]);
        break;
      }
      else ;
    }
    
    
    printf("\n");
    for(int i=0;i<1000;i++)line1[i]='\0';
    for(int i=0;i<1000;i++)line2[i]='\0';
    for(int i=0;i<1000;i++)s2[i]='\0';
    for(int i=0;i<1000;i++)s3[i]='\0';
    for(int i=0;i<1000;i++)s4[i]='\0';
    for(int i=0;i<1000;i++)s5[i]='\0';
  }
  return 0;
} 

2014年9月3日星期三

[UVa] 401 - Palindromes


#include <stdio.h>
#include <string.h>
char c[100]={'\0'};
char mirror[]="AAE3HHIIJLLJMMOOS2TTUUVVWWXXYYZ500112S3E5Z88"; //建立鏡像對照表
int is_mirror(char a, char b){
  for(int i=0;i<strlen(mirror);i++){
    if(a==mirror[i] && b==mirror[i+1]){  //如果此字元和下一字元比較相同,傳回1
   return 1;
 }
  }
  //沒回傳則傳回0
  return 0;
}
int main(){
   //freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);

  while(scanf("%s",c)!=EOF){
    for(int i=0;i<100;i++)if(c[i]==' ')c[i]='\0'; //把字串之後的空白也初始化為\0
    int ispalin=1 , ismirror=1;
 //printf("%d\n", strlen(c));

 for(int i=0;i<strlen(c)/2;i++){//比較回文
   if(c[i]!=c[strlen(c)-i-1]){
     ispalin=0;
   }
 }
 for(int i=0;i<strlen(c);i++){//比較字串
   if(!is_mirror(c[i], c[strlen(c)-i-1])){
     ismirror=0;
   }
 }
 
    if(ispalin && !ismirror)printf("%s -- is a regular palindrome.\n\n",c);
 if(!ispalin && ismirror)printf("%s -- is a mirrored string.\n\n",c);
 if(ispalin && ismirror)printf("%s -- is a mirrored palindrome.\n\n",c);
 if(!ispalin && !ismirror)printf("%s -- is not a palindrome.\n\n",c);
 for(int i=0;i<25;i++)c[i]='\0';
  }
  return 0;
} 

2014年9月1日星期一

[UVa] 457 - Linear Cellular Automata


#include <stdio.h>
#include <string.h>
int DNA[10],dish[40]={0},dish_next[40]={0},cases;
int main(){
   //freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
   
   
   scanf("%d",&cases);
   while(cases--){
     //getchar();
     for(int i=0;i<10;i++)scanf("%d",&DNA[i]);
     //for(int i=0;i<10;i++)printf("%d ",DNA[i]);
     dish[19]=1;
     for(int i=0;i<40;i++){
       if(dish[i]==1)printf(".");
         else if(dish[i]==2)printf("x");
         else if(dish[i]==3)printf("W");
         else printf(" ");
     }
     printf("\n");
     for(int i=0;i<49;i++){
       for(int k=0;k<40;k++){
         if(k==0)dish_next[k] = DNA[ dish[k]+dish[k+1] ];
         else if(k==39)dish_next[k] = DNA[ dish[k]+dish[k-1] ];
         else dish_next[k] = DNA[ dish[k-1]+dish[k]+dish[k+1] ];
       }
       for(int i=0;i<40;i++){
         if(dish_next[i]==1)printf(".");
         else if(dish_next[i]==2)printf("x");
         else if(dish_next[i]==3)printf("W");
         else printf(" ");
         dish[i] = dish_next[i];
       }
       printf("\n");
     }
     for(int i=0;i<40;i++){
       dish[i]=0;
       dish_next[i]=0;
     }
     if(cases)printf("\n"); //重要!檢查是否已經到最後了,最後不能輸出兩空行
   }
   return 0;
}