c语言。数组的问题。急!

来源:百度知道 编辑:UC知道 时间:2024/06/05 08:49:34
题目是这样的: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.
我的问题是:怎样检验输入的数与以前输入过的数是否相同呢?用什么方法呢?
int main()
{
int nums[20];
int i;
printf("If the first number you type is not a right number, it will output the number 0.\n");

for(i=0;i<20;i++)
{ int counter=1;
printf("Please type in a number within the interval [10,100]:\n");
scanf("%d",&nums[i]);
rule=nums[0];

if(i==0)
printf("The number you have just typed is %d\n",nums[i]<100&&nums[i]>10?nums[i]:0);
else if(nums[i]<100&&nums

怎样检验输入的数与以前输入过的数是否相同呢?用什么方法呢?

以下方法可以参考::

我用伪代码写拉
//int num[20]
for(i=0;i<20;i++)
{
print("input number");
scanf("%d",num[i]);
//以下是判断
int temp=0;
for(int j=0;j<i;j++)
{
if(num[j]==num[i])
temp=1;
}

}

如果temp为1就表相同
其实就是在已经输入的数据中来一次循环查找拉

问题是用最小的数组来完成。你用的是大小为20的数组,已经最大了!!

若不用函数并且不考虑输入数据的范围错误,可以参考以下程序:

#include<stdio.h>
int main()
{
int nums[20];
int i;
printf("If the first number you type is not a right number, it will output the number 0.\n");

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

printf("Please type in a number within the interval [10,100]:\n");
scanf("%d",&nums[i]);

for (int j=i-1; j>=0; j--)
{
if (nums[i]==nums[j])
{
printf("The number you have just typed is %d\n",nums[j