題目:編寫函數(shù)int stat(int a[],int n,int c[][2])。a指向的數(shù)組中保存了由n個1位整數(shù)組成的數(shù)列(n為偶數(shù))。函數(shù)從前至后依次將a數(shù)組中每兩個相鄰元素拼成一個不超過2位的整數(shù),從而生成有n/2個元素組成的整數(shù)數(shù)列;統(tǒng)計(jì)該數(shù)列中不同整數(shù)各自出現(xiàn)的次數(shù),并將統(tǒng)計(jì)結(jié)果保存到c指向的二維數(shù)組中。函數(shù)返回不同整數(shù)的個數(shù)。
#include<stdio.h>int stat(int a[],int n,int c[][2]){	int j,u,k,i,count;	for(j = 0; j < n; j++)         //將相鄰兩位數(shù)組合在一起	{		u = 2 * j;                   //每次需要加2				c[j][0] = a[u] *10 + a[u + 1];   	}	for(k = 0; k < (n/2); k++)           //依次對其中的數(shù)和之后的比較	{		count = 1;		if(c[k][0] == 100)               //去除比較過相同的數(shù)字,因?yàn)榻M合數(shù)最多為兩位			continue;		for(i = (k + 1); i < (n/2); i++)        		{			if(c[k][0] == c[i][0])      //判斷兩數(shù)是否相等			{				count++;                  //記錄相等數(shù)的個數(shù)并將其置為100舍棄				c[i][0] = 100;			}		}		PRintf("%d   %d/n",c[k][0],count);	}}int main(){	int a[100],n,c[10][2],i;	printf("Please input the total:/n");	scanf("%d",&n);	printf("Please input the numbers:/n");	for(i = 0; i < n; i++)		scanf("%d",&a[i]);         //將數(shù)依次存入數(shù)組	stat(a,n,c);	}
新聞熱點(diǎn)
疑難解答