# 大一上 程設一 ## 2604 mid1_practice ## 11628 - Spiral ``` #include <stdio.h> int main(void) { int t,p,i,j,k,counter=0,c,cc; scanf("%d",&t); for(p=0;p<t;p++){ scanf("%d",&c); cc = c; int a=0,b=0; counter = 0; char spr[c][c]; for(i=0;i<c;i++){ for(j=0;j<c;j++){ spr[i][j]=32; } } while (c>0){ if (counter%4==0){ if(a!=0&&b!=0) b++; for (j=0;j<c;j++){ spr[a][b+j] = '#'; } b+=j-1; /* for(k=0;k<cc;k++){ for(j=0;j<cc;j++){ printf("%c",spr[k][j]); } printf("\n"); }*/ } else if(counter%4==1){ a++; for (j=0;j<c;j++){ spr[a+j][b] = '#'; } a+=j-1; /* for(k=0;k<cc;k++){ for(j=0;j<cc;j++){ printf("%c",spr[k][j]); } printf("\n"); } ` */ } else if(counter%4==2){ b--; for (j=0;j<c;j++){ spr[a][b-j] = '#'; } b-=j-1; /* for(k=0;k<cc;k++){ for(j=0;j<cc;j++){ printf("%c",spr[k][j]); } printf("\n"); } */ } else if(counter%4==3){ a--; for (j=0;j<c;j++){ spr[a-j][b] = '#'; } a-=j-1; /* for(k=0;k<cc;k++){ for(j=0;j<cc;j++){ printf("%c",spr[k][j]); } printf("\n"); } */ } //printf("%d %d\n",a,b); counter++; c-- ; } for(k=0;k<cc;k++){ for(j=0;j<cc;j++){ printf("%c",spr[k][j]); } printf("\n"); } } return 0; } ``` ### 12022 - prefix sum ``` #include <stdio.h> int main(void) { int n,m,i,j,l,r,c=0; scanf("%d",&n); int arr[n]; long long map[n+1]; map[0] = 0; for(i=0;i<n;i++){ scanf(" %d",&arr[i]); map[i+1] = map[i]+arr[i]; } scanf("%d",&m); for(i=0;i<m;i++){ scanf("%d %d",&l,&r); printf("%d\n",map[r]-map[l-1]); } return 0; } ``` ### 12441 - Palindrome ``` #include <stdio.h> #include <string.h> int main(void) { int i,l,counter; char arr[100001]; while(scanf("%s",arr)!= EOF){ counter = 0; size_t lenth = strlen(arr); l = lenth/2+lenth%2; for(i=0;i<l;i++){ if(arr[i] == arr[lenth-1-i]) counter++; } if(counter == l) printf("Yes\n"); else printf("No\n"); } return 0; } ``` ### 13633 - Indentation ``` #include <stdio.h> #include <string.h> int main(void) { int i,counter; char arr[100]; while(fgets(arr,100,stdin)){ size_t lenth = strlen(arr); for(i=0;i<lenth;i++){ if (arr[i] == 59) arr[i] = 32 ; else break; } if (arr[0]!=10){ for(i=0,counter=0;i<lenth-1;i++){ printf("%c",arr[i]); if (arr[i]=='{' || arr[i]=='}') counter++; } if (arr[lenth-2] == 125 && counter==2) printf(";\n"); else if (arr[lenth-2] != 123 && arr[lenth-2] != 125 && arr[lenth-2] !=62 ) { printf(";\n"); } else printf("\n"); } } return 0; } /* sample #include<stdio.h> int main(){ ;;;;int a, b ;;;;scanf("%d%d", &a, &b) ;;;;printf("%d\n", a + b ;;;;int arr[3] = {0, 1} ;;;;return 0 ;} */ ``` ### 13637 - Easy Gomoku Validator (4/6未過) ``` #include <stdio.h> int main(void) { int n,i,j,k,l,a,b,c,r=0; scanf("%d",&n); char z[15][15]; for(i=0;i<15;i++){ for(j=0;j<15;j++){ z[i][j] = ' '; } } for (i=0;i<(n/2+n%2);i++){ scanf("%d%d",&j,&k); r++; z[j-1][k-1] = 'x'; if (r>8){ for (a=0;a<15;a++){ for (b=0;b<15;b++){ if (z[a][b] == 'x'){ for(l=1,c=0;l<5;l++){ if(z[a+l][b]=='x') c++; } if (c==4) { printf("LSC wins at the %dth step.\n",r); return 0; } for(l=1,c=0;l<5;l++){ if(z[a][b+l]=='x') c++; } if (c==4) { printf("LSC wins at the %dth step.\n",r); return 0; } for(l=1,c=0;l<5;l++){ if(z[a+l][b+l]=='x') c++; } if (c==4) { printf("LSC wins at the %dth step.\n",r); return 0; } for(l=1,c=0;l<5;l++){ if(z[a+l][b-l]=='x') c++; } if (c==4) { printf("LSC wins at the %dth step.\n",r); return 0; } } } } } scanf("%d%d",&j,&k); r++; z[j-1][k-1] = 'y'; if (r>8){ for (a=0;a<15;a++){ for (b=0;b<15;b++){ if (z[a][b] == 'y'){ for(l=1,c=0;l<5;l++){ if(z[a+l][b] =='y') c++; } if (c==4) { printf("1TSC wins at the %dth step.\n",r); return 0; } for(l=1,c=0;l<5;l++){ if(z[a][b+l]=='y') c++; } if (c==4) { printf("2TSC wins at the %dth step.\n",r); return 0; } for(l=1,c=0;l<5;l++){ if(z[a+l][b+l]=='y') c++; } if (c==4) { printf("3TSC wins at the %dth step.\n",r); return 0; } for(l=1,c=0;l<5;l++){ if(z[a+l][b-l]=='y') c++; } if (c==4) { printf("TSC wins at the %dth step.\n",r); return 0; } } } } } } /* for(j=0;j<15;j++){ for(k=0;k<15;k++){ printf("%c",z[j][k]); } printf("\n"); } */ printf ("There's no winner.\n"); return 0; } ``` ## 2603 hw6 ### 13309 - How much is the string worth 2 ``` #include <stdio.h> #include <string.h> int main(void) { int i,j,counter=0; char arr[27],map[101]; scanf("%s",arr); size_t l1 = strlen(arr); while(1){ counter = 0; fgets (map, 101, stdin); size_t l2 = strlen(map)-1; if(l2 == 3 && map[0]==101 && map[1]==110 && map[2]==100) break; else if(l2 == 1 && map[0]==48) break; else if (map[0] != 10){ for(i=0;i<l2;i++){ for(j=0;j<l1;j++){ if(map[i]==arr[j]) counter += (j+1) ; } } printf("%d\n",counter); } } return 0; } ``` ### 12434 - Bacteria Widespread ``` #include <stdio.h> int main(void) { int r,c,t,i,j,k; scanf("%d%d%d",&r,&c,&t); char cla[r][c+1],temp[r][c+1]; for (i=0;i<r;i++){ scanf("%s",cla[i]); } for (i=0;i<r;i++){ for (j=0;j<c;j++){ temp[i][j] = cla[i][j]; } } for(k=0;k<t;k++){ for (i=0;i<r;i++){ for (j=0;j<c;j++){ cla[i][j] = temp[i][j]; } } for (i=0;i<r;i++){ for (j=0;j<c;j++){ if (cla[i][j]==70){ if(cla[i+1][j]==67 && i+1<r-1) temp[i+1][j] = 70 ; if(cla[i-1][j]==67 && i-1>0) temp[i-1][j] = 70 ; if(cla[i][j+1]==67 && j+1<c-1) temp[i][j+1] = 70 ; if(cla[i][j-1]==67 && j-1>0) temp[i][j-1] = 70 ; } } } } for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("%c",temp[i][j]); } printf("\n"); } } ``` ## 2593 hw5 ### 13591 - Second Highest Value ``` #include <stdio.h> int main(void) { int n,max,sux; scanf("%d",&n); int arr[n]; for (int i=0;i<n;i++){ scanf("%d",&arr[i]); } printf("0"); if (arr[0]>arr[1]){ max = arr[0]; sux = arr[1]; } else{ max = arr[1]; sux = arr[0]; } for(int i = 1;i<n;i++) { if (arr[i]>max){ sux = max ; max = arr[i]; } else if (max>arr[i] && arr[i]>sux){ sux = arr[i]; } printf(" %d",sux); } printf("\n"); return 0; } ``` ### 13572 - String Operations 1 ``` #include <stdio.h> #include <string.h> int main(void) { int i,j,q ; char s[1000000]; char map[26]= {'a', 'b', 'c', 'd', 'e', 'f','g','h','i','j', 'k','l','m','n','o', 'p','q','r','s','t', 'u','v','w','x','y','z'}; char a,b; scanf("%s",s); size_t l = strlen(s); scanf("%d",&q); for(i=0;i<q;i++){ scanf(" %c %c",&a,&b); for(j=0;j<26;j++){ if (map[j] == a) { map[j] = b ; } } } for(i=0;i<l;i++){ printf("%c",map[s[i]-97]); } printf("\n"); return 0; } ``` ### 13290 - Matching Strings ``` #include <stdio.h> #include <string.h> int main(void) { int n ,m ,i,j,k,c,l=0; scanf("%d%d",&n,&m); char s1[n][1000] ; char s2[1000] ; for (i=0;i<n;i++){ scanf("%s",s1[i]); } for (i=0;i<m;i++){ scanf("%s",s2); size_t length = strlen(s2); for(j=0,c=0;j<n;j++){ for(k=0;k<length;k++){ if (s2[k] == s1[j][k]){ //printf("%d %d\n",j,k); c++ ; } } if(c == length){ printf("Yes\n"); l++; break; } else{ c=0; } } if (l == 0) printf("No\n"); } return 0; } ```