Ekka Dokka -- 乘积奇数最大偶数最小
2017-10-07 15:31
190 查看
Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the
cake such that Ekka gets a share of N square centimeters and Dokka gets a share of M square centimeters where N is odd and M is even. Both N and M are positive
integers.
They want to divide the cake such that N * M = W, where W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer W (2 ≤ W < 263). And W will not be a power of 2.
Output
For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print N and M. If there are multiple solutions, then
print the result where M is as small as possible.
Sample Input
3
10
5
12
Sample Output
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4
分析:题目求解的是如果一个数可以被写成一个人奇数和偶数的乘积,并且偶数要求是最小的,如果存在则输出否则输出不可能
import java.util.*;
public class Main{
static Scanner in=new Scanner(System.in);
public static void main(String args[]){
int k=in.nextInt();
int ca = 0;
while(k-->0){
ca++;
long w = in.nextLong();
if(w%2!=0){
System.out.println("Case "+ca+": Impossible");
}
else{
long odd = 1,even;
for(even=2;even <= w;even+=2)
if(w%even==0){
odd=w/even;
if(odd%2!=0)
break;
}
System.out.println("Case "+ca+": "+odd+" "+even);
}
}
}
}
既然偶数要求最小,不如直接从最小偶数开始,这样才会不超时,即使for(odd=w-1,odd<=w;odd-=2)也会超时的。。
奇数一定不可以,一个偶数可以写成一奇一偶或者两偶,而一个奇数只能写成两个奇数的乘积
cake such that Ekka gets a share of N square centimeters and Dokka gets a share of M square centimeters where N is odd and M is even. Both N and M are positive
integers.
They want to divide the cake such that N * M = W, where W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer W (2 ≤ W < 263). And W will not be a power of 2.
Output
For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print N and M. If there are multiple solutions, then
print the result where M is as small as possible.
Sample Input
3
10
5
12
Sample Output
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4
分析:题目求解的是如果一个数可以被写成一个人奇数和偶数的乘积,并且偶数要求是最小的,如果存在则输出否则输出不可能
import java.util.*;
public class Main{
static Scanner in=new Scanner(System.in);
public static void main(String args[]){
int k=in.nextInt();
int ca = 0;
while(k-->0){
ca++;
long w = in.nextLong();
if(w%2!=0){
System.out.println("Case "+ca+": Impossible");
}
else{
long odd = 1,even;
for(even=2;even <= w;even+=2)
if(w%even==0){
odd=w/even;
if(odd%2!=0)
break;
}
System.out.println("Case "+ca+": "+odd+" "+even);
}
}
}
}
既然偶数要求最小,不如直接从最小偶数开始,这样才会不超时,即使for(odd=w-1,odd<=w;odd-=2)也会超时的。。
奇数一定不可以,一个偶数可以写成一奇一偶或者两偶,而一个奇数只能写成两个奇数的乘积
相关文章推荐
- (6)最大奇数与最小偶数之差的绝对值
- OpenJudge计算概论-最大奇数与最小偶数之差的绝对值
- 最大奇数与最小偶数之差的绝对值
- 程序员面试题目总结--数组(四)【数列中符合条件数对的个数、数组是否存在重复元素、重新排列数组使数组左边为奇数,右边为偶数、数组中的第二大数、数组中的最小值和最大值】
- 最大奇数与最小偶数之差的绝对值
- js写一个方法,求一个数组中的最大奇数和最小偶数,并求他们的和,如果一个数不存在则返回null。
- if 求最小值、判断键盘录入的数是奇数还是偶数、输出2个数中的最大值
- 连续子数组的最大乘积、最小乘积
- 奇数变成最大的偶数
- uva 1619 单调 求一段序列和 和 其中最小值的乘积的最大值
- 求最小子序列和,最小的正子序列和,最大子序列乘积问题
- 华为:给定一个数组input[],如果数组长度n为奇数,则将数组中最大的元素放到 output[]数组最中间的位置,如果数组长度n为偶数。。 .
- 求最小子序列和 求最小的正序列 求最大子序列乘积
- 给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组 最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上
- 两个数的乘积等于其最大公约数与最小公倍数的乘积,怎么证明?
- 华为:给定一个数组input[],如果数组长度n为奇数,则将数组中最大的元素放到 output[]数组最中间的位置,如果数组长度n为偶数。。
- 1.将数组A中的内容和数组B中的内容进行交换。(数组一样大) 2.获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。 3.将三个数按从大到小输出。 4.求两个数的最大公约数。
- 在圆圈中,把相隔一个数据的两个数 称作一对数,编写算法求出乘积最大的一对数和乘积最小的一对数
- [ACM] POJ 2796 Feel Good (求序列中子序列的和与子序列中的最小数最大乘积)
- 三人年龄 三个神秘蒙面人来访F博士。博士询问他们年龄时,他们说:我们中年龄最小的不超过19岁。我们3人年龄总和为70岁。且我们三人年龄的乘积是所有可能情况中最大的。