Self Describing Numbers自描述数字问题(Python版)

该博客介绍了自描述数字的概念,即每个位置上的数字等于该数字在数中出现的次数。提供了一个输入样例,解释了如何判断一个正整数是否为自描述数字,并给出了用Python解决该问题的方案。

Description:

A number is a self-describing number when (assuming digit positions are labeled 0 to N-1), the digit in each position is equal to the number of times that that digit appears in the number.

Input sample:

The first argument is the pathname to a file which contains test data, one test case per line. Each line contains a positive integer. Each line is in the format: N i.e. a positive integer eg.

2020
22
1210

Output sample:

If the number is a self-describing number, print out a 1. If not, print out a 0 eg.

1
0
1

解释:

自描述数字是指第i位上的数字等于i在整个数字中出现的次数,如2020,第0位上的数字为2,2020中共有2个0;第1位上的数字是0,2020中共有0个1;第2位上的数字是2,2020中共有2个2;第3位上的数字是0,2020中共有0个3;所以2020是自描述数字

求解方案:

import sys

if __name__ == "__main__":
        argv = sys.argv
        inf = open(argv[1],'r')
        while True:
                line = inf.readline()[:-1]
                if len(line) == 0:
                        break
                flag = True
                for i in range(0,len(line)):
                        if int(line[i]) != line.count(str(i)):
                                flag = False
                                break
                if flag:
                        print 1
                else:
                        print 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值