필기시험 문제: 숫자 한 줄 을 입력 하고 쉼표 로 구분 하여 숫자 를 정렬 한 후 출력 합 니 다.
6133 단어 알고리즘
#include
#include
#include
//
//k
//count
int CountNum(char *arr,int k,int count)
{
int result=0;
int i=0;
while(count>0)
{
float ten=pow(10,i);
result+=(arr[k-1-i]-48)*ten;
i++;
count--;
}
return result;
}
// , num
//length arr
int ConverToNum(char *arr, int length, int *num)
{
int n=0;
int k=0;
int count_temp=0;
while(k<=length)
{
// , ,
if(arr[k]==','||arr[k]=='\0')
{
num[n]=CountNum(arr,k,count_temp);
n++;
count_temp=0;
}
else
{
count_temp++;
}
k++;
}
return n;
}
//
// ,
void sort(int *arr, int length)
{
int temp=0;
for(int i=0;ifor(int j=0;j1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
//
int DropRepeat(int *arr,int length, int *temp)
{
temp[0]=arr[0];
int i=1;
int k=1;
while(iif(arr[i]!=arr[i-1])
{
temp[k++]=arr[i];
}
i++;
}
return k;
}
int main()
{
char arr[100]={'\0'};
scanf("%s",&arr);//
int length=strlen(arr);
int num[100]={0};
int count_num=ConverToNum(arr, length, num);//
sort(num,count_num);//
int temp[100]={0};
int count_temp=DropRepeat(num,count_num,temp);//
int flag=0;
printf("%d ",temp[0]);
for(int i=1;i// ,
if(temp[i]!=temp[i-1]+1 || temp[i]+1!=temp[i+1])
{
printf("%d ",temp[i]);
}
}
getchar();
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Codility Lesson3】FrogJmpA small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.