2014-11-20 4 views
0
int Rearrange(int a) 
{ 
    long int b,j,i=0,num=0,count=0,arr[100]; 
    while(a>0) 
    { 
     b=a%10;a=a/10; 
     arr[i]=b; 
     i++; 
     count ++; 
    } 
    j=count; 
    for(i=0;i<=count/2;i++) 
    { 
     t=arr[i]; 
     arr[i]=arr[count-i-1]; 
     arr[count-i-1]=t; 
     count--; 
    } 
    for(i=0;i<j;i+=2) 
    { 
     num=num*10 + arr[i]%10; 
    } 
    return num; 
} 

주어진 숫자의 대체 숫자를 인쇄하는 c rearrange에 함수를 쓰고 싶습니다. 예를 들어숫자를 허용하고 대체 숫자를 인쇄하십시오.

:

input:- 12345 
output:- 135 

당신에게 감사

+0

숫자는 한 자리 숫자 밖에되지 않습니다. 어떻게 대체 숫자를 사용할 수 있습니까? –

답변

0

귀하의 for 루프에 결함이 있습니다.

for(i=0;i<=count/2;i++) 
{ 
    int t=arr[i]; 
    arr[i]=arr[j]; /* Use j */ 
    arr[j]=t; /* Use j */ 
    /* count--; Dont decrement */ 
    j--; 
} 
for(i=0;i<count;i+=2) /* Should be count */ 
{ 
    num=num*10 + arr[i]%10; 
} 

Demo

해결하기위한 많은 다른 방법이있을 수 있지만, 난 그냥 생각 프로세스의 접근 방식이 제대로 구현 될 수있는 방법을 보여주고 싶은 님의 사람들을 변경합니다.

+0

여전히 mah를 얻지 못함 대답 ....... –

+0

당신은 [데모] (http://ideone.com/fork/gz48PD) –

+0

데모에서 일하는 고맙습니다 .....하지만 내 문제가 있다고 생각합니다. 터보 C 컴파일러 –

1

간단한 문제를 복잡하게하는 이유는 무엇입니까?

다른 방법이 마음에 들지 않으면 아래 코드를 확인하십시오.

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

int main() 
{ 
    int input = 0; 
    int len = 0; 
    int i = 0; 
    char sinput[64] = {0, }; 

    printf("Enter the number :"); 
    scanf("%d", &input); 
    sprintf(sinput, "%d", input); 
    len = strlen(sinput); 

    printf("Output : "); 
    for (i = 0; i < len; i+=2) 
    { 
     printf("%c\t", sinput[i]); 
    } 
    printf("\n"); 
    return 0; 
} 

샘플 I/O :

[[email protected] temp]$ ./a.out 
Enter the number :123456 
Output : 1 3 5 
[[email protected] temp]$ 
+0

thanx 많은 선생님 ...... 나는이 접근 방법을 시도했지만 ..... 위의 하나를 시도하고 .....하지만 내 길을 벗어나 출력을 얻지 못하는 것 –

0

코드 문제에서 첫 번째 for 루프가 있습니다. 아래 코드를 확인하십시오.

int Rearrange(int a) 
{ 
    long int b = 0, j = 0, i = 0, num = 0, count = 0, arr[100]; 

    while (a > 0) 
    { 
     b = a % 10; a = a/10; 
     arr[i] = b; 
     i++; 
     count++; 
    } 
    j = count; 
    for (i = 0; i < count/2; i++) // Condition is problematic 
    { 
     long int t = arr[i]; 
     arr[i] = arr[count-i-1]; 
     arr[count - i - 1] = t; 
     // count--; // this is problamatic. 
    } 
    for (i = 0; i < j; i += 2) 
    { 
     num = num * 10 + arr[i] % 10; 
    } 
    return num; 
} 
관련 문제