11.8 one-dimensional array

https://www.cnblogs.com/zpcdbky/p/5857656.html

sizeof(arr)/sizeof(arr[0])

https://blog.csdn.net/daijinghui512/article/details/52104534

http://www.cocoachina.com/articles/53120

 

 

 

1. Enter a positive integer n ( assuming n ≦ 100 ) , and enter the n integer into integer array a and determined that n average value of integers.

#include <stdio.h>
int main()
{
	int i,n,sum;
	int a[n];
	double ave;
	sum=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	ave=sum*1.0/n;
	printf("%.2f",ave);
}

 

 

* 2 insertion sort - an already existing array row good sequence, now enter a number, required by law insert it into the original sorted array.

https://blog.csdn.net/weixin_42859280/article/details/84996712

#include <stdio.h>
int main()
{
int a[10],i,n;
for(i=0;i<9;i++)
scanf("%d",&a[i]);
scanf("%d",&n);
i=8;
 while(i>=0 && a[i]>n)
{
a[i+1]=a[i];
i--;
}
i++;
a[i]=n;
for(i=0;i<9;i++)
printf("%d", a[i]);
printf("%d",a[9]);
return 0;
}


 

3. The value of an array in reverse order to re-store. For example, the original sequence is 1,2,3,4,5,6,7,8. Requirements changed to 8,7,6,5,4,3,2,1. Note: not just in reverse order output is stored in reverse order.

 

#include<stdio.h>
#define N 8 
int  main()
{
	int t,i; 
    int a[N]={1,2,3,4,5,6,7,8};
    for(i=0;i<N/2;i++)
    {
        t=a[i];
        a[i]=a[N-i-1];
        a[N-i-1]=t;          
    }                 
   for(i=0;i<N;i++)
   {
      printf("%d\t",a[i]);        
   }               
    return 0;
}

* 4 decimal number can write a program to convert an arbitrary hexadecimal number (e.g., 8, 16 hex), the requirements: to enter a decimal number and to convert the base band (2 or 8, or from the keyboard 16 ) as input, the output 1111011b 123,2; 14,8 input output 173O; inputs 14 and 16, the output 7BH.

#include<stdio.h>
#include<math.h>
char b[1000];
char a[16]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int main()
{
    int i,n,r;//十进制的整数n,转化成r进制的数;
    while(~scanf("%d %d",&n,&r))
    {
    	int i=0,flag=0;
    if(n<0)                 //判断负数
        flag=1;
    while(n>0)
    {
         b[i++]=a[n%r];
         n/=r;
    }
    i--;
    if(flag==1)
        printf("-");
    for(; i>=0; i--)
    {
        printf("%c",b[i]);
    }
    printf("\n");
    }
    return 0;
}

 

 

 

 

5. Enter a positive integer n , and the input n integers into integer array A , and then enter an integer X , which is a value to find several elements of the array. If this number is not in the array, "no such number" is output. (Binary search easiest)

Why N before and after the program is running will have an impact?

 N must first determine the scope of the definition to otherwise error-prone 

1.顺序查找
#include<stdio.h>
int main()
{
	
	int N,i,x;
	int a[N];
	scanf("%d",&N);
	printf("请输入%d个数:\n",N);
	for(i=0;i<N;i++)
	{
		scanf("%d",&a[i]);
	 } 
	 printf("请输入要查找的数:");
	 scanf("%d",&x);
	 for(i=0;i<N;i++)
	 {
	 	if(a[i]==x)
	 	{
	 		printf("%d是a[%d]",x,i);
	 		break;
		} 
	 }
	 if(i==N)
	printf("%d没有找到\n",x);
 } 

2.二分查找法
#include<stdio.h>
int main()
{
	int N,i,x,n,top,bot,mid;
	int a[N];
	scanf("%d",&N);
	printf("请输入%d个数:\n",N);
	for(i=0;i<N;i++)
	{
		scanf("%d",&a[i]);
	 } 
	 printf("请输入要查找的数:");
	 scanf("%d",&x);
	 top=0;bot=N-1;
	 while(top<=bot)
	 {	
	 	mid=(top+bot)/2;
	 	if(x==a[mid])
	 	{
	 			printf("%d是a[%d]",x,mid);
	 			break;
		 }
	 	else if(x<a[mid])
	 		bot=mid-1;
	 	else if(x>a[mid])
	 		top=mid+1;
	  } 
	  if(top>bot)
	  printf("%d没有找到",x);
 } 

 

 

 

 

6. The two arrays in ascending order merging, the combined array should be arranged in order from small to large, such as an array a elements are 2,6,12,39,53,89; array b the elements 1,3,6,10,35; array element is combined 1,2,3,6,6,10,12,35,39,53,99. (Self given array)

