TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评[cpp] view plaincopy![]() ![]()
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre class="cpp" name="code" code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179"><pre class="cpp" name="code" code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179"><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><pre code_snippet_id="91880" snippet_file_name="blog_20131209_3_1310323" class="cpp" name="code">/*
- 功能:已知A是有30个元素的整型数组,编写一个对A[I1]到A[I2](I1≤I2)之间的元素排序的函数(从大到小排序)
- 请调用上述函数先将A[5]至A[20]之间的元素排序,然后将A中的所有元素值输出。再调用上述函数对A[15]至A[30]之间的元素排序,
- 最后再将A中30个元素值输出。
- 要求:1) 输入输出均由主函数实现。
- 2) 输出时每10个元素占1行。
-
- 时间:1:30 2013/10/26
- */
-
- #include<stdio.h>
- #include <stdlib.h>
-
- void order55(int a[],int n,int s,int t)
- {
- if(s>t && s<n && t<n) //保证数据正确性!
- {
- printf("ERROR");
- return;
- }
- else
- {
- for(int i=s,j=0;i<=(s+t)/2;j++,i++) //从第i到第j个元素遍历,并且排序
- {
- a=a+a[t-j];
- a[t-j]=a-a[t-j];
- a=a-a[t-j];
- }
- }
- }
-
- void main()
- {
- int a[30];
- for (int i=0;i<30;i++) //输入数组
- {
- scanf_s("%d",&a);
- }
- puts("Before changes :the array is :");
- for (int i=0;i<30;i++)
- {
- if(i%10==0)printf("\n");
- printf("%3d",a);
- }
- order55(a,30,4,19); //第一次排序
- printf("\n1st changes:\n");
- for (int i=0;i<30;i++)
- {
- if(i%10==0)printf("\n");
- printf("%3d",a);
- }
- order55(a,30,14,29); //第二次排序
- printf("\n1st changes:\n");
- for (int i=0;i<30;i++)
- {
- if(i%10==0)printf("\n");
- printf("%3d",a);
- }
- system("pause");
- }</pre><br><br></pre></pre></pre>
|
|