Created
January 28, 2015 02:48
-
-
Save junichiro/f3bcf964cbcc69c6bb4e to your computer and use it in GitHub Desktop.
code_iq:1198
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func main() { | |
CodeIq() | |
} | |
func CodeIq() { | |
ret := 0 | |
for i := 10; i <= 99; i++ { | |
b := toB(i) | |
num_b := countOne(b) | |
bcd := toBCD(i) | |
num_bcd := countOne(bcd) | |
if num_b == num_bcd { | |
ret++ | |
fmt.Printf("i = %d, b = %s, b_num = %d, BCD = %s, BCD_num = %d \n", i, b, num_b, bcd, num_bcd) | |
} | |
} | |
fmt.Printf("%d 個\n", ret) | |
} | |
func toBCD(i int) string { | |
var bcd string | |
for _, char := range fmt.Sprint(strconv.Itoa(i)) { | |
b, _ := strconv.Atoi(string(char)) | |
bcd = bcd + fmt.Sprintf("%b", b) | |
} | |
return bcd | |
} | |
func toB(i int) string { | |
return fmt.Sprintf("%b", i) | |
} | |
func countOne(x string) int { | |
ret := 0 | |
for _, char := range x { | |
i, _ := strconv.Atoi(string(char)) | |
ret = ret + i | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment