求三位数的质数

没做出来啊,原来有这么多方法啊。

首先,我连质数是什么都不知道!

质数:只能被本身和1整除的数

帖子里回复了不少方法:

class Zhishu 
{
        public static void main(String[] args) 
        {
                int count=0;
                for(int i=1;i<=100;i++)
                {
                        count=0;
                        for(int j=1;j<=i;j++)
                        {
                                if(i%j==0)
                                {
                                        count++;
                                }
                        }
                        if(count==2||count==1)
                        {
                                System.out.print(i+" ");
                        }

                }
        }
}
package com.ms.test;

public class GetSushu {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                for(int x=100;x<=999;x++){
                        boolean flag=true;
                        for(int y=2;y<x;y++){
                                if(x%y==0){
                                        flag=false;
                                        break;
                                }
                        }
                        if(flag){
                                System.out.println(x);
                        }
                        
                }
        }

}

判断某一个三位数是否是质数

import java.util.Scanner;

public class PrimeNum {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个三位数");
                int  num = sc.nextInt();
                if(num<100 || num>999)
                        throw new RuntimeException("输入数据不合要求");
                
                getPrime(num);
        }
        public static void getPrime(int num){
                
                for(int x=2;x<num;x++){
                        if(num%x!=0){
                                if(x==num-1){
                                        System.out.println(num+"是质数");
                                }
                                continue;
                        }
                        else{
                                System.out.println(num+"不是质数");
                                break;
                        }                                
                }                
        }
}

列出所有介于100到你输入的三位数中的质数(不包含100)

import java.util.Scanner;

public class TotalPrimeNum {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个三位数");
                int  num = sc.nextInt();
                if(num<100 || num>999)
                        throw new RuntimeException("输入数据不合要求");
                getPrime(num);
        }
        public static void getPrime(int num){
                int count = 0;//计数器记录质数个数
                W:for (int x = 101; x <= num; x++) {
                        Q:for (int y = 2; y < x; y++) {
                                if(x%y!=0){
                                        if(y==x-1){
                                                System.out.print(x+"\t");
                                                count++;
                                                continue W;
                                        }
                                        continue Q;
                                }
                                else{
                                        continue W;
                                }
                        }
                }
                System.out.println();
                System.out.println("101到"+num+"共有质数个数为:"+count);        
        }
}
时间: 2024-08-03 10:09:57

求三位数的质数的相关文章

js求三位数的和

例如输入508就输出5+0+8的和13: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> //输入三位数求其和 //第一步

求三位数的逆序

例如    712 a     =    712/100=7 b     =    712%10=2 712%100=12 c     =    12/10=1 或 712/10=71 71%10=1 然后 b*100+c*10+a 为了避免700  被输出成007 所以不写成%d%d%d 原文地址:https://www.cnblogs.com/hahalovehenhen/p/12217311.html

华为机试 --- 求最大三位数

题目:输入10位0-9数字,取其中三位不同数字组合,求组合出来的最大三位数. 如输入 1 2 3 4 5 6 7 8 9 0,组合出来987最大. 测试代码如下: #include <stdio.h> #include <stdlib.h> int IsSame(int *a, int num); int main() { int i=0; int j=0; int a[10]={0}; int input =0; int length =0; int temp=0; for (i

(快速幂) 求A^B的最后三位数表示的整数

Description 求A^B的最后三位数表示的整数. 说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理. Output 对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行. Sample Input 2 3 12 6 6789 10000 0 0 Sample Output 8 984 1 快速幂求n^n; 1 int f1(i

1155.回文三位数

题目:如果一个数从左边读和从右边读都是同一个数,就称为回文数.例如6886就是一个回文数,求出所有的既是回文数又是素数的三位数. 先放代码: #include<iostream> #include<cstdio> using namespace std; bool isZhi(int a) { if(a==1)return false; for(int i=2;i<a;i++) { if(a%i==0)return false; } return true; } int ma

C语言之函数调用02—一个三位数的正整数=它的各位数字的阶乘之和

//函数调用+枚举法 /* ========================================================== 题目:求一个三位数的正整数=它的各位数字的阶乘之和! 如:145=1!+4!+5!. ========================================================== */ #include<stdio.h> int J(int n) { int t=1,i; for(i=1;i<=n;i++) t*=i;

zzuli OJ 1076: 三位数求解

Description 已知xyz+yzz=n,其中n是一个正整数,x.y.z都是数字(0-9),编写一个程序求出x.y.z分别代表什么数字.如果无解,则输出"No Answer" 注意:xyz和yzz表示一个三位数,而不是表示x*y*z和y*z*z. Input 输入一个正整数n. Output 输出一行,包含x.y.z的值,每个数值占4列. Sample Input 532 Sample Output 3 2 1 HINT Source #include <stdio.h&g

求一百以内的质数

这种类型题目在工作中几乎不可能遇到,但是非常考察shell编程功底 #!/bin/bash # 求100以内的质数 #declare -i i=1 i=1 while (( i<=100 )) do ret=1 for ((j=2;j<i;j++)) do # if (( i%j == 0));then if [ $(( i%j )) -eq 0 ];then ret=0 break fi done if [ $ret -eq 1 ];then echo "$i" fi #

C语言之基本算法29—整数任意次方的最后三位数(精度问题)

//精度问题! /* ================================================================== 题目: 求整数的任意次方最后三位数!347的72次方最后三位数是241. ================================================================== */ #include<stdio.h> #include<iomanip> main() { int x,y,p,n;