Division UVA - 725 , Maximum Product UVA - 11059 (简单枚举)

目录

Division UVA-725:

Maximum Product  UVA - 11059

Fractions Again?!UVA - 10976


Division UVA-725

Maximum Product UVA - 11059

Fractions Again?! UVA - 10976

 Division UVA-725:

题目大意:

给定一个正整数n,请按照从小到大的顺序输出形如abcde / fghij = n的表达式,其中每个字母代表0~9中的一个数字且每个数字只出现一次。

分析:

只需要枚举fghij就可以运算出abcde,然后判断所有数字是否相同即可。

#include 
using namespace std;
int cnt[10];

void count(int num) {
    int t = num;
    if(t>98766) return;
    bool flag = false;
    while(t>0) {
        cnt[t%10]++;
        if(t%10==0) flag = true;
        t /= 10;
    }
    if(num<=9999 && !flag) cnt[0]++;
    return ;
}

int main() {
    freopen("i.txt","r",stdin);;
    freopen("o.txt","w",stdout); 
    int n, rnd = 0;
    while(cin >> n && n) {
        if(rnd++) cout << endl;
        bool have = false;
        for(int i = 1234; i <= 98765; i++) {
            memset(cnt, 0, sizeof(cnt));
            bool flag = true;
            count(i);
            count(i*n);
            for(int j = 0; j < 10; j++) {
                if(cnt[j]!=1) flag = false;
            }
            if(flag) {
                printf("%05d / %05d = %d\n",n*i, i, n);
                have = true;
            }
        }
        if(!have)
            cout << "There are no solutions for " << n << "." << endl;
    }
    return 0;
}

Maximum Product  UVA - 11059

题目大意:

找出一个序列中乘积最大的连续子序列。

分析:

枚举起点和终点即可。注意要属于long long。

#include 
using namespace std;
const int maxn = 20;
int arr[maxn];
long long mm;

long long calculate(int a, int b) {
    long long sum = 1;
    for(int i = a; i <= b; i++) 
        sum *= arr[i];
    return sum;
}

int main() {
    freopen("i.txt","r",stdin);
    freopen("o.txt","w",stdout); 
    int n,rnd = 0;
    while(cin >> n) {
        cout << "Case #" << ++rnd << ": ";
        memset(arr, 0, sizeof(arr)); mm = 0;
        for(int i = 0; i < n; i++)
            cin >> arr[i];
        for(int i = 0; i < n; i++) {
            for(int j = i; j < n; j++)
                mm = max(mm,calculate(i,j));
        }
        cout << "The maximum product is " << mm << "." << endl << endl;
    }
    return 0;
}

Fractions Again?!UVA - 10976

分析:

从x+1-2*x之间枚举即可。

#include 
using namespace std;


int main() {
    freopen("i.txt","r",stdin);
    freopen("o.txt","w",stdout); 
    int n;
    while(cin >> n) {
        int cnt = 0;
        for(int i = n+1; i <= 2*n; i++) {
            int n1 = i*n;
            int n2 = i-n;
            if(n1%n2==0) cnt++; 
        }
        cout << cnt << endl;
        for(int i = n+1; i <= 2*n; i++) {
            int n1 = i*n;
            int n2 = i-n;
            if(n1%n2==0) 
                printf("1/%d = 1/%d + 1/%d\n",n , n1/n2, i);
        }
    }
    return 0;
}

 

 

你可能感兴趣的:(刷题)