思路:除了把它的六种结果都写出来,我想不到更好的方法了。这个题做得很乱,因为思路不够清晰,每个步骤都相似,数据要分得很明确,一不小心就错了。还有审题问题。
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int maxn = 12 + 5; int main() { int t, n, m, u; char str[maxn]; int num1[maxn], num2[maxn]; scanf("%d", &t); for(int i = 0; i < t; i++){ n = m = u = 0; memset(str, 0, sizeof(str)); memset(num1, 0, sizeof(num1)); memset(num2, 0, sizeof(num2)); scanf("%s", str); for(int j = 0; j < 12; j++){ if(str[j] == 'O') m++; } if(m == 12){ printf("0\n"); continue; } else{ n = 1; num1[0] = 1, num2[0] = 12; for(int j = 0; j < 6; j++){ m = 0; for(int k = j; k < 12; k = (k + 6)){ if(str[k] == 'X') m++; } if(m == 2){ num1[1] = 2, num2[1] = 6; n++; break; } else continue; } for(int j = 0; j < 4; j++){ m = 0; for(int k = j; k < 12; k = (k + 4)){ if(str[k] == 'X') m++; } if(m == 3){ num1[2] = 3, num2[2] = 4; n++; break; } else continue; } for(int j = 0; j < 3; j++){ m = 0; for(int k = j; k < 12; k = (k + 3)){ if(str[k] == 'X') m++; } if(m == 4){ num1[3] = 4, num2[3] = 3; n++; break; } else continue; } for(int j = 0; j < 2; j++){ m = 0; for(int k = j; k < 12; k = (k + 2)){ if(str[k] == 'X') m++; } if(m == 6){ num1[4] = 6, num2[4] = 2; n++; break; } else continue; } m = 0; for(int j = 0; j < 12; j++){ if(str[j] == 'X') m++; } if(m == 12){ num1[5] = 12, num2[5] = 1; n++; } printf("%d", n); for(int j = 0; j <= 6 ; j++){ if((num1[j] != 0) && (num2[j] != 0)){ printf(" %dx%d", num1[j], num2[j]); u++; } else{ if(u == n){ printf("\n"); break; } else continue; } } } } return 0; }