public static boolean judge(int n) { int i; for (i = 2; i < n / 2; i++) { if (n % i == 0) { return false; } } return true; }解析
时间: 2024-02-14 19:32:50 浏览: 121
这段代码是一个判断一个数是否为质数的函数,函数名为judge,参数为一个整数n。具体解析如下:
1. 首先定义了一个变量 i,用于循环判断n是否能被2至n/2之间的整数整除。
2. 然后进入for循环,i从2开始循环,直到n/2,每次循环判断n是否能被i整除。
3. 如果n能被i整除,则说明n不是质数,直接返回false。
4. 如果for循环结束后仍然没有找到n的因子,则说明n是质数,返回true。
需要注意的是,这个函数的判断范围是2至n/2之间的整数,可能会存在一些较大的质数无法被正确判断。同时,这个函数只适用于正整数,对于负整数和0的判断结果不准确。
相关问题
public class PrimeNumber { public static void main(String[] args) { // StringBuffer numbers = new StringBuffer(); StringBuffer numbers = new StringBuffer(); int k = 0; for (int n = 100; n <= 200; n++) { boolean res = judge(n); if (res) { if (k == 0) { numbers.append(n); } else { numbers.append(" ").append(n); } k = 1; } } System.out.println(numbers); } public static boolean judge(int n) { int i; for (i = 2; i < n / 2; i++) { if (n % i == 0) { return false; } } return true; } }
这段代码是用 Java 语言编写的,目的是输出 100 到 200 之间的所有质数。在代码中,首先定义了一个 StringBuffer 对象 numbers,用于存放所有的质数。然后通过一个 for 循环,从 100 循环到 200,对于每个数都调用了一个名为 judge 的方法来判断其是否为质数。如果是质数,则将其添加到 StringBuffer 对象中。最后输出 StringBuffer 对象即可得到所有质数。需要注意的是,在判断一个数是否为质数时,代码采用了一种常见的方法,即从 2 到该数的一半之间的数逐个判断是否为该数的因数,如果存在因数,则该数不是质数,否则该数是质数。
将一下java代码转成c语言:import java.util.; public class Main { public static boolean judge(char c1,char c2) { char ch[] = {'#','+','-','','/','%','(',')'}; int num1 = 0,num2 = 0; for(int i = 0;i <= 7;i ++) { if(ch[i] == c1) num1 = i; if(ch[i] == c2) num2 = i; } if(num1 > num2) return true; return false; } public static int calculation(int t1,int t2,char c) { if(c == '+') return t2 + t1; else if(c == '-') return t2 - t1; else if(c == '*') return t2 * t1; else if(c == '%') return t2 % t1; else return t2 / t1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); Stack<Integer> stk_int = new Stack<>(); Stack<Character> stk_str = new Stack<>(); int index = 0; stk_str.push('#'); for(int i = 0;i < s.length();i ++) { if(s.charAt(i) < '0' || s.charAt(i) > '9') { if(index != i) stk_int.push(Integer.parseInt(s.substring(index,i))); index = i+1; boolean flag = false; while(flag == false) { if (judge(s.charAt(i),stk_str.peek())) { stk_str.push(s.charAt(i)); if(stk_str.peek() == '(' ) stk_str.push('#'); if( stk_str.peek() == ')') { stk_str.pop(); while(stk_str.peek()!='#') { stk_int.push(calculation(stk_int.pop(),stk_int.pop(),stk_str.pop())); } stk_str.pop(); stk_str.pop(); } flag = true; } else { stk_int.push(calculation(stk_int.pop(),stk_int.pop(),stk_str.pop())); } } } if(index != s.length() && i == s.length()-1) stk_int.push(Integer.parseInt(s.substring(index,s.length()))); } while(!stk_int.isEmpty() && stk_str.peek()!= '#') { stk_int.push(calculation(stk_int.pop(),stk_int.pop(),stk_str.pop())); } System.out.println(stk_int.pop()); } }
以下是将 Java 代码转换为 C 语言代码的结果:
```c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
bool judge(char c1, char c2) {
char ch[] = {'#', '+', '-', '*', '/', '%', '(', ')'};
int num1 = 0, num2 = 0;
for (int i = 0; i <= 7; i++) {
if (ch[i] == c1) num1 = i;
if (ch[i] == c2) num2 = i;
}
if (num1 > num2) return true;
return false;
}
int calculation(int t1, int t2, char c) {
if (c == '+') return t2 + t1;
else if (c == '-') return t2 - t1;
else if (c == '*') return t2 * t1;
else if (c == '%') return t2 % t1;
else return t2 / t1;
}
int main() {
char s[1000];
fgets(s, 1000, stdin);
s[strlen(s) - 1] = '\0';
int index = 0;
int stk_int[1000], top_int = -1;
char stk_str[1000], top_str = '#';
for (int i = 0; i < strlen(s); i++) {
if (s[i] < '0' || s[i] > '9') {
if (index != i) {
stk_int[++top_int] = atoi(&s[index]);
}
index = i + 1;
bool flag = false;
while (flag == false) {
if (judge(s[i], top_str)) {
stk_str[++top_str] = s[i];
if (top_str == '(') stk_str[++top_str] = '#';
if (top_str == ')') {
top_str--;
while (top_str != '#' && top_str != '(') {
stk_int[top_int - 1] = calculation(stk_int[top_int], stk_int[top_int - 1], stk_str[top_str--]);
top_int--;
}
top_str--;
}
flag = true;
} else {
stk_int[top_int - 1] = calculation(stk_int[top_int], stk_int[top_int - 1], stk_str[top_str--]);
top_int--;
}
}
}
if (index != strlen(s) && i == strlen(s) - 1) {
stk_int[++top_int] = atoi(&s[index]);
}
}
while (top_int >= 0 && top_str != '#') {
stk_int[top_int - 1] = calculation(stk_int[top_int], stk_int[top_int - 1], stk_str[top_str--]);
top_int--;
}
printf("%d\n", stk_int[top_int--]);
return 0;
}
```
需要注意的是,Java 代码中使用了 Scanner 类来接收用户的输入,在 C 语言中需要使用 fgets 函数。此外,Java 中的 Stack 类可以使用数组来实现栈的功能。需要注意的是,C 语言中数组下标从 0 开始计数,而 Java 中数组下标从 1 开始计数,因此在转换过程中需要进行相应的调整。
阅读全文