枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)

 1 //Uva725
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 using namespace std;
 7
 8 void evalu(int n)
 9 {
10     const int maxn = 1024 + 10;
11     char num[10];                      //将数字用字符保存
12     int flag[10];                      //判断每个数,是否重复
13     char buf[maxn];                    //将出现的字符全部存到里面
14     for (int i = 1234; i <= 5000; i++)
15     {
16         memset(flag, 0, sizeof(flag));
17         memset(num, 0, sizeof(num));
18         sprintf(num, "%05d", i);
19 //        cout << "Debug: " << num << endl;
20 //        system("pause");
21         int digit = 0, rest = 0;
22         digit = (num[0]-‘0‘)*10000 + (num[1]-‘0‘)*1000 + (num[2]-‘0‘)*100 + (num[3]-‘0‘)*10 + (num[4]-‘0‘);
23 //        cout << "Debug:digit: " << digit << endl;
24 //        system("pause");
25         rest = digit * n;
26         sprintf(buf, "%05d%05d", rest, digit);
27         int len = strlen(buf), j = 0;
28         for (j = 0; j < len; j++) {
29             if (flag[buf[j] - ‘0‘]) {
30                 break;
31             }
32             else {
33                 flag[buf[j] - ‘0‘] = 1;
34             }
35         }
36         if (j == len) {
37             cout << rest << " / " << num << " = " << n << endl;
38         }
39     }
40 }
41
42
43 int main()
44 {
45     int num;
46     while (cin >> num) {
47         evalu(num);
48     }
49     return 0;
50 }
 1 //Uva11059
 2 #include <iostream>
 3 #include <vector>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <fstream>
 7 using namespace std;
 8
 9 //ifstream in("in.txt");
10 //ofstream out("out.txt");
11
12 int main()
13 {
14     long long pro = 1, max_pro = 0;
15     vector<long long> num;
16     long long data;
17     int T, kase = 0;
18     while (cin >> T)
19     {
20         num.clear();
21         pro = max_pro = 0;
22         while (T--) {
23             cin >> data; num.push_back(data);
24         }
25         for (unsigned i = 0; i < num.size(); i++) {
26             pro = num[i];
27             for (unsigned j = i; j < num.size(); j++) {
28                 if (i != j) {
29                     pro *= num[j];            //pro尽管乘
30                     if (pro > num[i])         //pro > num[i]
31                         num[i] = pro;         //num[i] = pro, 将最大的乘积放到该位置
32                 }
33             }
34         }
35         for (unsigned i = 0; i < num.size(); i++) {
36             if (num[i] > max_pro) {
37                 max_pro = num[i];
38             }
39         }
40         cout << "Case #" << ++kase << ": The maximum product is " << max_pro << "." << "\n\n";
41     }
42     return 0;
43 }
44  
时间: 2024-10-12 03:27:39

枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)的相关文章

leecode 每日解题思路 152 Maximun Product Subarray

问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问题的核心思路与解决最大子段和相同, 但是唯一需要注意的就是负数的情况. 每次在比较当前最大结果的同时,也需要保存当前最小结果,所以每个当前点i处的取值, 就是从当前值nums[i], 和cur_max+nums[i], cur_min+nums[i]三者中取极值: 每次的取值递推路径如上所示, 应该

2的幂的合并运算实例

Powers of two can be combined, under the laws of exponents, to create other powers of two. Under these rules, you can multiply powers of two, divide powers of two, or raise a power of two to a power and still get another power of two. You can combine

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

UVA 11059 Maximum Product(枚举start+end)

题目大意: 就是说给你n个数字,然后求出连续序列的最大和.如果ans为负数就输出0,要么就输出这个ans. 解题思路: 其实我们只需要枚举起点和终点,然后考虑所有的情况就可以了,有点类似于求解最大连续和的问题. 唯一的不同就是每次都要初始化t = 1, t*=a[j].注意long long.因为最多有18个数字,且每个数字的 最大值为10,那么他们的乘积是不会超过10^18的. 代码: # include<cstdio> # include<iostream> # include

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

UVA725 Division【枚举】

  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij = N wher

UVA 725 Division ( 找出 abcde / fghij = N的形式—— 暴力枚举 )

Division Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divide

uva 725 Division(暴力枚举)

uva 725  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =