• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
crucio
博客园    首页    新随笔    联系   管理    订阅  订阅
card

if sum of the two cards of the same number is identical with the number in another card,you can remove these three cards.

in otner words,if there are cards as follows:

2 4 7 3 3 6 2 8

as 2 +2=4,three of the  cards can be removed.

finally,two cards of 7,8 remained .therefore ,return 15=7+8.

there is no fixed orders to rermove cards.

 

code:

#include <stdio.h>
#include <stdlib.h>
int test_main(int data[8]);
void build_data(int data[999])
 {
  for(int i=0;i<999;i++)
  {
     data[i]=rand()%24+1;
  }
    }
void main(void)
{ 
 int data[8]={2,4,7,3,3,6,2,8};
 for(int l=0;l<10;l++)
 {
  //build_data(data);
  printf("%d\n",test_main(data));
 }
 return ;
}
 
int test_main(int data[8])
{
   int i,n,sum,p;
   int times[25]={0};
   for(i=0;i<8;i++)//每个数字出现的次数
    {
     times[data[i]]++;
       } 
   for(n=12;n>0;n--)
 {
  while(times[2*n]>=1&&times[n]>=2)
  {
   times[n]=times[n]-2;
   times[2*n]--;
  }
 }
   for(p=1,sum=0;p<=24;p++)
   {
  sum=sum+p*times[p];
   }
   return sum;
}

 

posted on 2017-03-27 09:36  crucio  阅读(350)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3