我不知道位和卡住= =
用bubble sort 順排時卡在639行
逆排時卡在638
結果都是
答案不正確
您的答案為: 7589347589347589789238478972389749 正確答案為: 7482907834723894782934789237489234
可麻煩大大幫分析幫看?
--------以下是程式碼ww-----大大加油!!---------
#include <iostream>
using namespace std;
struct bignum{
string digit;
};
bool comp(bignum left,bignum right){
int flag=0;
if(left.digit[0]!='-' && right.digit[0]=='-') flag=1;
else if (left.digit[0]=='-' && right.digit[0]!='-') flag= 0;
else{
if (left.digit[0]=='-' && right.digit[0]=='-'){
if (left.digit.size() < right.digit.size()) flag=1;
else if (left.digit.size() > right.digit.size()) flag=0;
else{
for(int i=0;i<left.digit.size();i++){
if (left.digit[i]<right.digit[i]){
flag=1;
break;
}
}
}
}
else{
if (left.digit.size() > right.digit.size()) flag=1;
else if (left.digit.size() < right.digit.size()) flag=0;
else{
for(int i=0;i<left.digit.size();i++){
if (left.digit[i]>right.digit[i]){
flag=1;
}
}
}
}
}
if (flag==1) return 1;
else return 0;
}
int main(){
int n;
while(cin>>n){
bignum series[n];
for(int i=0;i<n;i++){
cin>>series[i].digit;
}
for(int j=n-1;j>0;j--){
for(int k=0;k<j;k++){
bignum bignumc;
if(comp(series[k],series[k+1])){
bignumc=series[k+1];
series[k+1]=series[k];
series[k]=bignumc;
}
}
}
for(int j=n-1;j>0;j--){
for(int k=j;k>=1;k--){
bignum bignumc;
if(comp(series[k-1],series[k])){
bignumc=series[k-1];
series[k-1]=series[k];
series[k]=bignumc;
}
}
}
for(int j=0;j<n;j++){
cout<<series[j].digit<<endl;
}
}
return 0;
}
我不知道位和卡住= =
用bubble sort 順排時卡在639行
逆排時卡在638
結果都是
答案不正確
您的答案為: 7589347589347589789238478972389749 正確答案為: 7482907834723894782934789237489234
可麻煩大大幫分析幫看?
--------以下是程式碼ww-----大大加油!!---------
#include
using namespace std;
struct bignum{
string digit;
};
bool comp(bignum left,bignum right){
int flag=0;
if(left.digit[0]!='-' && right.digit[0]=='-') flag=1;
else if (left.digit[0]=='-' && right.digit[0]!='-') flag= 0;
else{
if (left.digit[0]=='-' && right.digit[0]=='-'){
if (left.digit.size() < right.digit.size()) flag=1;
else if (left.digit.size() > right.digit.size()) flag=0;
else{
for(int i=0;i<left.digit.size();i++){
if (left.digit[i]<right.digit[i]){
flag=1;
break;
}
}
}
}
else{
if (left.digit.size() > right.digit.size()) flag=1;
else if (left.digit.size() < right.digit.size()) flag=0;
else{
for(int i=0;i<left.digit.size();i++){
if (left.digit[i]>right.digit[i]){
flag=1;
}
}
}
}
}
if (flag==1) return 1;
else return 0;
}
int main(){
int n;
while(cin>>n){
bignum series[n];
for(int i=0;i<n;i++){
cin>>series[i].digit;
}
for(int j=n-1;j>0;j--){
for(int k=0;k<j;k++){
bignum bignumc;
if(comp(series[k],series[k+1])){
bignumc=series[k+1];
series[k+1]=series[k];
series[k]=bignumc;
}
}
}
for(int j=n-1;j>0;j--){
for(int k=j;k>=1;k--){
bignum bignumc;
if(comp(series[k-1],series[k])){
bignumc=series[k-1];
series[k-1]=series[k];
series[k]=bignumc;
}
}
}
for(int j=0;j<n;j++){
cout<<series[j].digit<<endl;
}
}
return 0;
}
你的排序是錯誤的~
在相同的長度下,例如輸入
3
-123456
-456123
-222222
而你的輸出是
-222222
-123456
-456123