算法小集合

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;
   }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值