Small set of Algorithms

Source: Internet
Author: User

1. In the following multiplication formula: each Chinese Character represents 1 digit (1 ~ 9 ). The same Chinese Character represents the same number, and different Chinese characters represent different numbers.
Competition software * competition = software competition
Try programming to determine the number combination that makes the entire formula true.

 

One solution is as follows:
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 ++) {// If N5 = 1, the entire idiom is misunderstood. If N5 = 0, there is only one group of solutions.
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 ("unsolvable ");
}

 

}

}

2. Convert decimal to any base

/**
* Convert decimal to any base.
*
*/
Public void testrr (){
While (true ){
Pipeline SC = new pipeline (system. In );
System. Out. Print ("Please input any decimal number :");
Int shuru = SC. nextint ();
Int shuru2 = shuru;
System. Out. Print ("enter any hexadecimal format :");
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 ("decimal" + shuru2 + "" + Jinzhi + ":" + SS. tostring ());


}
}

 

 

 

2. convert any base to decimal

 

/**
* Convert any base to decimal
*
*/
Public void testhuhu (){
While (true ){
Pipeline SC = new pipeline (system. In );
System. Out. Print ("enter any hexadecimal format :");
Int jinzu = SC. nextint ();
System. Out. Print ("enter any number in hexadecimal format (to conform to the preceding hexadecimal format ):");
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;
}
}
/* Note: shurushuzu. Length-1-I is inverted */
Zhengshu + = integer. valueof (string. valueof (shurushuzu [shurushuzu. Length-1-I]) * h;
}
System. Out. println (the decimal value of jinzu + "hexadecimal" + shuru + "is" + zhengshu );
}
}
 

 

4. specify a date to calculate the day of the year.

 

/**
* A given date is the day of the year.
*
*/
Public void testdd (){
While (true ){
Running in = New Processing (system. In );
System. Out. println ("Enter year, month, and day in sequence :");
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 ("This is the first day of this year" + (days + day) + "day ");
}
}

 

 

/**
* Obtain the number of days of the month based on the month.
* @ 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;
}


/**
* Determine whether the condition is a leap year (can be rounded out by four integers and cannot be integer 100) (can be divisible by 400)
* @ Param year
* @ Return
*/
Public Boolean isleap (INT year ){
If (Year % 4 = 0 & amp; Year % 100! = 0 ){
Return true;
}
If (Year % 400 = 0 ){
Return true;
}
Return false;
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.