C++编程题(蓝桥杯)
 

运行结果

#include <iostream> using namespace std; void jingsai1() { //chh:水深; chs:最初水下深度; int chh=0,chs=0; int I_depth=0; cout<<"请输入水深和最初下水深度:"; cin>>chh>>chs; cout<<chh<<","<<chs<<endl; I_depth=chs; //u:上浮1米;d:下沉一米 cout<<"请输入指令集:"; char orderGroup[100] ; cin>>orderGroup; cout<<orderGroup<<endl; for (int i=0;i<20;i++) { if (orderGroup[i]=='\0') { break; } char ch=orderGroup[i]; switch (ch) { case 'u': if (I_depth>0) { I_depth--; } cout<<"上浮1米,当前水深"<<I_depth<<endl; break; case 'd': if (I_depth<chh) { I_depth++; } cout<<"下沉1米,当前水深"<<I_depth<<endl; break; default: cout<<"无效的命令"<<endl; break; } } cout<<"当前水深:"<<I_depth<<endl; }


 
运行结果

void jingsai2() { int n=0;//站点数 cin>>n; int fish[n][2];//第一列保持鱼的价格,第二列保存运费 int min_sum=0; //存储一路下来花费的最小费用 // //int min=100000;//当前 站点最小花费 int min=INT_MAX; for (int i=0;i<n;i++) { for (int j=0;j<2;j++) { cin>>fish[i][j]; } } for (int i=0;i<n;i++) { for (int j=0;j<2;j++) { cout<<fish[i][j]<<" "; } cout<<endl; } for (int i=0;i<n;i++) { if (fish[i][0]<min) { min=fish[i][0]; //本站的最小花费 } min_sum=min_sum+min; min=min+fish[i][1]; //站的最小花费+本站保存的费用 cout<<i<<":"<<min_sum<<endl; } cout<<min_sum; }
 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号