用C语言写出一道关于随机数的编程题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言写出一道关于随机数的编程题相关的知识,希望对你有一定的参考价值。

写一个程序:输入一个随机数,输出结果需要满足以下要求
1.求出它有多少位。
2.分别输出每一位数字。
3.将1~3的数字乘以3,4乘以2,5~9乘以1,例如1349等于3989,然后把得到的结果倒置输出。
4.输出其中没有重复过的数字。
5.并写出每个数字对应的英文字母,例如123等于onetwothree,假如有觉得难度不高的大神存在的话,可以写成one hundred and twenty three
程序要有条理地输出结果,求帮忙~~~

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"

#define NUMLEN 69
#define BigNum char *

void bigNumCpy(BigNum a, const BigNum b)//大数间复制

memcpy(a, b, NUMLEN);

int add(const BigNum a, const BigNum b, BigNum c)//大数加法

int i;
int t1 = 0, t2;
BigNum d = new char[NUMLEN];
for(i=0;i<NUMLEN;i++)

t2 = a[i] + b[i] + t1;
d[i] = t2 % 10;
t1 = t2 / 10;

bigNumCpy(c, d);
return t1;

void covertInt(int x, BigNum a)//将整数转换为大数

int i;
for(i=0;i<NUMLEN;i++)

a[i] = x % 10;
x /= 10;


int numLen(const BigNum a)//大数的位数

int i;
for(i = NUMLEN - 1; i >= 0 && a[i] == 0; i--);
return i+1;

int covert(BigNum a, const BigNum b)//将大数的顺序调换

char c[NUMLEN];
memset(c, 0, NUMLEN);
int len = numLen(b);
int i, j;
for(i = 0, j = len - 1; i < len; i++, j--) c[i] = b[j];
bigNumCpy(a, c);
return len;

void printBigNum(BigNum a, int mode = 0)//大数显示

int i;
int flag = 1;
for(i=NUMLEN-1;i>=0;i--)

if(flag == 1 && a[i] == 0) continue;
else flag = 0;
printf("%c", a[i] + 0x30);
if(mode) printf(" ");


void convert1(BigNum a, const BigNum b)//

char c[NUMLEN];
memset(c, 0, NUMLEN);
int i, len = numLen(a);
for(i=0;i<len;i++)

if(a[i]*3 < 10) c[i] = a[i] * 3;
else if(a[i]*2 <10) c[i] = a[i] * 2;
else c[i] = a[i];

bigNumCpy(a, c);

void outNorepeatNum(BigNum a)//输出没有重复的数字;

int count[10] = 0,0,0,0,0,0,0,0,0,0;
int i, len = numLen(a);
for(i=0;i<len;i++) count[a[i]] ++;
printf("没有重复的数字:");
for(i=0;i<10;i++) if(count[i] == 1) printf("%d ", i);
printf("\n");

void outEnNum(BigNum a)

const char enNum10[10][12] =
"zero ", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine ";
const char enNum20[10][12] =
"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ",
"fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen ";
const char enNum90[10][12]=
"", "", "twenty ", "thirty ", "fourty ",
"fifty ", "sixty ", "seventy ", "eighty ", "ninety ";
const char enNum100[] = "hundred ";
const char enNum1000[][20] =
"", "thousand ", "million ", "billion ", "trillion ",
"quadrillion ", "quintillion ", "sextillion ", "septillion ", "octillion ",
"nonillion ", "decillion ", "undecillion ", "duodecillion ", "tredecillion ",
"quattuordecillion ", "quindecillion ", "exdecillion ", "septendecillion ", "octodecillion ",
"novemdecillion ", "vigintillion ", "centillion ";
int len = numLen(a) - 1;
int m = (len) / 3 + 1;
int n;
int j, k = len;
for(j=m;j>=0;j--)

n = (len ) % 3;
n++;
len -= n;
if(n == 3)

printf("%s", enNum10[a[k]]);
if(a[k]) printf("%s", enNum100);
n--; k--;

if(n == 2)

if(a[k] > 1)

printf("%s",enNum90[a[k]]);
n--; k--;

