2014-02-25 67 views
-1

問題被寫入代碼下面輸出屏幕需要幫助到格式輸出

#include <stdio.h> 
#include <conio.h> 
#include <string.h> 

main() 
{ 
    int i,j,x,n,count; 
    char no[100],no1[100]; 
    printf("Enter an array of numbers:- "); 
    scanf("%s",no); 
    strcpy(no1,no); 
    x = strlen(no); 
    for(i=0;i<x;i++) 
    { 
    count = 0; 
    for(j=0;j<x;j++) 
    { 
     if(no[i]==no1[j]) 
      count++ ; 
     } 
    printf("\n\nFrequency of %c is %d",no[i],count); 
    } 
    getch(); 
} 

輸出:

Enter an array of numbers:- 17485112 


Frequency of 1 is 3 

Frequency of 7 is 1 

Frequency of 4 is 1 

Frequency of 8 is 1 

Frequency of 5 is 1 

Frequency of 1 is 3 

Frequency of 1 is 3 

Frequency of 2 is 1 

/**所以這個問題是它顯示了每個的次數和的每個數字字符串。我想要的是它應該忽略重複的數字。 例如這裏1有頻率3和同一行(1的頻率是3)重複3次。 所以我不想重複。

首先,我做這

\\ if(no[i]!=no[i-1]) 
     printf("\n\nFrequency of %c is %d",no[i],count); 

它的工作原理,如果重複的數字是連續的,但是沒有其他

然後我試圖

\\for(n=0;n<i;n++) 
\\{ 
\\if(no[i]!=no[i-n]) 
    printf("\n\nFrequency of %c is %d",no[i],count); 

解決方案: -

我安排的字符串按升序排列,並用下面的東西

if(no[i]!=no[i-1]) 
      printf("\n\nFrequency of %c is %d",no[i],count); 

輸出:

輸入數字的陣列: - 1236665554447182999352000

頻率的0是3頻率1的

是3

2頻率的2是

3的頻率是2

頻率4是3

的5頻率是4

的6頻率是3

的7頻率爲1

的8頻率是1頻率的9

是3

+0

你最外層循環應該通過所有可能的數字(0〜9),而不是通過串的元素進行迭代。 –

+0

謝謝你們... 找到解決方案。問題解決了 – user3138350

回答

0

試試這個

x = strlen(no); 
int number_count[10] ={0}; 
int i=0,j=0; 
for(i=0;i<x;i++) 
{ 
    for(j=0;j<=9;j++) 
     if(no[i] -'0' ==j) 
     number_count[j]++; 
} 
for(j=0;j<=9;j++) 
    if(number_count[j] != 0) 
     printf("\n\nFrequency of %c is %d",j,number_count[j]); 
0
  1. 排序N [1]列表,使得

    n[i] = 1, 7, 4, 8, 5, 1, 1, 2 
    

    出現,

    n[i] = 1, 1, 1, 2, 4, 5, 7, 8 
    
  2. 現在使用次數來計算出現的次數,你可以做谷歌那。

  3. 最後使用,

    if(no[i]!=no[i-1]) 
    { 
        printf("\n\nFrequency of %c is %d",no[i],count); 
    } 
    

換句話說,使用二進制搜索算法。 http://en.wikipedia.org/wiki/Binary_search_algorithm

+0

好的答案!二進制搜索是。 – Code

0
main() 
{ 
int i,x,count; 
char numbers[100]; 
int noofoccurance[10]={0}; 
printf("Enter an array of numbers:- "); 
scanf("%s",numbers); 
x = strlen(no); 
for(i=0;i<x;i++) 
{ 
    a[no[i]-48] = a[no[i]-48]+1; 
} 
for(i=0;i<10;i++) 
{ 
    if[noofoccurance[i]!=0] 
    printf(\nFrequency of %d is %d",i,noofoccurance[i]) 
} 
getch(); 
} 
0

,你可以這樣做: 保持處理元素的新陣列和處理新的元素之前檢查是否存在新的數組元素。如果可用,則跳過該元素的過程。

0

試試這個

#include <stdio.h> 
    #include <conio.h> 
    #include <string.h> 

    main() 
    { 
    int i,j,x,n,count; 
    char no[100],no1[100]; 
    printf("Enter an array of numbers:- "); 
    scanf("%s",no); 
    strcpy(no1,no); 
    x = strlen(no); 
    for(i=0;i<x;i++) 
    { 
    count = 0; 
    for(j=0;j<x;j++) 
    { 
     if(no[i]==no1[j]) 
      { 
       count++ ; 
       no1[j] = 0.1; 
      } 
    } 
    if(count!=0) 
    printf("\n\nFrequency of %c is %d",no[i],count); 
    } 
    getch(); 
    }