C language one-dimensional array input unique number

 Basic code, C language is not good enough, most functions are not good enough to use

The first:

#include <stdio.h>

int main()
{
    int a[10];
    
    int i = 0;
    
    int m, m_copy;
    
    scanf("%d", &m);
    m_copy = m+1;
    
    while(1){
        
        if(m != m_copy){
            a[i]=m;
            i++;
            //m_copy保存上次m的值
            m_copy = m;
        }
        
        if(i==10)
            break;
            
        scanf("%d", &m);
        
    }
    
    for(int j=0; j<10; j++){
        printf("%d ", a[j]);
    }

}

 

The second: from Baidu knows that the      definition array a[10], receives the number entered by the keyboard, no matter what number the user enters, the array a only accepts the number that is not repeated (First_ Baidu Know (baidu.com)

#include<stdio.h>
#define N 10

int main() { 
    int a[N], n, i, b;
    n=0;
    
    while( n < N ){
        
        scanf("%d",&a[n]);
        
        b=0; 
        for(i = 0; i < n; i++)
            if( a[i] == a[n] ){
                b = 1;
                break;
                
            }
            
        if(b==0)
            n++;
    }


    for(i=0;i<N;i++)
        printf("%d ",a[i]);
        
}

 

 

Guess you like

Origin blog.csdn.net/sdaujz/article/details/110723685