else if(a[k] == 1)

printf("%s",enNum20[a[--k]]);
n -= 2; k--;

else k--; n--;

if(n == 1)

if(a[k]) printf("%s",enNum10[a[k]]);
k--;

if(j) printf("%s",enNum1000[j-1]);

printf("\n");

//void outCnNum(BigNum a, int mode = 0)
//
// const char num10[10][2][4] =
// "〇", "零","一", "壹","二", "贰","三", "叁","四", "肆",
// "五", "伍","六", "陆","七", "柒","八", "捌","九", "玖";
// const char num20[5][2][4] = "十", "拾","百", "佰","千", "仟","万", "万","亿", "亿";
// int len = numLen(a) - 1;
// int m = (len) / 8 + 1;
// int n, o, p;
// int i, j, k = len;
// for(j=m;j>=0;)
//
// n = (len ) % 8;
// len -= n + 1;
// if(n > 3) o = 2; p = 3;
// else o = 1; p = n;
// //p = n - 4;
// while(o>0)
//
// for(i=p;i>=0;i--)
//
// printf("%s", num10[a[k]][mode]);
// if(i) printf("%s", num20[i-1][mode]);
// k--;
//
// p = n - 4;
// o--;
// if(o>0) printf("%s", num20[3]);
//
// for(i = j-1;i>0;i--) printf("%s", num20[4][mode]);
// j--;
//
// printf("\n");
//
int main()

char a[NUMLEN];
char c;
int n;
c = getchar();
srand(time(NULL));
while(c != 'n')

n = ((rand() & 0xFFFF) << 16) | (rand() & 0xFFFF);
printf("随机数字:%d\n", n);
covertInt(n, a);
printf("逐个输出随机数字:");
printBigNum(a,1);
printf("\n");
printf("数字位数:%d\n", numLen(a));
convert1(a, a);
covert(a, a);
printf("逐个反向输出转换后的数字:");
printBigNum(a,1);
printf("\n");
covertInt(n, a);
outNorepeatNum(a);
outEnNum(a);
//outCnNum(a);
//outCnNum(a, 1);
c = getchar();

for(n=0;n<NUMLEN;n++) a[n] = rand()%10;
printf("%d位随机数字:\n", NUMLEN);
printBigNum(a);
printf("\n");
outEnNum(a);
//outCnNum(a);
return 0;
参考技术A 这么多问估计想写的不多, 我正在编程。顺便给你个思路吧
while(k>0)

m[i]=k%10;
k=k/10;
i++;

printf("一共有%d位\n",i);
printf("输出:");//这里是m[i-1]装着第一个数字比如说1349的1,i为判断出来的位数;
for(j=i-1;j>=0;j++)
printf("%d",m[j]);
printf("\n");

之后循环检测m[]中的每一个数字,在1~3的就*3,4就*2,其他不管,之后把数组在输出一次(正序倒叙随你便)
之后就用for判断有没有重复的数字(int a[],n,i,j,f=0,c=0)
for(j=0;j<i;j++)

for(n=j+1;n<i;n++)

if(m[j]!=m[n])
f++;

if(f==(i-1-j))
a[c++]=m[j];f=0;

for(j=0;j<i;j++)
printf("%d",a[j]);
printf("\n");

最后一个你就用个for+ switch 就搞定了
int kk=i;
for(j=0;j<i;j++)

switch(m[j])

case 1: printf("one ");break;
case 2: printf("two ");break;



case 9:printf("nine");break;

switch(k--)

case 3:输出 "千" break;
case 2:输出 "百"break;
case 1:输出 "十"break;


匆匆写的 看不懂或者哪错了 追问我就行了追问

我不很明白用for语句来判断无重复数字的那一段程序,初学~~,不过我还能明白你是想用双重for循环来进行排查比较,以及对于那个将程序倒置的问题我到现在还是不知道该去调用什么知识,因为对于这个不确定位数的随机数,我之前别的想法都不管用。还有~~~,我不知道该如何把这个模块化的函数组织起来,感觉很混乱。不知不觉又来了一个死循环~~~。请同志帮帮忙,耐心教导我这只菜鸟!!

