简体   繁体   English

排序算法问题(购买商店商品)(崩溃)

[英]Problem with sort algorithm(buying shop items)(Crashing)

I have problem about little bit complex problem with sorting algorithm.我对排序算法有点复杂的问题有疑问。 And my program non-stop crashing(sorry i'm bad at English).我的程序不停地崩溃(对不起,我英语不好)。 The goal of program is to buy (from budget) most expensive item and sort every item to the list but, as you can see i'm suck at this and get an a crash from program(again sorry for rude).程序的目标是购买(从预算中)最昂贵的物品并将每件物品分类到列表中,但是,正如你所看到的,我很糟糕,并且从程序中崩溃了(再次为粗鲁感到抱歉)。

#include <iostream>
using namespace std;

int main()
{
double pare; //budget
int n; //numbers of articles
cin>>pare>>n;

string kod[n];//name of article

bool check[n]; //checker

double cena[n]; //price

int m,j; //max and extend for him(j)
m=cena[1];
j=0;

 for(int b = 0; b<n;b++){ //input
      cin>>cena[b];
}


for(int x = 0; x<n;n++){
    if(cena[x]==pare){
        pare-=cena[x];

           check[x] = true;
         }else if(cena[x]>pare){
             check[x] = false;

         }else if(cena[x] < pare){
                check[x] = true;
         }

for(int i2 = 0;i2<n;i2++){
    if(cena[i2] == true){
        if(m<cena[i2]){
            m=cena[i2];
            j=m;
        }
    }
}
 pare-=j;

}

 for(int i3 = 0; i3<n;i3++){   //output
     if(check[i3] == true){
        cout<<kod[i3]<< " " << cena[i3]<<endl;
    }
 }
 if(pare>0){
    cout<<pare<<endl;
 }




return 0;
}

Sorry if i something miss.对不起,如果我错过了什么。

 hello I'm not sure if I understood your code or not,
 but it supposed to be something like this 

 #include <iostream>
 //#include <string>
 #include <vector>
 using namespace std;

        int main(){

            double pare; //budget
            int n; //numbers of articles

            cin >> pare >> n;

            vector<string> kod(n); // name of article

            vector<bool> check(n); // checker

            vector<double> cena(n); // price

            for(int i = 0; i < n; i++){ //initilize
                kod[i] = "";
                check[i] = false;
                cena[i] = 0.0;
            }

            for(int b = 0; b < n; b++){ //input
                cin >> cena[b];
            }

            for(int x = 0; x < n; n++){
                if(cena[x] == pare){
                    pare -= cena[x];
                    check[x] = true;
                }
                else if(cena[x] > pare){
                    check[x] = false;
                }
                else if(cena[x] < pare){
                    check[x] = true;
                }
            }

            //max and extend for him(j)
            int m = cena[0];
            int j = 0;

            for(int x = 0; x < n; x++){
                for(int i2 = 0; i2 < n; i2++){
                    if(check[i2]){
                        if(m < cena[i2]){
                            j = m = cena[i2];
                        }
                    }
                }
                pare -= j;
            }

            for(int i = 0; i < n; i++){ // to string 
                kod[i] = to_string(cena[i]);
            }

            for(int i3 = 0; i3 < n; i3++){   //output
                if(check[i3]){
                    cout << kod[i3] << " " << cena[i3] << endl;
                }
            }
            if(pare > 0){
                cout << pare << endl;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM