Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2794912
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: C/C++

2012-08-23 20:38:05

先用快速排序,再把适合的数找出来

点击(此处)折叠或打开

  1. #include
  2. #include
  3. #define MaxN 11
  4. using namespace std;

  5. //升序,返回前面较小的(为真)
  6. int cmp(const int a,const int b)
  7. {
  8.     if(a!=b)
  9.         return (a
  10.     else
  11.         return 0;
  12. }
  13. void sumOfM(int a[],int M)
  14. {
  15.     int start=0,end=MaxN;
  16.     if(a[start]>M)
  17.     {
  18.         printf("所有都比M小");
  19.         return ;
  20.     }
  21.     while(start
  22.     {
  23.         if(a[start]+a[end]==M)
  24.         {
  25.             printf("%d %d\n",a[start],a[end]);
  26.             start++;
  27.             end--;
  28.         }
  29.         else if(a[start]+a[end]>M)
  30.         {
  31.             end--;
  32.         }
  33.         else
  34.         {
  35.             start++;
  36.         }
  37.     }
  38. }
  39. int main()
  40. {
  41.     int a[MaxN]={2,8,3,6,4,5,5,7,10,11,12};
  42.     sort(a,a+11,cmp);
  43.     /*for(int i=0;i<11;i++)
  44.         printf("%d ",a[i]);*/

  45.     sumOfM(a,10);
  46.     return 0;
  47. }
2 8
3 7
4 6
5 5
Press any key to continue

阅读(1989) | 评论(0) | 转发(0) |
0

上一篇:log4j

下一篇:BitMap 用于查重..只能查数字

给主人留下些什么吧!~~