周末作业-循环练习题

  1. 判断101-200之间有多少个素数,并输出所有素数。

    count = 0
    for x in range(101, 200):
        for y in range(2, x):
            if x % y == 0:
                break
        else:
            count += 1
            print(x)
    print('有', count, '个素数')
    
  2. 求整数1~100的累加值,但要求跳过所有个位为3的数。

    s = 0
    for x in range(1, 101):
        if x % 10 == 3:
            continue
        else:
            s += x
    print(s)
    
  3. 有⼀分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的第20个分数

    count = 1
    numerator = 1
    denominator = 2
    while count < 20:
        count += 1
        temp = denominator
        denominator = numerator + denominator
        numerator = temp
    print(denominator, '/', numerator)
    
  4. 写程序计算 n 的阶乘 n! 的结果

    n = int(input('请输入n:'))
    result = 1
    for x in range(1, n + 1):
        result *= x
    print(result)
    
  5. 求1+2!+3!+…+20!的和

    s = 0
    for x in range(1, 21):
        result = 1
        for y in range(1, x + 1):
            result *= y
        s += result
    print(s)
    
  6. 写程序求表达式 a + aa + aaa + aaaa+ … 的结果,其中a是1~9的数字,求和的项数用n来控制。(a和n可以用变量来表示)

    例如:a为3, n为5的时候: 3 + 33 + 333 + 3333 + 33333

    a = int(input('请输入a:'))
    n = int(input('请输入n:'))
    s = 0
    a1 = a
    for x in range(1, n + 1):
        print(a1)
        s += a1
        a1 = a1 + a * (10 ** x)
    print(s)
    
  7. 控制台输出三角形

    a.根据n的值的不同,输出相应的形状
    n = 5时             n = 4
    *****               ****
    ****                ***
    ***                 **
    **                  *
    *
    
    n = int(input('请输入n:'))
    for x in range(n, 0, -1):
        for y in range(x):
            print('*', end='')
        print()
    
    b.根据n的值的不同,输出相应的形状(n为奇数)
    n = 5               n = 7
      *                    *
     ***                  ***
    *****                *****
                        *******
        
    n = int(input('请输入n(n为奇数):'))
    print((n + 1) // 2)
    for x in range(1, (n + 1) // 2 + 1):
        for y in range(1, n + 1):
            if y <= n // 2 - x + 1 or y >= n // 2 + x + 1:
                print(' ',end='')
            else:
                print('*',end='')
        print()
        
        
        
    c. 根据n的值的不同,输出相应的形状
    n = 4
       1
      121
     12321
    1234321
    
    n = 5
        1
       121
      12321
     1234321
    123454321
    
    n = int(input('请输入n:'))
    for x in range(1, n + 1):
        count = 1
        for y in range(1, 2 * n):
            if y < n - x + 1 or y > n + x - 1:
                print(' ', end='')
            else:
                if y < n:
                    print(count, end='')
                    count += 1
                else:
                    print(count, end='')
                    count -= 1
        print()
    
  8. 小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元正好花掉,可有哪些购买结合?

    print('洗发水', '\t', '香皂', '\t', '牙刷')
    for soap in range(1, 50):
        for toothbrush in range(1, 20):
            # shampoo = (100 - soap * 2 - toothbrush * 5) / 15
            for shampoo in range(1, 6):
                if shampoo * 15 + soap * 2 + toothbrush * 5 == 100:
                    print(shampoo, '\t\t', soap, '\t\t', toothbrush)
    
  9. 一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?

    count = 0
    paper_h = 0.08
    mountain_h = 8848.13 * 1000
    while paper_h < mountain_h:
        count += 1
        paper_h = 0.08 * 2 ** count
        # print(count,'\t',paper_h)
    print(count)
    
  10. 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

    m0_r = 0
    m1_r = 1
    month = 1
    rabbit = 1
    n = int(input('请输入计算前多少个月的兔子对数:'))
    while month <= n:
        print('第', month, '个月有', rabbit, '只兔子')
        month += 1
        rabbit = m0_r + m1_r
        m0_r = m1_r
        m1_r = rabbit
    
  11. 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。

    num = int(input('请输入正整数:'))
    print(num, '= ', end='')
    
    from math import sqrt
    
    
    def is_prime(n):
        if n == 1:
            return False
        for i in range(2, int(sqrt(n)) + 1):
            if n % i == 0:
                return False
        return True
    
    
    while not is_prime(num):
        count = 2
        for x in range(count, int(sqrt(num)) + 1):
            if is_prime(x) and num % x == 0:
                num = int(num / x)
                print(x, end='x')
                break
    else:
        print(num)
    
  12. 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值

    data = int(input('请输入一个四位整数:'))
    a = (data // 1000 + 5) % 10
    print(a)
    b = (data // 100 % 10 + 5) % 10
    print(b)
    c = (data //10 % 10 + 5) % 10
    print(c)
    d = (data % 10 + 5) % 10
    print(d)
    a, d = d, a
    b, c = c, b
    print(a,b,c,d)
    
  13. 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。

  14. 本金10000元存入银行,年利率是千分之三。每过1年,将本金和利息相加作为新的本金。计算5年后,获得的本金是多少。

    money = 10000
    annual_interest_rate = 0.003
    for x in range(5):
        interest = money * annual_interest_rate
        money += interest
    print(money)
    
  15. 输入一个整数,计算它各位上数字的和。(注意:输入的整数可以是任意位)

    num = input('请输入一个整数:')
    s = 0
    for x in num:
        temp = int(x)
        print(x)
        s += temp
    print(s)
    
  16. 求两个数的最大公约数和最小公倍数。(提示:公约数一定小于等于两数中的小的那个数,且能同时被两个数整除;公倍数一定大于等于两数中的大数,且是大数的倍数又能被两数中的小数整除)

    a = int(input("请输入a:"))
    b = int(input("请输入b:"))
    big = a if a > b else b
    small = a if a < b else b
    for x in range(2,small):
        if a % x == 0 and b % x == 0:
            gcd = x
    print(gcd)
    for x in range(big, a * b + 1):
        if x % a == 0 and x % b == 0:
            lcm = x
            break
    print(lcm)
    
在Spring Boot应用中,如果你想要允许跨域请求并在周末(周六、周日)特定时间开启Access-Control-Allow-Origin策略,你可以创建一个自定义过滤器(Filter),例如使用`@CrossOrigin`注解,并通过`@PreAuthorize`或`@ConditionalOnDate`来控制其生效的时间。 首先,创建一个定制的过滤器,比如`WeekendCorsFilter.java`: ```java import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @Configuration @Component public class WeekendCorsFilter { private final ManagementServerProperties management; public WeekendCorsFilter(ManagementServerProperties management) { this.management = management; } @Bean(name = "weekendCorsFilter") public WebMvcConfigurer corsConfigurer() { return (configurer) -> { configurer.addCorsConfiguration( "/**", // 允许所有的路径 headers -> { if (isWeekend()) { // 检查是否为周末 headers.setAllowCredentials(true); // 设置允许凭证 headers.addAllowedHeader("*"); // 允许所有头信息 headers.addAllowedMethod("*"); // 允许所有HTTP方法 headers.addAllowedOrigin("*"); // 允许来自任何源的请求 } else { // 非周末则按默认设置或你自己的规则处理 configurer.cors().allowCredentials(false); configurer.cors().allowedOrigins("http://your-origin.com"); } }); }; } private boolean isWeekend() { String[] daysOfWeek = {"Saturday", "Sunday"}; for (String day : daysOfWeek) { if (management.getEndpointProperties().getWeb().getDatepatterns().contains(day)) { return true; // 如果找到对应的日期模式,则为周末 } } return false; } } ``` 然后在启动类或Spring Boot Actuator配置中启用这个过滤器: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Autowired private WeekendCorsFilter weekendCorsFilter; // ... @PostConstruct public void init() { weekendCorsFilter.corsConfigurer(); // 加载配置 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值