題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2087
http://acm.hdu.edu.cn/showproblem.php?pid=2089
兩道”找元素“題
87題要求:算出在已有字符串中含有另一個字符串的個數(shù)
89題要求在一個數(shù)學(xué)區(qū)間中寻行,對每個數(shù)字,查看是否含有不吉利數(shù)字62或4赌蔑,求出該區(qū)間內(nèi)不含有這些數(shù)字的個數(shù)
整體思路:因為題目給出范圍都不算大(字符串長度和數(shù)字區(qū)間),用循環(huán)遍歷加判斷就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;
}
技巧:題目以‘#’代表結(jié)束趾浅,這里利用scanf的返回值,若不為二馒稍,即只給了一個字符串皿哨,就代表結(jié)束,而不需要判斷是不是只給了‘#’
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;
}