close

 

---

import java.util.Scanner;

public class ClassTest {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int keyin = scanner.nextInt();
int flag; //質數
for (int i = 2; i <= keyin; i++) { // 判斷 i 是否質數
flag = 1; // 預設i為質數
for (int j = 2; j < i; j++) {
if (i % j == 0) {flag = 0;} // i÷j餘0則不是質數
}
if (flag == 1){System.out.print(i+", ");}
}

}
}

---

 

import java.util.Scanner;

public class ClassTest {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int keyin = scanner.nextInt();
int flag,a[]= new int [1000],count = -1 ;
//質數沒有因數
//EX:keyin = 74,要找出2-74之間為質數的數字
//EX:i=7, 2-6之間沒有一個數能整除7,所以7為質數
//EX:i=14, 2-13之間有一個數能整除14,所以14不為質數
for (int i = 2; i <= keyin; i++) { // 判斷 i 是否質數
flag = 1; // 預設i為質數
for (int j = 2; j < i; j++) { //判斷i有沒有因數
if (i % j == 0) {flag = 0;} // i÷j餘0則不是質數
}
if (flag == 1){
count++;
a[count]= i;
}
}
for(int n=0;n<=count;n++){
System.out.print(a[n]+", ");
}

}
}

--Console

72
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,

 

---------------------

import java.util.Scanner;
public class ClassTest {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int keyin = scanner.nextInt();
int flag,a[]= new int [1000],count = -1 ;
// 質數沒有因數
// EX:keyin = 74,要找出2-74之間為質數的數字
// EX:i=7, 2-6之間沒有一個數能整除7,所以7為質數
// EX:i=14, 2-13之間有一個數能整除14,所以14不為質數
for (int i = 2; i <= keyin; i++) { // 判斷 i 是否質數
flag = 1; // 預設i為質數
for (int j = 2; j < i; j++) { // 判斷i有沒有因數
if (i % j == 0) {flag = 0;} // i÷j餘0則不是質數
}
if (flag == 1){
count++;
a[count]= i;
}
}
for(int n=0;n<=count;n++)System.out.print(a[n]+", ");
int num=0 , target=12;
for(int n=0;n<=count;n++){
num=0;
while (true) //計算質因數的個數
{ if(target %a[n]==0 ) //n為72的因數
{target =target/a[n];
num++; //=num+1
}else
break;//不是因數就跳出迴圈
}
if (num>=1) System.out.print(a[n]+"("+num+")");
}

}
}

--Console

72
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 2(2)3(1)

-

12
2, 3, 5, 7, 11, 2(2)3(1)

-------------------

 

import java.util.Scanner;
public class ClassTest {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int keyin = scanner.nextInt();
int flag,a[]= new int [1000],count = -1 ;
// 質數沒有因數
// EX:keyin = 74,要找出2-74之間為質數的數字
// EX:i=7, 2-6之間沒有一個數能整除7,所以7為質數
// EX:i=14, 2-13之間有一個數能整除14,所以14不為質數
for (int i = 2; i <= keyin; i++) { // 判斷 i 是否質數
flag = 1; // 預設i為質數
for (int j = 2; j < i; j++) { // 判斷i有沒有因數
if (i % j == 0) {flag = 0;} // i÷j餘0則不是質數
}
if (flag == 1){
count++;
a[count]= i;
}
}
for(int n=0;n<=count;n++)System.out.print(a[n]+", ");
int num=0 , target=12;
for(int n=0;n<=count;n++){
num=0;
while (true) //計算質因數的個數
//EX:a[0]=2, 72%2==0, 2為72的因數,72÷2=36
//num=1, 36÷2=18, num=3 所以印出2(3)
//n=1,a[1]=3, 9÷3=3,num=1 ,3÷3=1,num=3,所以印出3(2)
{ if(target %a[n]==0 ) //n為72的因數
{target =target/a[n];
num++; //=num+1
}else
break;//不是因數就跳出迴圈
}
if (num>=1) System.out.print(a[n]+"("+num+")");
}

}
}

----------------

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Scanner;

public class t0605 {
public static void main(String[] args) throws Exception {
String str1, str2;
BufferedReader bb = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n 請輸入數字:");
str1 = bb.readLine();
if (str1.length() > 10) System.out.print(" 輸入錯誤");
else {
int sum = 0, count = 1;
//EX: str1 = 8457268802
//4<8 count=1,sum=1
//5>4 count=2
//7>5 count=3
//2<7 sum=3 count=1
//6>2 count=2
//8>6 count=3
//8=8 count=4
for (int i = 0; i < str1.length()-1; i++) {
if (str1.charAt(i + 1) >= str1.charAt(i)) // 非遞減為後項>=前項
{ count++; }
else{
if(count>sum ) sum = count; //非遞減次數count>sum時
count = 1 ; //非遞減重新計算
}
}
System.out.println(sum);
}
}
}

--Console


請輸入數字:8457268802
4

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()