您的位置:首页 > 编程语言 > Java开发

Java的一些简单示例(1)

2016-04-27 21:12 417 查看

1、求两个正整数的最大公约数和最小公倍数

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);
System.out.println("please input an integer!");
int max = s.nextInt();
int sml = s.nextInt();
int temp1,temp2;
temp1 = max;
temp2 = sml;
int temp;
int f;
while(temp2 != 0){
temp = temp1%temp2;
temp1 = temp2;
temp2 = temp;
}
System.out.println("最大公约数是:"+ temp1);
f = (max * sml)/temp1;
System.out.println("最小公倍数是:"+ f);
}
}


2、求100到200之间的所有素数

public class Test {

public static void main(String[] args) {

for(int i=101;i<=200;i++){
boolean b = false;
for(int k=2;k<=Math.sqrt(i);k++){
if(i%k == 0){
b = true;
break;
}
}
if(b == false){
System.out.println(i);
}
}
}
}


3、分解质因子

import java.util.Scanner;

class Test{

public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("please input an integer!");
int num = s.nextInt();
int k = 2;

while(k <= num){
if(num == k){
System.out.print(k);
break;
}else if(num % k == 0){
System.out.print(k+"*");
num = num/k;
}else{
k++;
}
}
}
}


4、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

import java.util.Scanner;

public class Test {

public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("please input a string!");
String ss = s.nextLine();
char c[] = null;
int digital = 0;
int character = 0;
int black = 0;
int other = 0;
c = ss.toCharArray();
for(int i=0;i<c.length;i++){
if(c[i]<='9' && c[i]>='0'){
digital++;
}else if(c[i]<='z' && c[i]>='a' || c[i]<='Z' && c[i]>='A'){
character++;
}else if(c[i] == ' '){
black++;
}else{
other++;
}
}
System.out.println("digital:"+digital);
System.out.println("character:"+character);
System.out.println("black:"+black);
System.out.println("other:"+other);
}
}


5、(1)杨辉三角

public class Test {

public static void main(String args[]){

int array[][] = new int[10][10];
for(int i=0;i<10;i++){
for(int j=0;j<=i;j++){
if(j==0 || j==i){
array[i][j] = 1;
}else{
array[i][j] = array[i-1][j-1] + array[i-1][j];
}
}
}
for(int i=0;i<10;i++){
for(int k=0;k<10-i;k++){
System.out.print("  ");
}
for(int j=0;j<=i;j++){
System.out.print(array[i][j]+"   ");
}
System.out.println();
}
}
}


(2)用c语言实现杨辉三角

#include <stdio.h>

int main(){
int array[10][10] = {0};
for(int i=0;i<10;i++){
array[i][0] = 1;
}
for(int k=1;k<10;k++){
for(int j=1;j<=k;j++){
array[k][j] = array[k-1][j-1] + array[k-1][j];
}
}
for(int m=0;m<10;m++){
for(int k=0;k<(10-m);k++){
printf("  "
d11a
);
}
for(int n=0;n<=m;n++){
printf("%5d",array[m]
);
}
printf("\n");
}
return 0;
}


(3)用c语言实现杨辉三角

#include <stdio.h>

int main(){
int array[10][10];
for(int i=0;i<10;i++){
for(int j=0;j<=i;j++){
if(j == 0 || j == i){
array[i][j] = 1;
}else{
array[i][j] = array[i-1][j-1] + array[i-1][j];
}
}
}

for(int m=0;m<10;m++){
for(int k=0;k<(10-m);k++){
printf("  ");
}
for(int n=0;n<=m;n++){
printf("%5d",array[m]
);
}
printf("\n");
}
return 0;
}


6、找出1000以内的所有完数

class Test{

public static void main(String args[]){
for(int i=1;i<1000;i++){
int sum = 0;
for(int j=1;j<=i/2;j++){
if(i%j == 0){
sum = sum + j;
}
}
if(sum == i){
System.out.println(i+"  ");
}
}
}
}


7、一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

public class Test {

public static void main(String[] args) {

double sum = 100;
double height = 100;
for(int i=1;i<=9;i++){
height = height/2;
sum = sum + 2*height;
}
System.out.println(height/2);
System.out.println(sum);
}
}


8、输入某年某月某日,判断这一天是这一年的第几天?

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int year;
int month;
int day;
int days = 0;
int sum = 0;
Scanner s = new Scanner(System.in);
System.out.println("请输入年!");
year = s.nextInt();
System.out.println("请输入月!");
month = s.nextInt();
System.out.println("请输入日!");
day = s.nextInt();

if(year<=0 || month<=0 || month>12 || day<=0 || day>31){
System.out.println("error!");
System.exit(-1);
}

for(int i=1;i<month;i++){
if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
days = 31;
}
if(i == 4 || i == 6 || i == 9){
days = 30;
}
if(i == 2 && ((year%400 == 0) || (year%100 != 0 && year%4 == 0))){
days = 29;
}
if(i == 2 && !((year%400 == 0) || (year%100 != 0 && year%4 == 0))){
days = 28;
}
sum = sum + days;
}
sum = sum + day;
System.out.println(year + "-" + month + "-" + day + "是这年的第" + (sum) + "天。");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: