合肥做网站优化哪家好上海整站seo
11.判断101-200之间有多少个素数,并输出所有素数。【重点】
程序分析:判断素数的方法,用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
public class lianxi {
public static void main(String[] args) {
int count = 0;
for(int i=101; i<200; i+=2) {
boolean b = false;
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
b = false; break;
} else {
b = true;
}
}
if(b == true) {
count ++;
System.out.println(i );
}
}
System.out.println( "素数个数是: " + count);
}
}
12.水仙花数。【了解】
问题描述:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
public class lianxi {
public static void main(String[] args) {
int b1, b2, b3;
for(int m=101; m<1000; m++) {
b3 = m / 100;
b2 = m % 100 / 10;
b1 = m % 10;
if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {
System.out.println(m+"是一个水仙花数");
}
}
}
}
13.正整数分解质因数。【了解】
问题描述:将一个正整数分解质因数,例如:输入90,打印出 90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可;
如果n <> k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数,重复执行第一步;
如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print( "请键入一个正整数: ");
int n = s.nextInt();
int k=2;
System.out.print(n + "=" );
while(k <= n) {
if(k == n) {
System.out.println(n);
break;
} else if( n % k == 0) {
System.out.print(k + "*");
n = n / k;
} else {
k++;
}
}
14.最大公约数和最小公倍数。【了解】
程序分析:在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的 较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
import java.util.*;
public class lianxi {
public static void main(String[] args) {
int a ,b,m;
Scanner s = new Scanner(System.in);
System.out.print( "键入一个整数:");
a = s.nextInt();
System.out.print( "再键入一个整数: ");
b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
System.out.println("最大公约数: "+m);
System.out.println("最小公倍数: "+n);
}
}
class deff{
public int deff(int x, int y){
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
15.完数。【了解】
问题描述:一个数如果恰好等于它的因子之和,这个数就称为"完数",编程找出1000以内的所有完数,例如:6=1+2+3。
public class lianxi {
public static void main(String[] args) {
System.out.println("1到1000的完数有:");
for(int i=1; i<1000; i++) {
int t = 0;
for(int j=1; j<= i/2; j++) {
if(i % j == 0) {
t = t + j;
}
}
if(t == i) {
System.out.print(i + " ");
}
}
}
}
16.完全平方数。【了解】
问题描述:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
public class lianxi {
public static void main(String[] args) {
for(int x =1; x<100000; x++) {
if(Math.sqrt(x+100) % 1 == 0) {
if(Math.sqrt(x+268) % 1 == 0) {
System.out.println(x + "加100 是一个完全平方数,再加168 又是一个完全平方数");
}
}
}
}
}
17.猴子吃桃。【了解】
问题描述:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少?
public class lianxi {
public static void main(String[] args) {
int x = 1;
for(int i=2; i<=10; i++)
x = (x+1)*2;
System.out.println("猴子第一天摘了" + x + " 个桃子");
}
}
18.乒乓球比赛。【了解】
问题描述:两个乒乓球队进行比赛,各出三人。甲队为a、b、c三人,乙队为x、y、z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x、z比,请编程序找出三队赛手的名单。
public class lianxi {
static char[] m = { 'a', 'b', 'c' };
static char[] n = { 'x', 'y', 'z' };
public static void main(String[] args) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++){
if (m[i] == 'a' && n[j] == 'x'){
continue;
} else if (m[i] == 'a' && n[j] == 'y') {
continue;
} else if ((m[i] == 'c' && n[j] == 'x') || (m[i] == 'c' && n[j] == 'z')) {
continue;
} else if ((m[i] == 'b' && n[j] == 'z') || (m[i] == 'b' && n[j] == 'y')) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
19.求1+2!+3!+...+20!的和。【重点】
public class lianxi {
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for(int i=1; i<=20; i++) {
fac = fac * i;
sum += fac;
}
ystem.out.println(sum);
}
}
20.计算年龄。【了解】
问题描述:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
public class lianxi {
public static void main(String[] args) {
int age = 10;
for(int i=2; i<=5; i++) { age =age+2; }
System.out.println(age);
}
}