[转帖]JAVA经典算法几例(2)_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 12308 | 回复: 0   主题: [转帖]JAVA经典算法几例(2)        下一篇 
    本主题由 yuyanzheng 于 2013-2-1 16:47:12 版块置顶
liulin_2
注册用户
等级:少校
经验:1143
发帖:85
精华:5
注册:2012-12-10
状态:离线
发送短消息息给liulin_2 加好友    发送短消息息给liulin_2 发消息
发表于: IP:您无权察看 2012-12-14 9:28:31 | [全部帖] [楼主帖] 楼主

【程序 6】 题目:输入两个正整数mn,求其最大公约数和最小公倍数。 
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=123.编程 找出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)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论