追答

判断重复与否那里的意思是利用里面的循环判断某一位数字与其他的是否相同比如 12341 中先用第一个1 一次与 2341比较 如果都不同那么 代表开关的量f 就会等于一个值 那么就会启动a数组 把这个数字给a 如果有一个相同那么f 就达不到相应的开启if语句的量 就不会调用a[] 利用外面的for 第二次进入循环后 就是判断下一位数字 与之后的是否相同,我这个程序当时时间仓促写的有点错误 就是如果第一次判断的数字如果与第二个相同那么这个虽然不会赋给a 但是第二位数字在判断的时候由于只与之后的判断 而没与第一个数字比较所以就不会被排除了。 因此应该修改成 每次都与除了自己的每一位数字比较~~

关于倒置输出很简单 就是我给你的第一个程序, 那里不是有 while(k>0)

m[i]=k%10;//取余并赋给m[]
k=k/10;//比如说1234 经过这一句就会变为123.4 但是由于定义的是整型 所以自己变为123
i++;// 位数+1

么 这里 m[0] m[1] m[2]...依次装的是什么 自己想, 知道装的什么了 你就知道怎么把数字倒过来输出了

组织起来还不简单么, 你现在如果没学过函数编写 那你就全写在main里面 该定义的全定义好了 把这些程序依次写进去就好了啊~
【ps: 可怜没采纳我 我还这么详细解答。。。 有不懂再追问】

参考技术B

提供个思路吧,具体代码不是很想写= =!

    通过srand和rand结合使用产生随机数,用int型的变量存储;

    用itoa或者sprint将int型随机数转换为字符串。

    再利用atoi,解析字符串什么的功能,实现你要的逻辑功能。

感觉不是很难。。就是现在在研究学习ARM,不是很想写了。。

参考技术C 你是求程序代码还是求思路,如果是代码,恐怕没人愿意给你写,如果求思路,见下:
1、按字符串处理输入,并校验合法性,检查非法字符
2、统计位数,这个很简单吧,数数字符串长度就知道了
3、统计每个字符的数量,将数量为1的记录下来,供后续输出
4、按位将字符转为数字,然后按规则计算值,再转为字符,然后倒置
5、为每个数字字符设置一个英文名字的对照表,用数组就行,下标索引方式用字符值-"0"即可,你懂吧
6、至于大神级需求,请大神回答
参考技术D C语言很久没接触,VBS我就写得出。

c语言关于链表的一道题

6.链表的拆分和排序
题目描述
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据按照从小到大排序。
输入
第一行输入整数N;;
第二行依次输入N个整数。
输出
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。

示例输入

10
1 3 22 8 15 999 9 44 6 1001

示例输出

4 6
6 8 22 44
1 3 9 15 999 1001

#include<stdio.h>
#include<stdlib.h>

//整数单链表
struct numList

int num;
numList* next;
;

//函数原型声明
numList* create(int n);
int add(numList* head, int n);
void sort(numList* head);
void output(numList* head);

void main()

char* p;
p=NULL;
int count;
int evenCount = 0; //奇数个数
int oddCount = 0; //偶数个数
numList* list;
numList* evenList;
numList* oddList;
numList* pNode;
printf("输入整数个数及各整数值:\n");
scanf("%d",&count);

list = create(count);
evenList = (numList*)malloc(sizeof(numList));
evenList->next = NULL;
oddList = (numList*)malloc(sizeof(numList));
oddList->next = NULL;
pNode = list->next;
while(pNode != NULL)

if((pNode->num & 1) == 0)

//如果是偶数
evenCount++;
add(evenList, pNode->num);

else

//如果是奇数
oddCount++;
add(oddList, pNode->num);

pNode = pNode->next;

//对偶数和奇数链表排序
sort(evenList);
sort(oddList);

//输出
printf("%d %d\n",evenCount, oddCount);
output(evenList);
output(oddList);

/*************************************************
【函数名称】 create
【功能】 创建一个具有n个节点的单链表
【参数】 节点数
【返回值】 指向头节点的指针
*************************************************/
numList* create(int n)

