Hdoj (HDU) 2502-month number (binary)

Source: Internet
Author: User

Problem Description
When the cold month is still reading freshman year, he in a martial arts cheats (according to later research, is the Computer Foundation, Kuanghan-ing), found the magic of binary numbers.
If a positive integer m is represented as a binary, its number of BITS is n (without leading 0), and the cold month is called an n binary number. Of all n binary numbers, the total number of 1 is called the number of months corresponding to N.
For example, the total number of 32 binary is 4, 4 (100), 5 (101), 6 (110), 7 (111), and 1 of them are 1+2+2+3=8, so 3 corresponds to 8 of the month.

Input
Give you an integer t that represents the number of groups of input data, followed by a T-line, each containing a positive integer n (1<=n<=20).

Output
For each n, the number of months that corresponds to the output n in a row.

Sample Input
3
1
2
3

Sample Output
1
3
8

This topic. Using Java to calculate a timeout of 0.0

N-bit binary number a total number of x=2^ (n-1), and then a few examples can be seen:
For example:
Enter 4, then there are so many 4-bit binary numbers:
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
The first column has x 1, each column has X/2 1, then a total of s=x+ (n-1) *X/2 1

JAVAAC:

Import Java.util.Scanner; Public classmain{ Public Static void Main(string[] args) {Scanner sc=NewScanner (System.inch);intT =sc.nextint (); while(t-->0){intn =sc.nextint ();intCont =0;intx = (int) Math.pow (2, N-1);//int x = (int) Math.pow (2, N)-(int) Math.pow (2, n-1);//Samecont+=x* (n1)/2; System. out. println (CONT+X); }    }}

C Analog Output:
(This will time out in Java)

#include <iostream>#include <stdio.h>#include <math.h>using namespace STD;intMain () {intt;scanf("%d", &t); while(t-->0){intn;intCont =0;intXscanf("%d", &n); for(intI=POW(2, N-1); i<=POW(2, N)-1; i++) {x=i; while(x>0)                {if((%2)==1) cont++; X/=2; }            }printf("%d\n", cont); }    }

Hdoj (HDU) 2502-month number (binary)

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.