#include <stdio.h>
#include<string.h>
#define len 1000
struct Num{
char ch[len]; //儲存原始字串
char simplify[len];// 簡化字串(如0001230=1230)
int unit[len]; //換算單位大小(包含負號)
int size; //字串size (字串為負時,加上負號
int rank; //總結比較各size的大小
};
void Sorting(struct Num *ary,int a,int b){//比較相同size字串的大小(如-111 與 -112)後者較小
int i,j,s;
s=strlen(ary[a].simplify);
for(i=0;i<s;i++){
(ary[a].simplify[i]>ary[b].simplify[i])?ary[a].rank++:ary[b].rank++;
break;
}
}
void Ass_Unit(struct Num *ary,int n){//保留ch 簡化至simplify 計算後得到size長度(可為負數)
int num=0;
int i,j=0;
if(ary[n].ch[0]=='0'&&ary[n].ch[1]=='\0')ary[n].simplify[0]='0';
for(i=0;ary[n].ch[i]!='\0';i++){
if(ary[n].ch[i]=='-'){
ary[n].simplify[0]='-';
j++;
continue;
}
else if(ary[n].ch[i]>='1'&&ary[n].ch[i]<='9')break;
}
for(;ary[n].ch[i]!='\0';i++)
ary[n].simplify[j++]=ary[n].ch[i];
ary[n].size=strlen(ary[n].simplify);
if(ary[n].simplify[0]=='-')ary[n].size-=ary[n].size*2-1;
for(i=0,j=0;ary[n].simplify[i]!='\0';i++){
if(ary[n].simplify[0]=='-'&&ary[n].simplify[i+1]!='\0'){
ary[n].unit[j]-=ary[n].simplify[i+1]-'0';
j++;
}
else {
ary[n].unit[j]+=ary[n].simplify[i]-'0';
j++;
}
}
return;
}
int main(){
int n,i,j;
char c;
while(scanf("%d%c",&n,&c)!=EOF){
struct Num *ary=malloc(n*sizeof(struct Num));
for(i=0;i<n;i++){//取N組側資傳送函數
gets(ary[i].ch);
Ass_Unit(ary,i);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){//比較字串大小
if(ary[i].size==ary[j].size)Sorting(ary,i,j);
else (ary[i].size>ary[j].size)?ary[i].rank++:ary[j].rank++;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){//結果輸出(rank0則最小...n-1最大)
if(ary[j].rank==i)puts(ary[j].ch);
}
}
}
return 0;
}
#include
#include
#define len 1000
struct Num{
char ch[len]; //儲存原始字串
char simplify[len];// 簡化字串(如0001230=1230)
int unit[len]; //換算單位大小(包含負號)
int size; //字串size (字串為負時,加上負號
int rank; //總結比較各size的大小
};
void Sorting(struct Num *ary,int a,int b){//比較相同size字串的大小(如-111 與 -112)後者較小
int i,j,s;
s=strlen(ary[a].simplify);
for(i=0;i<s;i++){
(ary[a].simplify[i]>ary[b].simplify[i])?ary[a].rank++:ary[b].rank++;
break;
}
}
void Ass_Unit(struct Num *ary,int n){//保留ch 簡化至simplify 計算後得到size長度(可為負數)
int num=0;
int i,j=0;
if(ary[n].ch[0]=='0'&&ary[n].ch[1]=='\0')ary[n].simplify[0]='0';
for(i=0;ary[n].ch[i]!='\0';i++){
if(ary[n].ch[i]=='-'){
ary[n].simplify[0]='-';
j++;
continue;
}
else if(ary[n].ch[i]>='1'&&ary[n].ch[i]<='9')break;
}
for(;ary[n].ch[i]!='\0';i++)
ary[n].simplify[j++]=ary[n].ch[i];
ary[n].size=strlen(ary[n].simplify);
if(ary[n].simplify[0]=='-')ary[n].size-=ary[n].size*2-1;
for(i=0,j=0;ary[n].simplify[i]!='\0';i++){
if(ary[n].simplify[0]=='-'&&ary[n].simplify[i+1]!='\0'){
ary[n].unit[j]-=ary[n].simplify[i+1]-'0';
j++;
}
else {
ary[n].unit[j]+=ary[n].simplify[i]-'0';
j++;
}
}
return;
}
int main(){
int n,i,j;
char c;
while(scanf("%d%c",&n,&c)!=EOF){
struct Num *ary=malloc(n*sizeof(struct Num));
for(i=0;i<n;i++){//取N組側資傳送函數
gets(ary[i].ch);
Ass_Unit(ary,i);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){//比較字串大小
if(ary[i].size==ary[j].size)Sorting(ary,i,j);
else (ary[i].size>ary[j].size)?ary[i].rank++:ary[j].rank++;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){//結果輸出(rank0則最小...n-1最大)
if(ary[j].rank==i)puts(ary[j].ch);
}
}
}
return 0;
}
已找到排序錯誤><...
#include <stdio.h>
#include<string.h>
#define len 1000
struct Num{
char ch[len];//儲存原始字串
char simplify[len];// 簡化字串(如0001230=1230)
int unit[len];//換算單位大小(包含負號)
int size;字串size (字串為負時,加上負號
int rank;//總結比較各size的大小
};
void Sorting(struct Num *ary,int a,int b){//比較相同size字串的大小(如-111 與 -112)後者較小
int i,j;
for(i=0;i<len;i++){
if(ary[a].unit[i]==ary[b].unit[i])continue;
(ary[a].unit[i]>ary[b].unit[i])?ary[a].rank++:ary[b].rank++;
break;
}
}
void Ass_Unit(struct Num *ary,int n){//保留ch 簡化至simplify 計算後得到size長度(可為負數)
int num=0;
int i,j=0;
if(ary[n].ch[0]=='0'&&ary[n].ch[1]=='\0'){
ary[n].simplify[0]='0';
}
for(i=0;ary[n].ch[i]!='\0';i++){
if(ary[n].ch[i]=='-'){
ary[n].simplify[0]='-';
j++;
continue;
}
ary[n].simplify[j++]=ary[n].ch[i];
}
ary[n].size=strlen(ary[n].simplify);
if(ary[n].simplify[0]=='-'){
ary[n].size-=ary[n].size*2-1;
}
for(i=0,j=0;ary[n].simplify[i]!='\0';i++){
if(ary[n].simplify[0]=='-'&&ary[n].simplify[i+1]!='\0'){
ary[n].unit[j]-=ary[n].simplify[i+1]-'0';
j++;
}
else {
ary[n].unit[j]+=ary[n].simplify[i]-'0';
j++;
}
}
return;
}
int main(){
int n,i,j;
char c;
while(scanf("%d%c",&n,&c)!=EOF){
struct Num *ary=malloc(n*sizeof(struct Num));
for(i=0;i<n;i++){//取N組側資傳送函數
gets(ary[i].ch);
Ass_Unit(ary,i);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){//比較字串大小
if(ary[i].size==ary[j].size){
Sorting(ary,i,j);
continue;
}
(ary[i].size>ary[j].size)?ary[i].rank++:ary[j].rank++;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){//結果輸出(rank0則最小...n-1最大)
if(ary[j].rank==i)puts(ary[j].ch);
}
}
}
return 0;
}
經修正如上...結果還是不行,請高手幫忙看看>_<"...找不到問題在哪裡...