numList* head;
numList* pCur;
numList* pNext;

head = (numList*)malloc(sizeof(numList));
if(head == NULL)

return NULL;

head->next = NULL;
pCur = head;
while(n>0)

pNext = (numList*)malloc(sizeof(numList));
if(pNext == NULL)

return NULL;

if(scanf("%d",&pNext->num)==0)
break;
pNext ->next = NULL;
pCur->next = pNext;
pCur = pNext;
n--;

return head;

/*************************************************
【函数名称】 add
【功能】 向链表末尾添加一个节点
【参数】 指向链表头节点的指针及添加元素的值
【返回值】 插入成功返回1,失败则返回0
*************************************************/
int add(numList* head, int n)

if(head == NULL)

return 0;

numList* p;
numList* pNewNode;
p = head;
while(p->next != NULL)

//得到尾既节点指针
p = p->next;

pNewNode = (numList*)malloc(sizeof(numList));
if(pNewNode == NULL)

return 0;

pNewNode->num = n;
pNewNode->next = NULL;
p->next = pNewNode;

return 1;


/*************************************************
【函数名称】 sort
【功能】 使用简单选择排序对节点元素升序排列
【参数】 指向链表头节点的指针及添加元素的值
【返回值】 无
*************************************************/
void sort(numList* head)

if(head == NULL)

return;

int temp;
numList* pStart;
numList* pCur;
for(pStart = head->next; pStart != NULL; pStart = pStart->next)

for(pCur = pStart->next; pCur != NULL; pCur = pCur->next)

if(pStart->num > pCur->num)

temp = pStart->num;
pStart->num = pCur->num;
pCur->num = temp;





/*************************************************
【函数名称】 output
【功能】 输出链表节点元素
【参数】 指向链表头节点的指针及添加元素的值
【返回值】 无
*************************************************/
void output(numList* head)

if(head == NULL)

return;

numList *p;
p = head->next;
while(p != NULL)

printf("%d ",p->num);
p = p->next;

printf("\n");
参考技术A //---------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

typedef struct node

int data;
struct node *next;
node;
typedef struct

node *head;
node *end;
int cnt;
list;

void prt_list(list *a)

node *t=a->head;
while (t)

printf("%d ",t->data);
t=t->next;

putchar('\n');

void list_insert(list *a,int n)

node *t=malloc(sizeof(node));
t->data=n;
t->next=NULL;
if (a->end==NULL) a->head=a->end=t;
else
a->end->next=t;
a->end=t;

++a->cnt ;

void pickup_insert(list *a,int d)

node *s,*t=malloc(sizeof(node));
t->data=d;
t->next=NULL;
if (a->head==NULL)
a->head=a->end=t;

else if (d<a->head->data)
t->next=a->head;
a->head=t;

else if (d>a->end->data)
a->end->next=t;
a->end=t;

else
s=a->head;
while (s&&d>s->next->data)


s=s->next;


t->next=s->next;
s->next=t;

++a->cnt;

void list_pickup(list *a,list *even,list *odd)

node *t;
for (t=a->head; t!=NULL; t=t->next)
if (t->data%2) pickup_insert(odd,t->data);
else pickup_insert(even,t->data);


void delete_list(list *a)

node *t;
for (t=a->head; a->head!=NULL; t=a->head)
a->head=t->next;
free(t);


int main(int argc, char* argv[])


list even=0,odd=0,lt=0;
int i,n,a;
scanf("%d",&n);
for (i = 0; i < n; i++)
scanf("%d",&a);
list_insert(<,a);

list_pickup(<,&even,&odd);
printf("%d %d\n",even.cnt,odd.cnt);
prt_list(&even);
prt_list(&odd);
delete_list(&even);
delete_list(&odd);
return 0;

//---------------------------------------------------------------------------

以上是关于用C语言写出一道关于随机数的编程题的主要内容,如果未能解决你的问题,请参考以下文章

用C++编程思想解决一道题

急求一道编程题

一道C语言编程题,求大神们帮帮忙,谢谢了。。

一道C或者C++编程题

求解一道Python编程题

一道C语言题,帮帮忙.