学校有近千名学生,在操场上排队,5人一行余2人,7人一行余三人,3人一行余1人,编写程序求该校的学生人数。(用python)

写出来的代码不能运行 ,我的初步想法有失误,不知道如何去构造代码顺序

for i in range(1001, 0, -1):
    if i % 5 == 2 and i % 7 == 3 and i % 3 == 1:
        print(i)
        break

1、结果如下

img

2、代码如下

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Apr 14, 2022 22:50 AM

# =====关键点信息=====
# 学校有近千名学生,说明还没到一千,条件就可以设置为小于一千即可
# 5人一行余2人
# 7人一行余3人
# 3人一行余1人
# =====/关键点信息=====

flag=False
indexValue=0
totalStudentCount=0

while(indexValue<1000):
    indexValue+=1
    if(indexValue%5==2 and indexValue%7==3 and indexValue%3==1):
        totalStudentCount=indexValue
        flag=True
        
# 输出最后一个满足条件的值,就是该校学生人数
print('学校总人数:%s' % totalStudentCount)
# 学校有近千名学生,在操场上排队,5人一行余2人,7人一行余三人,3人一行余1人,编写程序求该校的学生人数。


for i in range(1000, 1, -1):
    # 5人一行余2人,7人一行余三人,3人一行余1人
    if i % 5 == 2 and i % 7 == 3 and i % 3 == 1:
        print(f"学校有:{i}人")
        # 因为学校有近千名学生,所以比较靠近1000的就是,找到就中断
        break
学校有:997人
  • ```

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int i;
//人数不过千人
for (i = 100; i < 1000; i++)
{
if(i%5==2 && i%7==3 && i%3==1) //人数条件
System.Console.WriteLine("人数=" + i); //列出人数
}
}
}
}

```

比较硬核又简单的办法,用循环从999开始往下一个个试

i=1000
while True:
    if i%5==2 and i%7==3 and i%3==1:
        print(i)
        break
    else:
        i-=1