題目:編寫函數int stat(int a[],int n,int c[][2])。a指向的數組中保存了由n個1位整數組成的數列(n為偶數)。函數從前至后依次將a數組中每兩個相鄰元素拼成一個不超過2位的整數,從而生成有n/2個元素組成的整數數列;統計該數列中不同整數各自出現的次數,并將統計結果保存到c指向的二維數組中。函數返回不同整數的個數。
#include<stdio.h>int stat(int a[],int n,int c[][2]){ int j,u,k,i,count; for(j = 0; j < n; j++) //將相鄰兩位數組合在一起 { u = 2 * j; //每次需要加2 c[j][0] = a[u] *10 + a[u + 1]; } for(k = 0; k < (n/2); k++) //依次對其中的數和之后的比較 { count = 1; if(c[k][0] == 100) //去除比較過相同的數字,因為組合數最多為兩位 continue; for(i = (k + 1); i < (n/2); i++) { if(c[k][0] == c[i][0]) //判斷兩數是否相等 { count++; //記錄相等數的個數并將其置為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]); //將數依次存入數組 stat(a,n,c); }
新聞熱點
疑難解答