Fourth session of Henan Province Program design contest serial Number interchange Nyoj 303

Source: Internet
Author: User
Tags numeric min printf time limit
Serial Number InterchangeTime limit: Ms | Memory limit: 65535 KB Difficulty: 2 description

Dr.kong designed a smart robot, which will quickly calculate cell coordinates in a spreadsheet. The row coordinates of a cell are numeric ordinal numbers numbered by numbers, and column coordinates use alphabetic numbers. Looking at the alphabetical sequence, we found that the alphabetical sequence number 1th through 26th was a,b,...,z, followed by the 27th column ordinal of AA, the 28th column AB, and so on.

If you give Dr.kong robot a number (such as 32), it can quickly calculate the equivalent of the letter sequence number (ie, AF), if you give the robot a letter number (such as AA), it can also quickly calculate the equivalent number (27), you can compete with the man, see who can calculate faster and more accurate. Enter the first line: N indicates how many sets of test data are available.
Next there are n rows, each of which is either a positive integer or a string consisting only of uppercase letters.
Input guarantee, all numbers and alphanumeric numbers corresponding to the number of ≤2*10^9 output for each row of test data, output one line. If the input is a positive integer ordinal, the output is equivalent to the alpha ordinal, and if the input is a string, the equivalent numeric sequence number is output. Sample input

3
G
AA
Sample output
AA
7
27
Source fourth session of Henan Province Program design Contest uploader Zhang Yunzun





No, it's just 26. Convert a B C D ... Y Z is 1 2 3 4...25 0

Note that when the number is in the letter Z, the quotient should be specially handled: minus one

Stick to the code.



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
Char a[500];
void dig (int len)
{
	long sum=0;
	for (int i=len-1,j=0;i>=0;i--, j + +)
	{
		sum+= (long) POW (26,j) * (a[i]-' a ' + 1);
	}
	printf ("%ld\n", sum);
	return;
}
void Zim (int len)
{
	int i=0;
	Long sum;
	Char c[500];
	Sum=atol (a);//character converted to Long data 
	while (sum)
	{
		long T;
		t=sum%26;
		SUM=SUM/26;
		if (t==0) c[i++]= ' Z ', sum-=1;
		else c[i++]=t+ ' A '-1;
	}
	for (int j=i-1;j>=0;j--)
	printf ("%c", C[j]);
	printf ("\ n");
	return;
}
int main ()
{
	int n,len;
	scanf ("%d", &n);
	while (n--)
	{
	    scanf ("%s", a);
		Len=strlen (a);
		if (a[0]>= ' a ') dig (len);
		Else Zim (len);
	}
	return 0;
}


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.