演算法小集合

來源:互聯網
上載者:User

1. 下列乘法算式中:每個漢字代表1個數字(1~9)。相同的漢字代表相同的數字,不同的漢字代表不同的數字。
     賽軟體 * 比賽 = 軟體比拼
  試編程確定使得整個算式成立的數字組合。

 

其中的一種解法:
package cyt.project.init;

public class gg {

 /**
  * @param args
  */
 public static void main(String[] args) {
 
     int num1 = 0;
     int num2 = 0;
     int num3 = 0;
    
     boolean isRight = false;
    
     for (int n1 = 1; n1 <= 9; n1++) {
     
      for (int n2 = 1; n2 <= 9; n2++) {
       if(n2 == n1)
       {
        continue;
       }
      
       for (int n3 = 1; n3 <=9; n3++) {
        if(n3 == n1 || n3 == n2)
        {
         continue;
        }
        num1 = n1 * 100+ n2 * 10 +  n3;
       
        for (int n4 = 1; n4 <= 9; n4++) {
         if(n4 == n1 || n4==n2 || n4==n3)
         {
          continue;
         }
         num2 = n4 * 10 + n1;
        
         for (int n5 = 1; n5 <= 9; n5++) {                                //這個地方如果是n5 = 1  整個成語就誤解    如果是n5 = 0  只有一組解
          if(n5==n1 || n5==n2 || n5==n3 || n5==n4)
          {
           continue;
          }
          num3 = n2*1000 + n3*100 + n4*10 + n5;
          if(num1 * num2 == num3)
          {
           isRight = true;
           System.out.println(num1 + " * " + num2 + " = " + num3);
          }
         
         }
        
        }
       
       }
      
      }
     
     }
    
    if(!isRight){
     System.out.println("無解");
    }

 

 }

}

2.把十進位轉換成任意進位

   /**
  * 把十進位轉換成任意進位
  *
  */
 public void testrr(){  
  while(true){
  Scanner sc = new Scanner(System.in);
  System.out.print("請輸入任意一個十進位數:");
  int shuru = sc.nextInt();
  int shuru2 = shuru;
  System.out.print("請輸入任意一種進位:");
  int jinzhi = sc.nextInt();
  int s = shuru;
  int i = 0;
  int l = 0;
  while(s !=0){
   int h = s%jinzhi;
   l++;   
   s = s/jinzhi; 
  }  
  int[] hehe = new int[l];
  while(shuru !=0){   
   hehe[i++] = shuru%jinzhi;   
   shuru = shuru/jinzhi;  
  }
  StringBuffer ss = new StringBuffer();
  for(int j = hehe.length - 1;j >= 0; j--){
   ss.append(hehe[j]);   
  }
  System.out.println("十進位 "+shuru2+" 的"+jinzhi+"進位是:  "+ ss.toString());
  
  
  }  
 }

 

 

 

2.把任意進位轉換成十進位

 

/**
  * 把任意進位轉換成十進位
  *
  */
 public void testhuhu(){  
  while(true){  
  Scanner sc = new Scanner(System.in);
  System.out.print("請輸入任意一種進位:");
  int jinzu = sc.nextInt();
  System.out.print("請輸入任意一種進位的數(要符合你上面輸入的進位格式):");
  String shuru = sc.next();  
  char[] shurushuzu = shuru.toCharArray();  
  int zhengshu=0;  
  for(int i = shurushuzu.length - 1; i >= 0; i--){
   int h = 1;
   for(int j = 0;j <= i;j++){
    if(j == 0){
     h=1;
     }else{   
        h*=jinzu;
    }    
   }   
   /*注意這個地方: shurushuzu.length - 1 - i  是倒過來的*/
   zhengshu+=Integer.valueOf(String.valueOf(shurushuzu[shurushuzu.length - 1 - i]))*h;    
  }
  System.out.println(jinzu+"進位  "+shuru+"  的十進位是"+zhengshu);   
 }
 }
 

 

4.給定一個日期  算出這個日期是這全年中的第幾天

 

/**
  * 給定一個日期  算出這個日期是這全年中的第幾天
  *
  */
 public void testdd(){
 while(true){
  Scanner in = new Scanner(System.in);
  System.out.println("請依次輸入年、月、日:");
  int year = in.nextInt();
  int month = in.nextInt();
  int day = in.nextInt();
  int days = 0;
  boolean isLeap = isLeap(year);
  for (int i = 1; i < month; i++) {
   days += getDays(i, isLeap);
   }
  System.out.println("這是今年的第 " + (days + day) + " 天 ");
  }
 }

 

 

/**
  * 根據月份獲得這個月的天數
  * @param month
  * @param isLeap
  * @return
  */
 public static int getDays(int month, boolean isLeap) {
    int days = 0;
    switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
     days = 31;
     break;
    case 4:
    case 6:
    case 9:
    case 11:
     days = 30;
     break;
    case 2:
     if (isLeap)
      days = 29;
     else
      days = 28;
     break;
    }

    return days;
   }


 /**
  * 判斷是否是閏年條件(能被4整出並且不能被100整數)(能被400整除)
  * @param year
  * @return
  */
  public boolean isLeap(int year) {
    if (year % 4 == 0 && year % 100 != 0) {
     return true;
    }
    if (year % 400 == 0) {
     return true;
    }
    return false;
   }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.