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">/************************************************************************/
- /*
- 20. 找出 M×N 数组中所有不相邻元素,
- 并求出它们的和(相邻的数:前一个是偶数,后一个是素数)。
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
-
- bool IsOdd(int num)
- {
- return (bool)(num%2);
- }
- bool IsSS20(int num)
- {
- if (num<2)
- {
- return false;
- }
- else if (num==2)
- {
- return true;
- }
- else if (num==3)
- {
- return true;
- }
- else
- {
- for (int i=2;i<=sqrt((double)num);i++)
- {
- if (num%i==0)
- {
- return false;
- }
- }
- return true;
- }
- }
- void main()
- {
- const int N=10;
- const int M=10;
- int arr[N][M]={0};
- for (int i=0;i<N;i++)
- {
- for (int j=0;j<M;j++)
- {
- arr=rand()%100;
- }
- }
- int * brr=&arr[0][0];
- int sum=0;
- if (!IsOdd(*(brr)&&IsSS20(*(brr+1))))
- {
- sum+=*(brr);
- printf("%5d",*(brr));
- }
- for (int i=1;i<M*N-1;i++)
- { int flag=1;
- if (!IsOdd(*(brr+i)&&IsSS20(*(brr+i+1))))
- {
- flag=0;
-
- }
- if (!IsOdd(*(brr+i-1)&&IsSS20(*(brr+i))))
- {
- flag=0;
-
- }
- if (flag==1)
- {
- sum+=(*(brr+i));
- printf("%5d",*(brr+i));
- }
-
- }
-
- if (!IsOdd(*(brr+M*N-1)&&IsSS20(*(brr+M*N-2))))
- {
- sum+=*(brr+M*N-1);
- printf("%5d",*(brr+M*N-1));
-
- }
- printf("\n和为:%d",sum);
- system("pause");
- }</pre><br><br><br></pre>
|
|