题目大一:根据题目的算法,找出最小的C。
解题思路:因为题目有说C至少要是w集合的某一个的倍数,又要找最小的,所以现在w内找个最小的,然后在根据所给的条件遍历判度所有的w是否满足要求,如果不满足就根据要求更换C,然后再次判断。
#include<stdio.h>
#include<string.h>
const int N = 100000;
char s[N];
int word[15], n, flag;
int min( int a, int b) {
return a < b ? a:b;
}
void handle() {
n = 0;
memset(word, 0, sizeof(word));
for ( int i = 0; s[i] ; i++) {
if (s[i] != ' ')
word[n] = word[n] * 32 + s[i] - 'a' + 1;
else if ( s[i] == ' ' && s[i - 1] != ' ')
n++;
}
n++;
}
int cal(int cur) {
int i, j;
for (i = 1; i <= n; i++)
for (j = i + 1; j <= n ;j++) {
if ( word[j - 1] == 0 || word[ i - 1] == 0 )
continue;
if (cur / word[i - 1] % n == cur / word[ j - 1] % n)
return min((cur / word[i - 1] + 1 ) * word[i - 1] , ( cur / word [ j - 1] + 1) * word[j- 1] );
}
flag = 1;
return cur;
}
int main() {
while (gets(s)) {
flag = 0;
handle();
puts(s);
int max = 999999999;
for (int i = 0; i < n; i++ )
if ( word[i] < max)
max = word[i];
while (!flag) {
max = cal(max);
}
printf("%d\n\n", max);
}
return 0;
}