/*****************************************************
copyright (C), 2016-2017, Lighting Studio. Co., Ltd.
File name:
Author:王 Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
int stat(int a[],int n,int c[][2])
{
int i,j,k,flag;
int m = 0;
for(i = 0;i < n;i += 2)
{
a[j++] = a[i]*10 + a[i+1]; // 兩個數合併爲一個數
}
for(i=0;i<n/2;i++)
{
flag = 0;
for(j = 0;j<=i;j++)
{
if(a[i] == c[j][0]) // 判斷所合併的數是否和之前的數一致,相同時 計數器 加1 ,並且關閉與其他數的比較
{
c[j][1]++;
flag = 1;
}
}
if(flag == 0) // 如果合併的數與之前所有的數都不相同,那麼將它存入數組C 中,並給定個數爲 1
{
c[m++][0] = a[i];
c[m-1][1] = 1;
}
}
for(i=0;i<m;i++)
printf("%d : %d \n",c[i][0],c[i][1]); // 輸出合併後各不相同的數,並輸出其個數
}
int main()
{
int a[10]={1,2,1,2,5,6,7,8,9,1};
int c[5][2];
stat(a,10,c);
return 0;
}
編寫函數int stat(int a[],int n,int c[][2])。a指向的數組中保存了由n個1位整數組成的數列(n爲偶數)。函數從前至後依次將a數組中每兩個相鄰元素拼成一個不超過2位
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.