C#习题三/学习

题目:对于[10-99]之间的正整数N,计算N2+N+41值R。如果R为质数,则输出N,R;如果R不为质数,则输出N、R及R的所有因数。

 

涉及到的语法知识:

1、 虽然本题没有使用,但写的时候出现了问题,记录一下数组的创建方法。

基本格式为 变量类型[] 变量名;

a)   string[] myArray=new string[10];

b)   string[] myArray={"1","2"};

c)   string[] myArray=new string[3]{"1","2","3"};

d)   string[] myArray=new string[]{"1","2","3","4"};

2、List数组的应用

 1     static void PrintThreeSolution()
 2         {
 3             int n = Convert.ToInt32(Console.ReadLine());
 4             int r = n * n + n + 41;
 5             bool flag = false;
 6             List<int> divisor = new List<int>();
 7             for (int i=2; i<Math.Sqrt(r);i++)
 8             {
 9                 if(r%i==0)
10                 {
11                     flag = true;
12                     divisor.Add(i);
13                 }
14             }
15             if(flag)
16             {
17                 Console.WriteLine("n=" + n + "   r=" + r);
18                 foreach(var index in divisor)
19                 {
20                     Console.WriteLine(index);
21                 }
22                 Console.WriteLine("1");
23                 Console.WriteLine(r);
24             }
25             else
26             {
27                 Console.WriteLine("n="+n+"   r="+r);
28             }
29         }
30     }

猜你喜欢

转载自www.cnblogs.com/wu199723/p/11537201.html