c语言问题

Useasingle-subscriptedarraytosolvethefollowingproblem.Readin20numbers,eachofwhichisbe... Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. 展开
 我来答
flying_search
推荐于2021-02-08 · TA获得超过131个赞
知道答主
回答量:207
采纳率:0%
帮助的人:0
展开全部
#include <math.h>
#include <stdio.h>
#define MINRANGE 10

#define MAXRANGE 100

#define FIRST_SLOT 32

#define SECOND_SLOT 64

int rvCheckNum(unsigned int rvSource, unsigned int rvNum);

unsigned int rvAddNum(unsigned int rvSource, unsigned int rvNum);

int main()

{

unsigned int rvArray[3] = {0, 0, 0};

unsigned int rvNum;

unsigned int rvRelativeOffset;

int rvFlag;

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

{

printf("Enter the number (10..100 inclusive) :");

scanf("%d", &rvNum);

if(rvNum < MINRANGE || rvNum > MAXRANGE)

{

printf("Number Out of Bound\n");
continue;

}

if(rvNum - MINRANGE < FIRST_SLOT )

{

rvRelativeOffset = rvNum - MINRANGE;

rvFlag = rvCheckNum(rvArray[0], rvRelativeOffset);

if(!rvFlag)

{

printf("%d already Present\n", rvNum);

}

else

{

printf("the number you just read is: %d\n", rvNum);
rvArray[0] = rvAddNum(rvArray[0], rvRelativeOffset);

}

}

else if(rvNum - 10 < SECOND_SLOT)

{

rvRelativeOffset = rvNum - FIRST_SLOT - MINRANGE;

rvFlag = rvCheckNum(rvArray[1], rvRelativeOffset);

if(!rvFlag)

{

printf("%d already Present\n", rvNum);

}

else

{

printf("the number you just read is: %d\n", rvNum);
rvArray[1] = rvAddNum(rvArray[1], rvRelativeOffset);

}

}

else

{

rvRelativeOffset = rvNum - SECOND_SLOT - MINRANGE;

rvFlag = rvCheckNum(rvArray[2], rvRelativeOffset);

if(!rvFlag)

{

printf("%d already Present\n", rvNum);

}

else

{
printf("the number you just read is: %d\n", rvNum);

rvArray[2] = rvAddNum(rvArray[2], rvRelativeOffset);

}

}

}
return 0;

}

int rvCheckNum(unsigned int rvSource, unsigned int rvNum)
{

unsigned int rvTemp;

unsigned int rvComp = (unsigned int)pow(2, sizeof(int)*8 - 1);

rvTemp = rvSource << rvNum ;

if(rvTemp >= rvComp)

return 0;

return 1;

}

unsigned int rvAddNum(unsigned int rvSource, unsigned int rvNum)
{

unsigned int rvMask = (unsigned int)pow(2,(sizeof(unsigned int)*8 - rvNum -1));

rvSource = rvSource | rvMask;

return rvSource;

}
//数组长度3
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yipian_yezi
2007-11-27 · TA获得超过191个赞
知道小有建树答主
回答量:616
采纳率:0%
帮助的人:0
展开全部
int i,j,x,arr[10]={0},eq;
for (i=0;i<=20;i++)
{
scanf("%d",&x);
while (x>100 || x<10)scanf("%d",&x);//x在10~100
for(j=0;j<i;j++)
if (x==a[j])break; //有重复值跳出
if (i==j) a[i]=x;//没有重复值
else print(" duplicate data:%d",x);
//为0 的,表示那时输入数据有重复,题目没说怎么处理?你可以写个函数,再把为0的,全部放在后面
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
危献e3
2007-11-28 · TA获得超过2053个赞
知道大有可为答主
回答量:2738
采纳率:0%
帮助的人:1944万
展开全部
数组长度三

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

#define MINRANGE 10
#define MAXRANGE 100
#define FIRST_SLOT 32
#define SECOND_SLOT 64

int rvCheckNum(unsigned int rvSource, unsigned int rvNum);
unsigned int rvAddNum(unsigned int rvSource, unsigned int rvNum);

int main()
{
unsigned int rvArray[3] = {0, 0, 0};
unsigned int rvNum;
unsigned int rvRelativeOffset;
int rvFlag;
for (int i = 0; i<20; i++)
{
printf("Enter the number (10..100 inclusive) :");
scanf("%d", &rvNum);
if(rvNum < MINRANGE || rvNum > MAXRANGE)
{
printf("Number Out of Bound\n");
i--;
continue;
}
if(rvNum - MINRANGE < FIRST_SLOT )
{
rvRelativeOffset = rvNum - MINRANGE;
rvFlag = rvCheckNum(rvArray[0], rvRelativeOffset);
if(!rvFlag)
{
printf("%d already Present\n", rvNum);
}
else
{
printf("the number you just read is: %d\n", rvNum);
rvArray[0] = rvAddNum(rvArray[0], rvRelativeOffset);
}
}
else if(rvNum - 10 < SECOND_SLOT)
{
rvRelativeOffset = rvNum - FIRST_SLOT - MINRANGE;
rvFlag = rvCheckNum(rvArray[1], rvRelativeOffset);
if(!rvFlag)
{
printf("%d already Present\n", rvNum);
}
else
{
printf("the number you just read is: %d\n", rvNum);
rvArray[1] = rvAddNum(rvArray[1], rvRelativeOffset);
}
}
else
{
rvRelativeOffset = rvNum - SECOND_SLOT - MINRANGE;
rvFlag = rvCheckNum(rvArray[2], rvRelativeOffset);
if(!rvFlag)
{
printf("%d already Present\n", rvNum);
}
else
{
printf("the number you just read is: %d\n", rvNum);
rvArray[2] = rvAddNum(rvArray[2], rvRelativeOffset);
}
}
}
return 0;
system("pause");
}

int rvCheckNum(unsigned int rvSource, unsigned int rvNum)
{
unsigned int rvTemp;
unsigned int rvComp = (unsigned int)pow(2, sizeof(int)*8 - 1);
rvTemp = rvSource << rvNum ;
if(rvTemp >= rvComp)
return 0;
return 1;

}

unsigned int rvAddNum(unsigned int rvSource, unsigned int rvNum)
{
unsigned int rvMask = (unsigned int)pow(2,(sizeof(unsigned int)*8 - rvNum -1));
rvSource = rvSource | rvMask;
return rvSource;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
奇妙百货铺
2007-11-27 · TA获得超过1472个赞
知道小有建树答主
回答量:605
采纳率:0%
帮助的人:580万
展开全部
用了bits来存状态,数组长度只是4。
#include <stdio.h>
int main(){
unsigned int mask[4] = {0,0,0,0}, num, i, e = 1;
for (i = 0; i < 20; ++i) {
scanf("%d", &num);
if(num < 10 || num > 100) {
continue;
}
if(0 == (mask[num/32] & (e <<(num - 32 * (num / 32) ) ) ) ){
printf("%d\n", num);
}
mask[num/32] |= e <<(num - 32 * (num / 32) );
}
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式