Java BASIC Programming 50 questions (7-9 questions) detailed

Source: Internet
Author: User
Tags repetition

First, describe

1, enter a line of characters, respectively, the English letters, spaces, numbers and other characters of the total number and the frequency of each character appears.
Program Analysis: Use the Matchs () of the string class to count the total number of each type of character that matches a regular expression, and then use the list and map collection classes to count the occurrences of each character respectively.

2, the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (a total of 5 numbers are added at this time), several numbers are added by keyboard control.

3, Title: If a number is exactly equal to the sum of its factors, this number is called the "end", that is, in addition to its own non-repetition factor and equal to itself. Programming to find all the finished numbers within M.
For example 6=1+2+3. The second complete number is 28, it has approximate 1, 2, 4, 7, 14, 28, except it itself 28, the remaining 5 numbers add, 1+2+4+7+14=28.
First, all factorization are calculated, and then the sum is determined and is equal to the original number.

Second, the source code

1. Procedure 1

Package Tong.yue.hong;


Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Scanner;
Import Java.util.Set;


/**
* Enter a line of characters to count the total number of letters, spaces, numbers, and other characters and how often each character appears.
Program Analysis: Use the Matchs () of the string class to count the total number of each type of character that conforms to the regular expression, and then use the list and map collection classes to count the frequency of each character occurrence, respectively.
* @author Tong
*
*/


public class Statistics {


public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
System.out.println ("Please enter a line string:");
String string = Scanner.nextline ();
Scanner.close ();
Count (string);
System.out.println ("-------------");
Counteveryonebylist (string);
System.out.println ("-------------");
Counteveryonebymap (string);
}


/**
* Regular expressions that define several types of characters, and use the Matchs () method of the string class to match the number of characters per class
* @param str
*/
private static void count (String str) {
Defines four regular expressions representing each type of character
String E1 = "[\u4e00-\u9fa5]"; Chinese characters
String E2 = "[A-za-z]"; Chinese and English letters
String E3 = "[0-9]";Digital
String E4 = "\\s";//Spaces
Five statistical variables are defined separately to count the total number of characters
int Countchinese = 0;
int countletter = 0;
int countnumber = 0;
int countspace = 0;
int countother = 0;

Convert a string to a character array
char[] Array_char = Str.tochararray ();
Because kanji can only be processed as strings, a string array is defined to hold all characters
string[] array_string = new String[array_char.length];
Convert a character array to an array of strings
for (int i=0;i<array_char.length;i++) {
Array_string[i] = string.valueof (Array_char[i]);
}
Iterate through the elements in a string array to count the total number of characters
for (String s:array_string) {
if (S.matches (E1))
countchinese++;
else if (s.matches (E2))
countletter++;
else if (s.matches (E3))
countnumber++;
else if (s.matches (E4))
countspace++;
Else
countother++;
}
System.out.println ("Number of Chinese characters entered:" +countchinese);
System.out.println ("Number of letters entered:" +countletter);
System.out.println ("Number of inputs:" +countnumber);
System.out.println ("Number of spaces entered:" +countspace);
System.out.println ("Number of other characters entered:" +countspace);
}



/**
* Count the number of individual characters using the list collection
* @param str
*/
private static void Counteveryonebylist (String str) {
list<string> list = new arraylist<string> ();
char[] Array_char = Str.tochararray ();
for (char C:array_char) {
To add a character as a string to the list table
List.add (String.valueof (c));
}
Call the Collections Collection Tool class to sort the data in the list class, with the same elements in the adjacent position
Collections.sort (list);//Sort
Iterates through the sorted collection and calculates the total number of the string based on the first occurrence and last occurrence of the same value in the list
for (String s:list) {
The index of the first occurrence of the string
int begin = List.indexof (s);
The index of the last occurrence of the string, as the same element is adjacent to the number
int end = List.lastindexof (s);
Index End Statistic character count
if (List.get (end) ==s) {
System.out.print ("character '" +s+ "' has" + (end-begin+1) + "one");
}
}
}

/**
* Use the Map collection class to count the number of occurrences of the string as a key, with the number of occurrences as the value, loop through the
* @param str
*/
private static void Counteveryonebymap (String str) {
Create a HashMap object to hold an array of strings and the number of occurrences
map<string,integer> HashMap = new hashmap<string,integer> ();
char[] Array_char = Str.tochararray ();

for (char C:array_char) {
String key = String.valueof (c);
If the string is already present, add 1 to the value of the string
if (Hashmap.containskey (key)) {
Integer value = Hashmap.get (key) +1;
Hashmap.put (key, value);
}else {
If the string appears for the first time, it adds the string and the number of occurrences to the HashMap
Hashmap.put (key, 1);
}

}
Traverse HashMap Collection, output result
set<map.entry<string, integer>> sets = Hashmap.entryset ();
For (map.entry<string, integer> mapentry:sets) {
System.out.println ("character '" +mapentry.getkey () + "' has" +mapentry.getvalue () + "one");
}

}
}


Operation Result:



2. Procedure 2

Package Tong.yue.hong;


Import Java.util.Scanner;


Import javax.xml.transform.Templates;


/**
* Title: Find the value of S=A+AA+AAA+AAAA+AA...A, where a is a number.
* For example, the number of times the keyboard controls the addition of 2+22+222+2222+22222, how many data are entered to add
* @author Tong
*
*/
public class Sumonenum {


public static void Main (string[] args) {

String E3 = "[0-9]";
Scanner Scanner = new Scanner (system.in);
System.out.println ("Please enter a number of 0-9:");
String num = Scanner.next ();
while (!num.matches (E3)) {
SYSTEM.OUT.PRINTLN ("You entered the wrong number, please re-enter:");
num = Scanner.next ();
}
System.out.println ("Please enter the number of data to add:");
String times = Scanner.next ();
while (!times.matches (E3)) {
SYSTEM.OUT.PRINTLN ("You entered the wrong number, please re-enter:");
Times = Scanner.next ();
}
Scanner.close ();
Method 1 is directly based on the string concatenation law and the number change law sum
Muladd (Num,times);
Method 2 uses the StringBuffer to perform the concatenation of the previously displayed string, adding the numbers using the Add method
System.out.println (expressed (Num,times) +add (num,times));
}
/**
* Determine the source data change law, according to the law number =number*10+number%10 change, and add more times
* @param num
* @param times
*/
private static void Muladd (String num, string times) {
Convert String to int
int number = Integer.parseint (num);
Long sum = 0;
int temp = 0;
for (int i = 1; I <= integer.parseint (times); i++) {
The law of every change of data
Temp =temp*10+number;
Sum +=temp;
If it is not the last addition, the output plus
if (I<integer.parseint (times)) {
System.out.print (temp+ "+");
}else {
System.out.print (temp+ "=");
}
}
SYSTEM.OUT.PRINTLN (sum);

}

string expression for successive multiplication of the preceding digits with the StringBuffer output
private static string expressed (string num,string times) {
StringBuffer sb = new StringBuffer ();
StringBuffer SUBSB = new StringBuffer ();
for (int i=1;i<=integer.parseint (times); i++) {
SUBSB = Subsb.append (num);
SB = Sb.append (SUBSB);
if (I<integer.parseint (times))
SB = Sb.append ("+");
}
Sb.append ("=");
return sb.tostring ();
}

Output continuous digital summation and
private static long Add (String num,string times) {
Integer a = Integer.parseint (num);
Long sum = 0;
Long subsum = 0;
for (int i=1;i<=integer.parseint (times); i++) {
Subsum = Subsum*10+a;
sum = sum+subsum;
}
return sum;
}
}


Operation Result:



3. Procedure 3

Package Tong.yue.hong;


Import Java.util.Scanner;
/*
* Title: If a number is exactly equal to the sum of its factors, this number is called the "end number", that is, the non-repetition factor other than itself and equal to itself. Programming to find all the finished numbers within M.
* For example 6=1+2+3. The second complete number is 28, it has approximate 1, 2, 4, 7, 14, 28, except it itself 28, the remaining 5 numbers add, 1+2+4+7+14=28.
* First Find all the factorization, then determine the sum and whether equal to the original number
*/


public class Numberspec {


public static void Main (string[] args) {
System.out.println ("Please enter a positive integer greater than 0 less than or equal to 10000 to represent the range of the end number (>0):");
Scanner Scanner = new Scanner (system.in);
int scale = Scanner.nextint ();
while (scale<0| | scale>10000) {
SYSTEM.OUT.PRINTLN ("Your input is incorrect, please re-enter a positive integer greater than 0 less than or equal to 10000 to indicate the range of the end number (>0):");
Scale = Scanner.nextint ();
}
Compnumber (scale);
}

private static void Compnumber (int n) {
System.out.println (n+ "in the end of the number:");
for (int i=1;i<=n;i++) {
int sum = 0;
for (int j=1;j<i/2+1;j++) {
if ((i%j) ==0) {
sum + = j;
}

}
if (sum==i) {
System.out.print (i+ "");
}
}
}



}

Operation Result:


Java BASIC Programming 50 questions (7-9 questions) detailed

Related Article

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.