[HDU] [2502] Number of Moons

Problem Description

Loading...

Problem Analysis

The way I wrote it back then was a bit cumbersome. Actually, you just need to keep track of the number of 1s.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
#include<string.h>
#include<math.h>
int Rt(int a[],int N) {
	int i;
	for(i=0; N>=2; i++) {
		a[i]=N%2;
		N=N/2;
	}
	a[i]=N;
	return i;
}
int main() {
	int T,n,l,r;
	int i,j,k;
	int a[22];
	scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		r=0;
		for(i=pow(2,n-1),j=0; i<pow(2,n); i++) {
			l=Rt(a,i);
			for(k=0; k<=l; k++) {
				if(a[k]==1)
					r++;
			}
			memset(a,0,sizeof(a));
		}
		printf("%d\n",r);
	}
	return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...