TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">#include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- /*
- 判断整数n是否为质数
- 是:返回1
- 否:返回0
- */
- int isPrimeNum1(int n)
- {
- int isPrime = 1;
- if (n == 1)
- {
- return 0;
- }
- for (int i = 2; i <= (int)(sqrt((double)n)); i++)
- {
- if (n % i == 0)
- {
- isPrime = 0;
- break;
- }
- }
- return isPrime;
- }
- // 排序采用冒泡进行
- void sort1(int *a,int n)
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = i + 1; j < n; j++)
- {
- if (a > a[j])
- {
- a = a ^ a[j];
- a[j] = a ^ a[j];
- a = a ^ a[j];
- }
- }
- }
- }
-
-
- void main()
- {
- const int N = 20;
- int a[N];
- int index = 0;
- int data;
- for (int i = 0; i< N;i++)
- {
- scanf_s("%d",&data); // 输入数字
- if (isPrimeNum1(data)) // 判断是否素数,如果是则保存在a数组中
- {
- a[index++] = data;
- }
- }
-
- sort1(a,index); // 对a数组进行排序
- for (int i = 0; i < index; i++) // 打印数组
- {
- printf("%-3d",a);
- }
- printf("\n");
- system("pause");
- }
- </pre><br><br><br></pre></pre>
|
|