题目大意:判断回文和镜面字符串。
#include
#include
#include
int JudgePal(char p[]) {
int i, j=strlen(p)-1;
for (i = 0; i < j; i++, j--) {
if ((p[i] == '0' && p[j] == 'O') || (p[i] == 'O' && p[j] == '0'))
continue;
if (p[i] != p[j]) return 0;
}
return 1;
}
int JudgeMir(char p[]) {
char temp[50];
char letter[] = { 'A', ' ', ' ', ' ', '3', ' ', ' ', 'H',
'I', 'L', ' ', 'J', 'M', ' ', 'O', ' ', ' ', ' ', '2', 'T', 'U', 'V', 'W', 'X', 'Y', '5' };
char number[] = { '1', 'S', 'E', ' ', 'Z', ' ', ' ', '8', ' ' };
int l = strlen(p), i, j, index;
for (i = 0, j = l - 1; i < l; i++, j--) {
if (p[i] >= 'A' && p[i] <= 'Z'){
index = p[i] - 'A';
temp[i] = letter[index];
}
else{
index = p[i] - '1';
temp[i] = number[index];
}
if ((temp[i] == '0' && p[j] == 'O') || (temp[i] == 'O' && p[j] == '0'))
continue;
if (temp[i] != p[j]) return 0;
}
return 1;
}
int main() {
char s[50];
while (scanf("%s", s) != EOF) {
if (JudgePal(s) == 0 && JudgeMir(s) == 0)
printf("%s -- is not a palindrome.\n",s);
if (JudgePal(s) == 1 && JudgeMir(s) == 0)
printf("%s -- is a regular palindrome.\n",s);
if (JudgePal(s) == 0 && JudgeMir(s) == 1)
printf("%s -- is a mirrored string.\n",s);
if (JudgePal(s) == 1 && JudgeMir(s) == 1)
printf("%s -- is a mirrored palindrome.\n",s);
printf("\n");
}
return 0;
}
题目大意:在一个网格表中匹配给出的单词,可以从八个方向匹配到此,如果匹配成功则输出单词首字母在网格表中的坐标,如果一个单词在同一列中被匹配,以靠近顶部的为准,如果一个单词在同一行中被匹配,以靠近左边的为准。
#include
#include
char s[51][51];
char str[21];
int Match(int M, int N, int *Row, int *Column) {
int i, j, k, flag;
*Row = *Column = 51;
int len = strlen(str);
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
flag = 1;
if (str[0] == s[i][j] && j + len <= N) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i][j + k]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && j + 1 - len >= 0) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i][j - k]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && i + len <= M) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i + k][j]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && i + 1 - len >= 0) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i - k][j]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && j + len <= N && i - len + 1 >= 0) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i - k][j + k]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && j + len <= N && i + len <= M){
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i + k][j + k]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && j + 1 - len >= 0 && i + 1 - len >= 0) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i - k][j - k]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
if (str[0] == s[i][j] && j + 1 - len >= 0 && i + len <= M) {
flag = 0;
for (k = 0; k < len; k++) {
if (str[k] != s[i - k][j + k]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (*Row > i + 1) {
*Row = i + 1;
*Column = j + 1;
}
else if (*Row == i + 1 && *Column > j + 1) {
*Row = i + 1;
*Column = j + 1;
}
}
}
}
}
return 0;
}
int main() {
int i, j, Case, k, M, N;
int Row, Column;
/* freopen("D:\\in.txt","r",stdin); */
while (scanf("%d", &Case) != EOF) {
while (Case--) {
scanf("%d %d", &M, &N);
for (i = 0; i < M; i++) {
scanf("%s", s[i]);
for (j = 0; j < N; j++) {
if (s[i][j] >= 'A' && s[i][j] <= 'Z') {
s[i][j] = s[i][j] - 'A' + 'a';
}
}
}
scanf("%d", &k);
for (i = 0; i < k; i++) {
scanf("%s", str);
int len = strlen(str);
for (j = 0; j < len; j++) {
if (str[j] >= 'A' && str[j] <= 'Z') {
str[j] = str[j] - 'A' + 'a';
}
}
Match(M, N, &Row, &Column);
printf("%d %d\n", Row, Column);
}
if (Case) printf("\n");
}
}
return 0;
}
题目大意:把s1
#include
#include
char str1[110], str2[110], s1[110], s2[110], s3[110], s4[110], s5[110], s6[110];
int main()
{
int Case,i,j,k;
/*freopen("D:\\in.txt", "r", stdin);*/
scanf("%d", &Case);
getchar();
while (Case--) {
gets(str1);
gets(str2);
int l = strlen(str1);
for (i = 0, j = 0; str1[i]!='<'; i++, j++) {
s1[j] = str1[i];
}
s1[j] = '\0';
for (i = i + 1, j = 0; str1[i]!='>'; i++, j++) {
s2[j] = str1[i];
}
s2[j] = '\0';
for (i = i + 1, j = 0; str1[i]!='<'; i++, j++) {
s3[j] = str1[i];
}
s3[j] = '\0';
for (i = i + 1, j = 0; str1[i]!='>'; i++, j++) {
s4[j] = str1[i];
}
s4[j] = '\0';
for (i = i + 1, j = 0; i
题目大意:判断题目中给出的U、I、P其中两个的信息,然后根据公式P=UI计算出结果,注意题目中给出数值的单位。
#include
#include
char s[1024];
int main() {
int Case, i, j, l, count=1;
double pp, uu, ii, temp;
/*freopen("D:\\in.txt", "r", stdin);*/
scanf("%d", &Case);
getchar();
while (Case--) {
gets(s);
pp = uu = ii = 0; temp = 1;
l = strlen(s);
for (i = 0; i < l; i++) {
if (s[i] == 'P' && s[i + 1] == '=') {
i += 2;
while (s[i] >= '0' && s[i] <= '9') {
pp = pp * 10 + s[i] - '0';
i++;
}
if (s[i] == '.') {
i++;
while (s[i] >= '0' && s[i] <= '9') {
temp *= 0.1;
pp += temp*(s[i] - '0');
i++;
}
}
temp = 1;
if (s[i] == 'k') {
pp *= 1000;
i++;
}
else if (s[i] == 'm') {
pp /= 1000;
i++;
}
else if (s[i] == 'M') {
pp *= 1000000;
i++;
}
}
else if (s[i] == 'U' && s[i + 1] == '=') {
i += 2;
while (s[i] >= '0' && s[i] <= '9') {
uu = uu * 10 + s[i] - '0';
i++;
}
if (s[i] == '.') {
i++;
while (s[i] >= '0' && s[i] <= '9') {
temp *= 0.1;
uu += temp*(s[i] - '0');
i++;
}
}
temp = 1;
if (s[i] == 'k') {
uu *= 1000;
i++;
}
else if (s[i] == 'm') {
uu /= 1000;
i++;
}
else if (s[i] == 'M') {
uu *= 1000000;
i++;
}
}
else if (s[i] == 'I' && s[i + 1] == '=') {
i += 2;
while (s[i] >= '0' && s[i] <= '9') {
ii = ii * 10 + s[i] - '0';
i++;
}
if (s[i] == '.') {
i++;
while (s[i] >= '0' && s[i] <= '9') {
temp *= 0.1;
ii += temp*(s[i] - '0');
i++;
}
}
temp = 1;
if (s[i] == 'k') {
ii *= 1000;
i++;
}
else if (s[i] == 'm') {
ii /= 1000;
i++;
}
else if (s[i] == 'M') {
ii *= 1000000;
i++;
}
}
}
printf("Problem #%d\n", count);
if (pp > 0 && ii > 0) {
printf("U=%.2fV\n", pp / ii);
}
else if (pp > 0 && uu > 0) {
printf("I=%.2fA\n", pp / uu);
}
else if (uu > 0 && ii > 0) {
printf("P=%.2fW\n", uu * ii);
}
printf("\n");
count++;
}
return 0;
}
题目大意:判断给出的关键字在所给的所有句子中出现的次数,输出关键字出现最多次的句子,如果关键字出现最多次的句子不止一句则同时输出,判断时忽略大小写。
#include
#include
char keyword[50][50];
char s[50][110];
char temp[110];
int main()
{
int K, E, i, j, k, l, n, t, number, Case = 1;
/*freopen("D:\\in.txt", "r", stdin);*/
while (scanf("%d %d\n", &K, &E) != EOF) {
number = 0;
char count[50] = {0};
for (i = 0; i < K; i++)
gets(keyword[i]);
for (i = 0; i < E; i++) {
gets(s[i]);
l = strlen(s[i]);
for (j = 0; j < l; j++) {
n = 0;
while (s[i][j] >= 'A' && s[i][j] <= 'Z' || s[i][j] >= 'a' && s[i][j] <= 'z') {
if (s[i][j] >= 'A' && s[i][j] <= 'Z')
temp[n++] = s[i][j] - 'A' + 'a';
else if (s[i][j] >= 'a' && s[i][j] <= 'z')
temp[n++] = s[i][j];
j++;
}
temp[n] = '\0';
for (k = 0; k < K; k++) {
if (strcmp(temp, keyword[k]) == 0)
count[i]++;
}
}
if (number < count[i])
number = count[i];
}
printf("Excuse Set #%d\n", Case);
Case++;
for (i = 0; i < E; i++){
if (number == count[i]){
puts(s[i]);
}
}
printf("\n");
}
return 0;
}
题目大意:根据磁带打印出磁带中蕴含的信息。
#include
#include
int main() {
char s[15];
int asc, i;
/*freopen("D:\\in.txt", "r", stdin);*/
gets(s);
while (gets(s) && s[0] == '|') {
asc = 0;
for (i = 2; i < strlen(s); i++) {
if (s[i] == 'o') {
asc = asc * 2 + 1;
}
else if (s[i] == ' ') {
asc *= 2;
}
}
printf("%c", asc);
}
return 0;
}
题目大意:找出一篇文章中出现的所有单词,然后根据单词首字母按字典顺序输出,重复的单词只输出一次。
#include
#include
#include
int cmp(const void* s1, const void * s2) {
return strcmp((char *)s1, (char*)s2);
}
char s[30000][205] = { '\0' };
int main() {
char c;
int i = 0, j = 0, flag = 0, count;
/*freopen("D:\\in.txt", "r", stdin);*/
while ((c = getchar()) != EOF) {
if (c >= 'A'&&c <= 'Z' || c >= 'a' && c <= 'z') {
flag = 1;
if (c >= 'A'&&c <= 'Z') {
c = c - 'A' + 'a';
s[i][j] = c;
j++;
}
else {
s[i][j] = c;
j++;
}
}
else if (flag == 1) {
flag = 0;
s[i][j] = '\0';
i++;
j = 0;
}
}
count = i;
qsort(s, i, sizeof(s[0]), cmp);
for (i = 0; i < count; i++) {
if (strcmp(s[i], s[i+1]))
puts(s[i]);
}
return 0;
}
题目大意:判断所给的几组数字能否用来编码,如果其中的一组数字是另一组数字的前缀,则不能编码,否则可以编码。
#include
#include
char s[100][100];
int cmp(char s1[], char s2[]) {
int min, i;
min = strlen(s1) > strlen(s2) ? strlen(s2) : strlen(s1);
for (i = 0; i < min; i++) {
if (s1[i] != s2[i])
return 1;
}
return 0;
}
int main() {
int i, j, l, k, n, flag = 0, Case = 0, Row;
/*freopen("D:\\in.txt", "r", stdin);*/
for (i = 0; scanf("%s", s[i]) != EOF; i++) {
if (s[i][0] == '9') {
Row = i;
Case++;
for (j = 0; j < Row-1; j++) {
for (k = j + 1; k < Row; k++) {
if (cmp(s[j], s[k]) == 0) {
flag = 1;
break;
}
}
if (flag) break;
}
if (flag) printf("Set %d is not immediately decodable\n", Case);
else printf("Set %d is immediately decodable\n", Case);
flag = 0;
i = -1;
}
}
return 0;
}
题目大意:根据所给出的find字符串寻找在所给句子中的位置,并替换成题目所给的replace-by字符串,直到所给的find字符串在句子中搜索不到。
#include
#include
int main() {
int n, i;
char find[11][85], rep[11][85], s[260], temp[300];
char *search, *t;
/*freopen("D:\\in.txt", "r", stdin);*/
while (scanf("%d\n", &n)!=EOF) {
if (n == 0) break;
for (i = 0; i < n; i++) {
gets(find[i]);
gets(rep[i]);
}
gets(s);
for (i = 0; i < n; i++)
while (1) {
search = strstr(s, find[i]);
if (search == NULL) break;
else {
t = search + strlen(find[i]);
strcpy(temp, rep[i]);
strcat(temp, t);
strcpy(search, temp);
}
}
puts(s);
}
return 0;
}