UVA10483 - The Sum Equals the Product(枚举)

题目链接

题意:在实数a,b之间找到一个数c(最多到小数点的后两位),找出存在c = x + y + z = x * y * z,按字典序输出。

思路:先将数都扩大100倍,方便计算。但直接枚举所有情况的话会TLE,所以我们要缩小枚举范围。先枚举x,因为x,y,z要按照非递减顺序,所以x * x * x必须要小于c * 10000,再枚举y,同理可的x * y * y也必须小于c * 10000,至于z可以由公式(x + y + z)* 10000 = x * y * z得到,所以z = (x + y) * 10000
/ (x * y - 10000)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 10000;

struct state{
    double sum, x, y, z;
}ans[MAXN];

double a, b;
int n, m, cnt;

int cmp(state a, state b) {
    if (a.sum != b.sum)
        return a.sum < b.sum;
    if (a.x != b.x)
        return a.x < b.x;
    if (a.y != b.y)
        return a.y < b.y;
    if (a.z != b.z)
        return a.z < b.z;
}

int judge(int i, int j, int k) {
    int sum = i + j + k;
    int mul = i * j * k;
    if (sum != mul / 10000)
        return false;
    if (mul % 10000)
        return false;
    mul /= 10000;
    if (sum < n || sum > m || mul < n || mul > m)
        return false;
    return true;
}

void outPut() {
    sort(ans, ans + cnt, cmp);
    for (int i = 0; i < cnt; i++)
        printf("%.2lf = %.2lf + %.2lf + %.2lf = %.2lf * %.2lf * %.2lf\n",
                ans[i].sum, ans[i].x, ans[i].y, ans[i].z, ans[i].x, ans[i].y , ans[i].z);
}

int main() {
    while (scanf("%lf%lf", &a, &b) != EOF) {
        n = (int)(a * 100);
        m = (int)(b * 100);
        cnt = 0;
        for (int i = 1; (i * i * i) <= m * 10000; i++) {
            for (int j = i; (i * j * j) <= m * 10000; j++) {
                int sum = i + j;
                int mul = i * j;
                if (mul <= 10000)
                    continue;
                int k = sum * 10000 / (mul - 10000);
                if (k < j)
                    continue;
                if (!judge(i, j, k))
                   continue;
                ans[cnt].sum = (i * j * k * 1.0) / 1000000;
                ans[cnt].x = (i * 1.0) / 100;
                ans[cnt].y = (j * 1.0) / 100;
                ans[cnt].z = (k * 1.0) / 100;
                cnt++;
            }
        }
        outPut();
    }
    return 0;
}

UVA10483 - The Sum Equals the Product(枚举)

时间: 2024-11-15 18:24:35

UVA10483 - The Sum Equals the Product(枚举)的相关文章

UVA - 10483 The Sum Equals the Product 暴力

题目大意:给出一个范围,要求找出这个范围内满足 n = x + y + z = x * y * z的所有式子 解题思路:因为这里的小数位精确到了0.01,所以先讲x,y,z都相应放大100倍 从小到大枚举,假设x <= y <= z,那么x的范围就在 (0,n ^(1/3)],y的范围就在[x,(n-x)^(1/2)]这样范围就可以确定下来了 而z又可以通过x和y得到 因为x + y + z = x * y * z,且x,y,z都是放大100倍后的 所以(x + y + z) * 10000

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

[leetcode 560. Subarray Sum Equals K]

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

Map-560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

[LeetCode] 560. Subarray Sum Equals K_Medium

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

[LintCode] 1844. subarray sum equals k II

Given an array of integers and an integer k, you need to find the minimum size of continuous subarrays whose sum equals to k, and return its length. if there are no such subarray, return -1. Example Example1 Input: nums = [1,1,1,2] and k = 3 Output:

uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

POJ2785 4 Values whose Sum is 0 【枚举】

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 16000   Accepted: 4625 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how

325. Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Note: The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. Example 1: Given