UVa 725 Division 和 UVa 11059 Maximum Product

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
2 ≤ N ≤ 79. That is,
abcde
fghij = N
where each letter represents a different digit. The first digit of one of the numerals is allowed to be
zero.
Input
Each line of the input file consists of a valid integer N. An input of zero is to terminate the program.
Output
Your program have to display ALL qualifying pairs of numerals, sorted by increasing numerator (and,
of course, denominator).
Your output should be in the following general form:
xxxxx / xxxxx = N
xxxxx / xxxxx = N
.
.
In case there are no pairs of numerals satisfying the condition, you must write ‘There are no
solutions for N.’. Separate the output for two different values of N by a blank line.
Sample Input
61
62
0
Sample Output
There are no solutions for 61.
79546 / 01283 = 62
94736 / 01528 = 62

唉,不得不说暴力也是要讲脑子的,刷够了一百题,痛痛快快的玩了好几天,好几天没写代码了,脑子都有点不好使了,这个暴力我还想深搜枚举尝试

#include<iostream>
#include<cstring>
#include<cstdio>
#define Maxn 12
#include<algorithm>
using namespace std;
char ans[11] = "0123456789";
char str[11];
inline bool cmp (char a,char b) { return a < b; }

int main(int argc,char* argv[]) {
	int n,kase = 0,p;
	while(scanf("%d",&n) == 1 && n) {
		bool flag = false;
		if(kase) printf("\n"); kase++;
		for(int i=1234; i<=50000; i++) {
			p = n * i;
			if(p > 98765) break;
			if(i < 10000) sprintf(str,"%d%d%d",0,i,p);
			else sprintf(str,"%d%d",i,p);
			sort(str,str + 10,cmp);
			if(strcmp(str,ans) == 0) {
				flag = true;
				printf("%d / %05d = %d\n",p,i,n);
			}
		}
		if(!flag) printf("There are no solutions for %d.\n",n);
	}
	return 0;
}

UVa 11059 Maximum Product

【题目大意】
   找一个连续的子序列,使得其乘积最大,全小于0,则输出0

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 should consider 0 as the value of the maximum product.
Input
Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each element Si
is
an integer such that −10 ≤ Si ≤ 10. Next line will have N integers, representing the value of each
element in the sequence. There is a blank line after each test case. The input is terminated by end of
file (EOF).
Output
For each test case you must print the message: ‘Case #M: The maximum product is P.’, where
M is the number of the test case, starting from 1, and P is the value of the maximum product. After
each test case you must print a blank line.
Sample Input
3
2 4 -3
5
2 5 -1 2 -1
Sample Output
Case #1: The maximum product is 8.
Case #2: The maximum product is 20.

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define Maxn 101
using namespace std;
int f[Maxn],num[Maxn];
int main(int argc,char* agrv[]) {
	int kase = 0,n,x; long long Ans = 1,p;
	while(scanf("%d",&n) != EOF) {
		Ans = 0;
		for(int i=1; i<=n; i++) scanf("%d",&num[i]);
		for(int i=1; i<=n; i++) {//序列长度 
			for(int j=1; j+i<=n+1; j++) {// 序列起点 
				p = 1;
				for(int k=0; k<i; k++) p *= num[j + k];
				Ans = max(Ans,p);
			}
		}
		
		printf("Case #%d: The maximum product is %lld.\n\n",++kase,Ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35776409/article/details/104474540
今日推荐