Ultraviolet A 355-the bases are loaded

Source: Internet
Author: User

Question: hexadecimal conversion, plus legal judgment.

Analysis: number theory. Convert it to decimal, convert it to the corresponding hexadecimal format, and then output it.

Base to decimal: multiply the order by base addition;

Convert decimal to base: returns the remainder of base in reverse order.

Note: When the value is 0.

# Include <iostream> # include <cstdlib> # include <cstdio> using namespace STD; char number1 [11]; int ctoi (char ch) {If (CH> = '0' & Ch <= '9') return ch-'0'; else return ch-'A' + 10 ;} char itoc (INT v) {If (v <10) return V + '0'; else return V + 'a'-10;} void dtob2 (long V, int B2) {If (v) {dtob2 (V/B2, B2); printf ("% C", itoc (V % B2) ;}} int main () {int base1, base2; while (~ Scanf ("% d % s", & base1, & base2, number1) {// int flag = 0; For (INT I = 0; number1 [I]; ++ I) if (ctoi (number1 [I])> = base1) Flag = 1; if (FLAG) {printf ("% s is an illegal base % d Number \ n", number1, base1); Continue ;}// convert it to a 10th hexadecimal value long value = 0ll; for (INT I = 0; number1 [I]; ++ I) {value * = base1; Value + = ctoi (number1 [I]);} // convert to base 2 hexadecimal number printf ("% s base % d =", number1, base1); If (value = 0ll) printf ("0 "); else dtob2 (value, base2); printf ("base % d \ n", base2);} return 0 ;}

Ultraviolet A 355-the bases are loaded

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.