當前位置:編程學習大全網 - 編程軟體 - c語言編程 組數遊戲

c語言編程 組數遊戲

這個我以前寫過,不過不是用妳老師提供的思路:

C:

#include?<stdio.h>

#include?<stdlib.h>

int?MyStrCmp(const?void*?lhs,?const?void*?rhs)

{

const?char*?a?=?(const?char*)lhs;

const?char*?b?=?(const?char*)rhs;

while(1)

{

if(*a?!=?*b)

return?*a?-?*b;

while(*a?&&?*b?&&?*a?==?*b)

++a,?++b;

if(!(*a)?&&?!(*b))

return?0;

if(!*b)

b?=?(const?char*)rhs;

else?if(!*a)

a?=?(const?char*)lhs;

}

}

int?LexicalCmp(const?void*?lhs,?const?void*?rhs)

{

static?char?BufLhs[16],?BufRhs[16];

sprintf(BufLhs,?"%d",?*(const?int*)lhs);

sprintf(BufRhs,?"%d",?*(const?int*)rhs);

return?MyStrCmp(BufRhs,?BufLhs);

}

int?main()

{

int?n,?*seq;

scanf("%d",?&n);

seq?=?(int*)malloc(n?*?sizeof(int));

for(int?i?=?0;?i?<?n;?++i)

scanf("%d",?&seq[i]);

qsort(seq,?n,?sizeof(int),?LexicalCmp);

for(int?i?=?0;?i?<?n;?++i)

printf("%d",?seq[i]);

free(seq);

return?0;

}

C++:

#include?<iostream>

#include?<string>

#include?<algorithm>

#include?<vector>

#include?<iterator>

using?namespace?std;

bool?MyStrCmp(const?string&?s1,?const?string&?s2)

{

return?s1?+?s2?>?s2?+?s1;

}

int?main()

{

vector<string>?v;

string?str;

while(cin.peek()?!=?'\n'?&&?cin?>>?str?)

v.push_back(str);

sort(v.begin(),?v.end(),?MyStrCmp);

copy(v.begin(),?v.end(),?ostream_iterator<string>(cout));

}

  • 上一篇:求大家給點意見我是今年畢業的大專學生,學的是計算機應有技術學的不好,而且這個專業學的太雜了,求幫助
  • 下一篇:只會簡單sql,可以當DBA嗎?
  • copyright 2024編程學習大全網