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.
展开
4个回答
展开全部
#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
#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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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的,全部放在后面
}
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的,全部放在后面
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数组长度三
#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;
}
#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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用了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;
}
#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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询