【求助】C程序改写成JavaScript

MarkInOneDay 2010-11-04 01:21:58
大致目标:在N张牌中取出顺子并打印。

请各位大牛帮帮忙,很快就会结贴!


// NCard.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#define SeqNum 8
#define CardClass 13
using namespace std;

int BigSeq[SeqNum][2]; //记录顺子起始位置,长度
int SmallSeq[52][2]; //记录零散牌起始位置,长度
int Card[CardClass]; //记录各卡片数量
int SeqCount;
void Translate(const char* str)
{
char c;
while('\0'!=(c=*str++))
{
if(c>=2+'0'&& c<=9+'0') Card[c-'0'-2]+=1;
else
{
switch(c)
{
case 'B' : Card[8]++;break;
case 'J' : Card[9]++;break;
case 'Q' : Card[10]++;break;
case 'K' : Card[11]++;break;
case 'A' : Card[12]++;break;
}
}
}

}
void Segmention(int SmallCount)
{
int i=0,index=0,temp;
for(index=SmallCount-1;index>=0;index--)
{
for(i=0;i<SeqCount;i++)
{
if(SmallSeq[index][0]>=BigSeq[i][0]&&(BigSeq[i][0]+BigSeq[i][1])>=(SmallSeq[i][0]+SmallSeq[i][1])) //完全包含
{
if((SmallSeq[index][0]-BigSeq[i][0]+SmallSeq[index][1])>=5&&(BigSeq[i][1]-SmallSeq[index][0]+BigSeq[i][0])>=5) //可分
{
temp=BigSeq[i][1];
BigSeq[i][1]=SmallSeq[index][0]-BigSeq[i][0]+SmallSeq[index][1];
BigSeq[SeqCount][0]=SmallSeq[index][0];
BigSeq[SeqCount][1]=temp-SmallSeq[index][0]+BigSeq[i][0];
SeqCount++;
break;
}
}
else if(SmallSeq[index][0]>BigSeq[i][0]&&(BigSeq[i][0]+BigSeq[i][1])<(SmallSeq[i][0]+SmallSeq[i][1])) //尾部交叉
{
temp=SmallSeq[index][0]+SmallSeq[index][1]-BigSeq[i][0]-BigSeq[i][1];
BigSeq[i][1]+=temp;
SmallSeq[index][1]-=temp;
}
else if(SmallSeq[index][0]<BigSeq[i][0]&&(BigSeq[i][0]+BigSeq[i][1])<(SmallSeq[i][0]+SmallSeq[i][1])) //首部交叉
{
temp=BigSeq[i][0]-SmallSeq[index][0];
BigSeq[i][0]=SmallSeq[index][0];
SmallSeq[index][0]=BigSeq[i][0]+temp;
BigSeq[i][1]+=temp;
SmallSeq[index][1]-=temp;
}
}
}
}
void ScanCards()
{
int i=0,bi=0,si=0,len=0,begin=-1;
int CardNum=0; //牌的数量
SeqCount=0;
for(i=0;i<CardClass;i++)
{
CardNum+=Card[i];
}
while(CardNum>0)
{ begin=-1;
for(i=0;i<CardClass;i++)
{
if(0==Card[i])
{
if(-1==begin) continue;
else
{
len=i-begin;
if(len>0)
{
if(len<5) //是小顺子
{
SmallSeq[si][0]=begin;
SmallSeq[si][1]=len;
si++;
}
else //是大顺子
{
BigSeq[bi][0]=begin;
BigSeq[bi][1]=len;
bi++;
}
}
begin=-1;
}
}
else
{
if(-1==begin) begin=i;
Card[i]--;
CardNum--;
}
}
if(-1!=begin)
{
len=CardClass-begin;
if(len>0)
{
if(len<5) //是小顺子
{
SmallSeq[si][0]=begin;
SmallSeq[si][1]=len;
si++;
}
else //是大顺子
{
BigSeq[bi][0]=begin;
BigSeq[bi][1]=len;
bi++;
}
}
}
}
SeqCount=bi;
Segmention(si);
}


void PrintCards()
{
cout<<"There are "<<SeqCount<<" Sequences!"<<endl;
int i=0,num;
for(i=0;i<SeqCount;i++)
{
num=BigSeq[i][0];
int j=0;
for(j=0;j<BigSeq[i][1];j++)
{
if(num>=0&&num<=8)
cout<<num+2<<",";
else
{
switch(num)
{
case 9: cout<<"J,";break;
case 10: cout<<"Q,";break;
case 11: cout<<"K,";break;
case 12: cout<<"A,";break;
}
}
num++;
}
cout<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//string cards("235677899@@JJQKKAA");
string cards("2356788999@@@JJKKAA"); //@为B,而B代表10 几个B连起来被百度屏掉了
int i;
for(i=0;i<CardClass;i++)
Card[i]=0;
for(i=0;i<SeqNum;i++)
{
BigSeq[i][0]=0; BigSeq[i][1]=0;
}
const char *p = cards.c_str();
Translate(p);
ScanCards();
PrintCards();
return 0;
}
...全文
135 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
没看过《有事您说话》那个小品吗。没事别揽自己力所不及的事,到头来终会压垮自己,还因误朋友的事而得罪朋友。朋友不只你一个,让他找别人吧。
MarkInOneDay 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 theforever 的回复:]

这种力工,没人愿意做的.别人可以指路,路还得你自己走.动不动就让人背你,难道还是小孩子吗?
[/Quote]

不好意思,我没学过C,有些东西实在看不懂,是我朋友非找我来帮忙,我也没办法。

烦请各位在闲暇时间帮个忙,算练练手了。
  • 打赏
  • 举报
回复
这种力工,没人愿意做的.别人可以指路,路还得你自己走.动不动就让人背你,难道还是小孩子吗?
MarkInOneDay 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zclmoon 的回复:]

是的呢,JS的语法和C差不多啦。。。突击下LZ就可以搞定了
[/Quote]
帮个忙啦…时间比较紧
AI应用技术 2010-11-04
  • 打赏
  • 举报
回复
是的呢,JS的语法和C差不多啦。。。突击下LZ就可以搞定了
MarkInOneDay 2010-11-04
  • 打赏
  • 举报
回复
没人要分么?

一定会结贴的
MarkInOneDay 2010-11-04
  • 打赏
  • 举报
回复
顶上来。。

哪位大牛晚上帮我改一下。

这儿还有100分。。

http://topic.csdn.net/u/20101104/13/51a2c504-aabc-4f7c-bb34-7f30f4892c2f.html?64167
knightzhuwei 2010-11-04
  • 打赏
  • 举报
回复

要不java区和C#区也去发一下?
小驴 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 markinoneday 的回复:]
引用 1 楼 knightzhuwei 的回复:
这个应该放到js区
会js的一般都会C 会C的不一定会JS

...js区也发了。。。。
[/Quote]

好吧
MarkInOneDay 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 knightzhuwei 的回复:]
这个应该放到js区
会js的一般都会C 会C的不一定会JS
[/Quote]
...js区也发了。。。。
knightzhuwei 2010-11-04
  • 打赏
  • 举报
回复

这个应该放到js区
会js的一般都会C 会C的不一定会JS

69,386

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