https://blog.csdn.net/weixin_43424154/article/details/84574964

#include<stdio.h>
int main()
{  int str1[6]={2,6,12,39,53,89};//两个升序排放的数组
    int str2[5]={1,3,6,10,35};
    int c[11];//用来存放两个数组
    int i,j,t,f,x;
    for(i=0;i<6;i++)
        c[i]=str1[i];
    for(j=0;j<5;j++,i++)
        c[i]=str2[j];
   for(j=0;j<11;j++)
        for(i=0;i<10-j;i++)
            if(c[i]>c[i+1])
           {
            t=c[i];
            c[i]=c[i+1];
            c[i+1]=t;
           }
    for(i=0;i<11;i++)
        printf("%d\t",c[i]);
        return 0;
}


 

 

* 7 how to verify whether the order number of elements, such as arrays. A is 1,3,5,7,9,10,11 elements, the elements in an ordered array; if the element is 1,5,3 , 7,9,8,11, the random elements in the array.

#include<stdio.h>
int main()
{
    int i,flag=1;
    int a[10]={0};
    for(i=0;i<5;i++)
    {
        scanf("%d",&a[i]);
    }
    if(a[0]<a[1])//第一个数比第二个数小 
    {
    	for(i=1;i<4;i++)
    {
        if(a[i]>a[i+1])//前一个数比后一个数大 
        {
        	flag=0;
        	break;
}

    }  
} 
else//第一个数比第二个数大 
    {
    	for(i=1;i<4;i++)
    {
        if(a[i]<a[i+1])//前一个数比后一个数小 
        {
        	flag=0;
        	break;
}
    }  
} 
if(flag==0)
    printf("无序");
else
printf("有序");	 
}

 

 

*8.输入一个正整数n(1<n≤10),再输入n个整数存入数组a;然后输入一个正整数m(1<m≤10),输入m整数存入数组b,找出只出现其中一个数组中的所有元素、找出两个数组共有的元素。

https://blog.csdn.net/hjh123668/article/details/102845203

https://blog.csdn.net/yu876876/article/details/79030106

https://blog.csdn.net/windyJ809/article/details/79679473

https://blog.csdn.net/Bob__yuan/article/details/84350152

#include<stdio.h>
int main()
{
	int k=0,s=0,n,i,x,j,f,m,a[10],b[10],c[10],d[10];
	scanf("%d,%d",&n,&m);
	printf("请输入数组a:");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	printf("请输入数组b:");
	for(j=0;j<m;j++)
	{
		scanf("%d",&b[j]);
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(a[i]==b[j])
			{
				c[k++]=b[j];
				break;
		   }
		}
	}
	for(i=0;i<n;i++)
	{
		for(x=0;x<k;x++)
		{
			if(a[i]==c[x])
			{
				break;
			}
			else
				if(x==k-1)
					d[s++]=a[i];
		}
	}
	for(j=0;j<m;j++)
	{
		for(x=0;x<k;x++)
		{
			if(b[j]==c[x])
			{
				break;
			}
			else
				if(x==k-1)
					d[s++]=b[j];
		}
	}
	for(x=0;x<k;x++)
	printf("相同=%d ",c[x]);
	printf("\n");
	for(x=0;x<s;x++)
	printf("特异点=%d ",d[x]);
}

*9.编写程序,统计一个整数序列中出现次数最多的整数。要求输出该整数及其出现的次数。


https://blog.csdn.net/jiangxiaoshan123/article/details/81701505

https://blog.csdn.net/AA11224488/article/details/79872288

https://blog.csdn.net/LMY_go/article/details/41632119

#include <stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	int a[n];
	int b[n];
	for(int i=0;i<n;i++)//数组的初始化 
	{
		scanf("%d",&a[i]);
		b[i]=1;
	}
	for(int j=0;j<n;j++)
	{
		if(b[j]==1)//如果这个数字之前没有记录过 
		{
			for(int k=j+1;k<n;k++)
			{
				if(a[j]==a[k])//出现相同的字母 
				{
					b[j]++;
					b[k]=b[j];
				}
			} 
	    }
	}
	int cnt=b[0];
	int max=a[0];
	for(int kk=1;kk<n;kk++)
	{
		if(b[kk]>cnt)
		{
			cnt=b[kk];//找到最大的出现次数 
			max=a[kk];
		}
	}
	printf("%d %d",max,cnt);
	return 0;
}

 

发布了57 篇原创文章 · 获赞 27 · 访问量 1万+

Guess you like

Origin blog.csdn.net/ao_mike/article/details/102978791