n = int ( input ()) num =0 for i in range (0, n ): temp = int ( input ()) num += temp print ('{:.2f}'. format ( num / n ))
时间: 2024-04-01 08:31:11 浏览: 22
这段代码的功能是计算 n 个数的平均值,并输出结果。首先,代码通过 `input()` 函数获取一个整数 `n`,表示接下来将输入多少个数。然后,代码通过 for 循环,每次获取一个整数 `temp` 并将其加到变量 `num` 中。最后,代码通过 `print()` 函数输出 `num` 除以 `n` 的结果,即 n 个数的平均值。其中,`'{:.2f}'.format()` 是格式化输出,表示输出一个保留两位小数的浮点数。
相关问题
def main(): N = int(input()) for num in range(N+1): sum = 0 temp = num while temp != 0: sum = sum + temp % 10 #**********begin********** temp = temp / 10 #**********end********** if sum == 8: print(num,end = ' ') main() 哪里错误
在代码中,`temp` 是一个整数,而在 `temp = temp / 10` 这行代码中,你使用了除法 `/` 运算符,其结果是一个浮点数,因此会导致程序错误。你需要使用整除运算符 `//` 来进行整数除法,如下所示:
```python
def main():
N = int(input())
for num in range(N+1):
sum = 0
temp = num
while temp != 0:
sum = sum + temp % 10
temp = temp // 10 # 注意这里使用了整除运算符 //
if sum == 8:
print(num,end = ' ')
main()
```
这样就可以正确地输出0到N之间各位数字之和为8的数了。
将如下代码转成C#语言代码 import math def relatively_prime(a,b): # a > b while b != 0: temp = b b = a%b a = temp if a==1: return True else: return False def millerRabin(num): if num%2 ==0: return False flag = True Subsquare = 0 temp = num - 1 while True: temp = temp / 2 Subsquare += 1 if temp % 2 != 0: break b=[] # 存放所求整数(num)的原根 count = 0 for i in range(2,num-1):# g^(P-1) = 1 (mod P) if relatively_prime(num,i): b.append(i) count += 1 if count == 5: # 控制检测次数 break for i in b: two = 0 while True: if (i**temp)**(2**two)%num == 1 or (i**temp)**(2**two)%num == num-1: flag = True break else: two += 1 if two == Subsquare: flag = False break if flag == False: break # 如果存在一次不满足条件,则退出循环 return flag num = input(u"请输入要进行Miller-Rabin算法检测的数:") if millerRabin(num): print u"{0}大概率是素数".format(num) else: print u"{0}是合数 ".format(num)
using System;
namespace MillerRabin
{
class Program
{
static bool RelativelyPrime(int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
if (a == 1)
{
return true;
}
else
{
return false;
}
}
static bool MillerRabin(int num)
{
if (num % 2 == 0)
{
return false;
}
bool flag = true;
int subsquare = 0;
int temp = num - 1;
while (true)
{
temp = temp / 2;
subsquare += 1;
if (temp % 2 != 0)
{
break;
}
}
int[] b = new int[5]; // 存放所求整数(num)的原根
int count = 0;
for (int i = 2; i < num - 1; i++)
{
if (RelativelyPrime(num, i))
{
b[count] = i;
count += 1;
if (count == 5) // 控制检测次数
{
break;
}
}
}
for (int i = 0; i < 5; i++)
{
int two = 0;
while (true)
{
if (Math.Pow(b[i], temp * Math.Pow(2, two)) % num == 1 || Math.Pow(b[i], temp * Math.Pow(2, two)) % num == num - 1)
{
flag = true;
break;
}
else
{
two += 1;
if (two == subsquare)
{
flag = false;
break;
}
}
}
if (flag == false)
{
break; // 如果存在一次不满足条件,则退出循环
}
}
return flag;
}
static void Main(string[] args)
{
Console.WriteLine("请输入要进行Miller-Rabin算法检测的数:");
int num = Convert.ToInt32(Console.ReadLine());
if (MillerRabin(num))
{
Console.WriteLine("{0}大概率是素数", num);
}
else
{
Console.WriteLine("{0}是合数", num);
}
Console.ReadLine();
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)