jeapedu 19 字符串算术运算习题答案

链接: https://pan.baidu.com/s/1eUlMJSI 密码: dk43

# 1 print jeapedu000~jeapedu100
# 2 count "c" in "aacacaac" * 100 count and pos
# 3 random.randint(100000,999999) 
#   1) if 4 and 7 in it, otherwise drop the num
#   2) when not 4 and 7 in it, whether 6 and 8 in it

# 1 print jeapedu000~jeapedu100
#jeapedu000
#jeapedu001
#jeapedu009
#...
#jeapedu010
#jeapedu011
#jeapedu099
#...
#jeapedu100
# while
i = 0
while i <= 100:
    if i < 10:
        print("jeapedu00" + str(i))
    elif i < 100:
        print("jeapedu0" + str(i))
    else:
        print("jeapedu" + str(i))
    i +=1
# str.zfill(width) str method
i = 0
while i <= 100:
    s = str(i)
    s = s.zfill(3)
    print("jeapedu" + s)
    #print("jeapedu" + str(i).zfill(3))
    i += 1
# num formatting
i = 0
while i <= 100:
    s = "%03d" %i
    print("jeapedu" + s)
    #print("jeapedu" + "%03d" %i)
    i += 1
# num format function
i = 0
while i <= 100:
    s = "{0:03d}".format(i)
    print("jeapedu" + s)
    i += 1

# 2 count "c" in "aacacaac" * 100 count and pos
s = "aacacaac" * 100
i = 0
count = 0
while i < len(s):
    if s[i] == "c":
        print("found", s[i], i)
        count += 1
    i += 1
print(count)

# 3 random.randint(100000,999999) 
#   1) if 4 and 7 in it, otherwise drop the num
#   2) when not 4 and 7 in it, whether 6 and 8 in it
import random
i = 0
while i < 20:
    x = random.randint(100000,999999)
    s = str(x)
    j = 0
    if "4" in s or "7" in s:
        print(s, end=' ')
        while j < len(s):
            if s[j] == "4" or s[j] == "7":
                print(s[j], j, end=' ')
            j += 1
    elif "6" in s or "8" in s:
        print(s, end=' ')
        while j < len(s):
            if s[j] == "6" or s[j] == "8":
                print(s[j], j, end=' ')
            j += 1

    print("")
    i += 1

import random
x = random.randint(100000, 999999)
print(x + 100, type(x), str(x), type(str(x)))
s = str(x)
i = 1
while i < 20:
    x = random.randint(100000, 999999)
    s = str(x)
    #if "4" in s:
        #print(i, "has 4 in", s)
    if "4" in s and "7" not in s:
        print(i, "has 4 in", s)
    else:
        print(i, s)
    i += 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.阶乘尾部0的个数 问题描述 给定非负整数n,计算n的阶乘尾部0的个数。 输入 输入数据有若干行,每行上有一个非负整数n,对应一种情形。 输出 对于每一种情形,直接输出结果、换行。 2.判断算式的正确性 问题描述 给定一个算式,该算式中只含一个四则运算符号,操作数及结果均为整数。要求判断该算式的正确性(规定:除法必须除尽才算正确)。 输入 输入数据有若干行,每行上有一个算式,对应一种情形。 输出 对于每一种情形,直接输出T(表示正确)或F(表示错误),换行。 3.计算一系列实数的个数、最小值、最大值和“平均值” 问题描述 给定若干个(大于0且不超过1024)实数,计算其中数据的个数(n)、最小值、最大值,以及“平均值”。此处的“平均值”定义为,若n>2,则需去掉一个最大值、一个最小值,剩下的n-2个数据的算术平均值;若n为1或2,则为普通的算术平均值。 输入 输入数据有多行,每一行上有若干个实数,对应一种情形(数据之间用一个空格字符分隔)。 输出 对于每一种情形,依次输出数据的个数、最小值、最大值、平均值、换行。其中数据之间用逗号和空格分隔,浮点型数据保留2位小数。 4.字符串倒置 问题描述 对于给定的字符串,将其按字符倒置。 输入 输入数据有多行。每一行为一个字符串字符串长度小于1024,其中可能含有空格字符),对应一种情形。 输出 对于每一种情形,输出结果并换行。 5.字符在字符串中首次出现的位置 问题描述 给定一个字符、一个字符串字符串的长度小于1024),计算该字符在字符串中首次出现的位置。 输入 输入数据有若干行。每两行对应一种情形,这两行中的第一行上有一个字符(请注意将该字符后面的换行字符“吃掉”),第二行上有一个字符串字符串中可能含有空白字符)。 输出 对于每一种情形,输出计算结果(若字符不在字符串中,则输出0),然后换行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值