1 /*
2 * Main.c
3 * E7-數組-07. 求一批整數中出現最多的個位數字
4 * Created on: 2014年8月22日
5 */
6
7 #include <stdio.h>
8
9 int main(void){
10
11 int array[1000],temp[10]={0};
12 int max=0;
13 int i,N;
14
15 scanf("%d",&N);
16 getchar();
17 for(i=0;i<N;i++){
18 scanf("%d",&array[i]);
19 }
20
21 for(i=0;i<N;i++){
22 while(array[i]){
23 temp[array[i]%10]++;
24 array[i]/=10;
25 }
26 }
27
28 for(i=0;i<10;i++){
29 if(max<temp[i])
30 max=temp[i];
31 }
32
33 printf("%d:",max);
34 for(i=0;i<10;i++){
35 if(temp[i]==max)
36 printf(" %d",i);
37 }
38 printf("\n");
39
40 return 0;
41 }
這個題目參考了一位網友的C++代碼,經過測試發現,還是C語言的執行效率略高。
這是我直觀的感受,有時候會用不同的語言、不同的算法去做對比,結果發現總是C的內存小、時間短。當然,Java的內存和時間消耗要大上30多倍!

題目鏈接:
http://pat.zju.edu.cn/contests/basic-programming/%E6%95%B0%E7%BB%84-07
參考:
http://www.cnblogs.com/gnodidux/p/3822761.html
(特別喜歡這段代碼,簡潔、精煉!)
.
代碼在txt文檔中編寫,有錯誤處自行修改下。
int num = new int[]{1,2,3,1};
int num2 = new int[num.length];
int b = 0;
for(int i = 0; i < num.length;i++)
{
int a = 0;
for(int j = 0; j < num.length;j++)
{
if(num[i] == num[j])
{
a++;
}
}
num2[i] = a;
}
for(int i = 0; i < num.length;i++)
{
for(int j = 0; j < num.length;j++)
{
if(num[i] < num[j])
{
c = num[j];
}
}
}
messageboxs.show("數組中最大的是" + c)
假設在數組a中:
dim b(),c()
n=-1
for i=lbound(a) to ubound(a)
f=false
if n>=0 then
for j=0 to n
if a(i)=b(j) then c(i)=c(i)+1:f=true:exit for
next
end if
if not f then
n=n+1
redim preserve b(n)
b(n)=a(i)
redim preserve c(n)
c(n)=1
end if
next
for i=0 to n
if max<c(i) then max=c(i):j=i
next
print "出現最多的數=";b(j)
print "出現次數=";c(j)