- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题来自在线竞赛网站codechef。它需要计算素数。问题是:
Farmer Feb has three fields with potatoes planted in them. He harvested x potatoes from the first field, y potatoes from the second field and is yet to harvest potatoes from the third field. Feb is very superstitious and believes that if the sum of potatoes he harvests from the three fields is a prime number , he'll make a huge profit. Please help him by calculating for him the minimum number of potatoes that if harvested from the third field will make the sum of potatoes prime. At least one potato should be harvested from the third field.
Input
The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines contain 2 integers separated by single space: x and y.Output
For each test case, output a single line containing the answer.Constraints
1 ≤ T ≤ 1000
1 ≤ x ≤ 1000
1 ≤ y ≤ 1000Example
Input:
2
1 3
4 3Output:
1
4Explanation
In example case 1: the farmer harvested a potato from the first field and 3 potatoes from the second field. The sum is 4. If he is able to harvest a potato from the third field, that will make the sum 5, which is prime. Hence the answer is 1 (he needs one more potato to make the sum of harvested potatoes prime).
我是这样解决的:
import java.io.*;
import java.util.StringTokenizer;
class java2s {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
while(T-->0) {
StringTokenizer st=new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int sum = x + y;
for(int i=1; i<100; i++) {
int res=sum+i;
if(res==3 || res==5 || res==7) {
System.out.println(i);
break;
}
else if((res%2)!=0 && (res%3)!=0 && (res%5)!=0 && (res%7)!=0) {
System.out.println(i);
break;
}
}
}
}
}
当我在我的电脑上运行该代码时,它运行得很好,但是当我将其提交到竞赛网站时,评分者指出我的答案不正确。
你能告诉我我遗漏了哪些极端情况吗?
最佳答案
首先,输入的显示方式几乎不会产生误导。我想输入应该看起来像
输入:
2
1 3
4 3
输出应该类似于
1
4
您提交的代码正在检查极少量的案例。
回答以下问题应该可以帮助您阐明问题
为什么你只将变量结果与 3,5,7 进行比较,结果也可以是 101,这是一个素数。
要检查结果是否为素数,为什么只将结果除以 2,3,5,7?例如:169 不是素数,不能被 2、3、5、7 中的任何一个整除。
二月份最多可以收获多少土 bean (在所有三个田地中)?
如何检查一个数是否为素数。
关于java - codechef farmer feb.Code :POTATOES 的角落案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31685482/
这个问题来自在线竞赛网站codechef。它需要计算素数。问题是: Farmer Feb has three fields with potatoes planted in them. He harv
代码如下: string msg; niceArray = txtReader("chatTest/replaces.txt"); vector>vMaster;
您好,我遇到了常见错误“由不同的对象管理器管理” 我在网上查了一下,没有找到适合我的问题的解决方案。我从 JSP 页面调用以下代码 PersistenceManager pm = PMF.get().
我是一名优秀的程序员,十分优秀!