A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 1519218880 中级黑马   /  2016-3-23 23:17  /  414 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

【程序6】  题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

1.程序分析:利用辗除法。

最大公约数: public class CommonDivisor{ public static void main(String args[]) { commonDivisor(24,32); } static int commonDivisor(int M, int N) { if(N<0||M<0) { System.out.println("ERROR!"); return -1; } if(N==0) { System.out.println("the biggest common divisor is :"+M); return M; } return commonDivisor(N,M%N); } } 最小公倍数和最大公约数: import java.util.Scanner; public class CandC { //下面的方法是求出最大公约数 public static int gcd(int m, int n) { while (true) { if ((m = m % n) == 0) return n; if ((n = n % m) == 0) return m; } } public static void main(String args[]) throws Exception { //取得输入值 //Scanner chin = new Scanner(System.in); //int a = chin.nextInt(), b = chin.nextInt(); int a=23; int b=32; int c = gcd(a, b); System.out.println("最小公倍数:" + a * b / c + "\n最大公约数:" + c); } }

【程序7】  题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

1.程序分析:利用while语句,条件为输入的字符不为 ‘\n ‘.

import java.util.Scanner; public class ex7 { public static void main(String args[]) { System.out.println("请输入字符串:"); Scanner scan=new Scanner(System.in); String str=scan.next(); String E1="[\u4e00-\u9fa5]"; String E2="[a-zA-Z]"; int countH=0; int countE=0; char[] arrChar=str.toCharArray(); String[] arrStr=new String[arrChar.length]; for (int i=0;i<arrChar.length ;i++ ) { arrStr[i]=String.valueOf(arrChar[i]); } for (String i: arrStr ) { if (i.matches(E1)) { countH++; } if (i.matches(E2)) { countE++; } } System.out.println("汉字的个数"+countH); System.out.println("字母的个数"+countE); } }

【程序8】  题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

1.程序分析:关键是计算出每一项的值。

import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; String output=""; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入a的值"); String input =stadin.readLine(); for(int i =1;i<=Integer.parseInt(input);i++) { output+=input; int a=Integer.parseInt(output); s+=a; } System.out.println(s); } } 另解: import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; int n; int t=0; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); String input = stadin.readLine(); n=Integer.parseInt(input); for(int i=1;i<=n;i++){ t=t*10+n; s=s+t; System.out.println(t); } System.out.println(s); } }

【程序9】  题目:一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3.编程   找出1000以内的所有完数。

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

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

public class Ex10 {
public static void main(String[] args)
{
         double s=0;
         double t=100;
         for(int i=1;i<=10;i++)
         {
         s+=t;
         t=t/2;
         }
         System.out.println(s);
         System.out.println(t);
         
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马