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 <bits/stdc++.h>
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 <bits/stdc++.h>
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 <bits/stdc++.h>
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;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值