题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087
http://acm.hdu.edu.cn/showproblem.php?pid=2089
两道”找元素“题
87题要求:算出在已有字符串中含有另一个字符串的个数
89题要求在一个数学区间中,对每个数字,查看是否含有不吉利数字62或4,求出该区间内不含有这些数字的个数
整体思路:因为题目给出范围都不算大(字符串长度和数字区间),用循环遍历加判断就ac了
我写了c语言的,如果用c++写,用s.find(),可以更简便。
87题代码:
#include <stdio.h>
#incude <string.h>
int main()
{
char x[1000], y[1000];
int a=0, b=0, c=0, d=0;
while ((a = scanf("%s %s", x, y)) == 2)
{
b = strlen(y);
for (int i = 0; x[i] != '\0'; i++)
{
c = i;
for (int j = 0; y[j] != '\0'; j++)
{
if (x[i] == y[j] && j == b - 1)
{
d++;
}
else if (x[i] == y[j]) i++;
else {i = c; break;
}
}
}
printf("%d\n", d);
d = 0;
}
return 0;
}
技巧:题目以‘#’代表结束,这里利用scanf的返回值,若不为二,即只给了一个字符串,就代表结束,而不需要判断是不是只给了‘#’
89题代码:
#include <stdio.h>
int main()
{
int a, b;
while (~scanf("%d %d", &a, &b) && (a + b))
{
int n = 0;
for (int i = a; i <= b; i++)
{
int j = i;
do {
if (j % 10 == 4)
{
n++; break;
}
if (j % 100 == 62)
{
n++; break;
}
j /= 10;
} while (j != 0);
}
printf("%d\n", b-a+1-n);
n = 0;
}
return 0;
}