如何随机产生26字母,计算个数并在指定两个字母单词出现时停止?

Y198895 2008-04-29 07:40:24
作业要求我们计算随机产生的字母个数,并在当出现at,is,he,up,on,we时停止并输出字母个数~~~~~·提示是根据1到26的数字来选择字母~~~~帮帮忙~~~不懂~~~
...全文
197 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
我也想了解,谢谢LZ.
rushman 2008-04-30
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANDCHAR (rand() % 26 + 'a')
#define CHARINDEX(c) (((unsigned)(unsigned char)(c - 'a')) % 26)

void main(void){
// 查表判断 at,is,he,up,on,we
const char flags[] = {'t', 0, 0, 0, 0, 0, 0, 'e', 's', 0, 0, 0, 0, 0, 'n', 0, 0, 0, 0, 0, 'p', 0, 'e', 0, 0, 0};
char c, lc;
signed count;

srand((unsigned)time(NULL));
printf("%d\n", CHARINDEX('b'));
for(count = 0, c = RANDCHAR, lc = 'b'; flags[CHARINDEX(lc)] != c; lc = c, c = RANDCHAR, count++){
if(count < 0)break;
}
printf("\n\nTotal : %d\n", count);
}
iwantfat 2008-04-29
  • 打赏
  • 举报
回复
4楼说的很对,考虑不周全

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

#include "string.h"
#include <stdio.h>

/*65-90 97-122*/

int Generate()
{
static int num = 0;
static int rand1;
static int rand2;
srand( (unsigned)time( NULL ) );
while(1)
{
rand1 = ((int) rand() % (int) 26 );
num++;

if(rand1 == 'a'-97 )
{
rand2 =rand1;
continue;
}
if((rand1 == 't'-97)) //其他组合类似
{
printf("%c%c",rand2+97,rand1+97);
break;
}

}
return num;
}

void main()
{
int m = Generate();
printf("\n%d",m);
}
Erorr 2008-04-29
  • 打赏
  • 举报
回复
楼上两位只能输出偶数吧
Erorr 2008-04-29
  • 打赏
  • 举报
回复

#include <stdlib.h>
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
int i = 0;
char pre = ' ';
char now = ' ';

int n = 0;
srand( (unsigned)time( NULL ) );

while (1)
{
i = rand() % 26;

pre = now;
now = (char) (i + 97);
printf("%c", now);

if ((pre == 'a' && now == 't')
|| (pre == 'i' && now == 's')
|| (pre == 'h' && now == 'e')
|| (pre == 'u' && now == 'p')
|| (pre == 'o' && now == 'n')
|| (pre == 'w' && now == 'e'))
{
break;
}

n++;
}

printf("\ncharacters: %d\n", n);

return 0;
}
iwantfat 2008-04-29
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

#include <stdio.h>

/*65-90 97-122*/

int Generate()
{
static int num = 0;
int rand1;
int rand2;
srand( (unsigned)time( NULL ) );
while(1)
{
rand1 = ((int) rand() % (int) 26 );
num++;
rand2 = ((int) rand() % (int) 26 );
num++;
if((rand1 == 'a'-97 ) && (rand2 == 't'-97)) //其他组合类似
{
printf("%c%c",rand1+97,rand2+97);
break;
}
}

return num;

}

int _tmain(int argc, _TCHAR* argv[])
{
int i =Generate();
printf("%d",i);
return 0;
}

-----------------------------vs2005
baihacker 2008-04-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAKE_WORD(a, b) (((a)-'a')*26+(b)-'a')
int main()
{
int a, b;
int dest[]={MAKE_WORD('a','t'), MAKE_WORD('i','s'), MAKE_WORD('h','e'),
MAKE_WORD('u','p'),MAKE_WORD('o','n'), MAKE_WORD('w','e')};
char* stb[]={"at","is","he","up","on","we"};
int i, flag = 1, n = 0;

srand(time(NULL));
while (flag)
{
a = rand()%26, b = rand()%26;
++n;
printf("%c%c\n", (a&0xff)+'a', (b&0xff)+'a');
for (int i = 0, it = a*26+b; i < sizeof(dest)/sizeof(dest[0]); ++i)
if (dest[i] == it)
{
flag = 0;
break;
}
}
printf("%d\n", n);
return 0;
}

70,037

社区成员

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

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