【问题标题】:Problem with sort algorithm(buying shop items)(Crashing)排序算法问题(购买商店物品)(崩溃)
【发布时间】:2018-09-30 21:28:52
【问题描述】:

我对排序算法有点复杂的问题有疑问。 而且我的程序不停地崩溃(对不起,我的英语不好)。节目的目标 是购买(从预算中)最昂贵的物品并将每件物品排序到列表中,但是,正如您所看到的,我对此很烂,并从程序中崩溃(再次抱歉粗鲁)。

#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;
}

对不起,如果我有什么遗漏。

【问题讨论】:

  • string kod[n]; 这样的 VLA 不是 C++ 的一部分。使用std::vector
  • m=cena[1]; 将未初始化的值分配给 m
  • "...你可以看到我很烂..."。即使这是真的,那也是因为我花时间阅读和/或运行您的代码。获得帮助的最佳方式是清楚地描述您遇到的问题以及您收到的任何错误。
  • 欢迎来到 Stack Overflow。新手程序员必须学会将任务分成几部分。我建议您在尝试对更复杂的结构进行排序之前学习对整数进行排序。
  • for(int x = 0; x&lt;n;n++) -- 你是说x++吗?

标签: c++ algorithm sorting console-application


【解决方案1】:
 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;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 2020-10-03
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多