The corresponding __java of column names and numbers in algorithm Excel

Source: Internet
Author: User
Tags pow
interview an algorithm pen test, now summarized as follows. Reference to the online code, has given the source code links, thanks to the original author. The program has been debugged and is correct 1. In Excel, the name of the column is such an ascending sequence: Seq={a, B, C 、... Z, AA, AB, AC 、... AZ, BA, BB, BC 、... BZ, CA 、... ZZ, AAA, AAB ...}. Ask for any sequence, and the sequence is the first few strings. Method One: This problem can be seen as 26 into the decimal system. namely abc=a* (10^2) +b* (10^1) +c This method is simpler, do not write ~ ~ ~ Method Two: abc= (a*10+b) *10+z. This method can be the sequence of each character to get a, and then, if there is B, then put a left one, that is, a*10+b, if there is another C, the previous result of the whole left one, plus C, that is (a*10+b) *10+c reference https://www.nowcoder.com /questionterminal/2313251da21e4f6390eaf8a4539ef41d?pos=6&mutitagids=589&orderbyhotvalue=2
Import Java.util.Scanner;
	public class Testcoltonum {public

		static void Main (string[] args) {
			Scanner cin=new Scanner (system.in);
	        String S=cin.next ();
	        int result=0;
	        for (int i=0;i<s.length (); i++)
	      {
	          result=result*26+ (S.charat (i)-' A ' + 1);
	          
	      }
	       
	        SYSTEM.OUT.PRINTLN (result);
		}
	



2. In Excel, the name of the column is such an ascending sequence: Seq={a, B, C 、... Z, AA, AB, AC 、... AZ, BA, BB, BC 、... BZ, CA 、... ZZ, AAA, reference AaB ...}. To give any one natural number n, to find the nth string in this sequence. function declaration: String getcolumnlable (int n)Reference http://blog.csdn.net/guomutian911/article/details/49129321
Package From0519;
Import Java.util.Scanner;

public class Exceltonum {public

	static void Main (string[] args) {
		Scanner cin=new Scanner (system.in);
        int Num=cin.nextint ();
        String tmp = "";
        int count = 1;
        while ((int) (NUM/MATH.POW, count-1) >0) {
        	int w3 = (int) (num% Math.pow (26,count)))/((int) Math.pow (26, count-1));
        	char C = (char) (w3+65);
        	TMP = c + tmp;
        	count++;
        }
        SYSTEM.OUT.PRINTLN (TMP);
	}



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.