这段代码的运行不符合预期,该怎么改const int B=10; int getmax(int a[],int n) { int maxx=0,b=1; for(int i=1;i<=n;i++) { maxx=max(maxx,a[i]); } while(maxx>=B) { maxx/=B; b++; } return b; } void radix(int a[],int n) { int maxb=getmax(a,n); int e=1; int cnt[B+1]; int tmp[n+1]; for(int i=1;i<=maxb;i++,e*=B) { memset(cnt,0,sizeof(cnt)); for(int j=1;j<=n;j++) { int bucket=(a[j]/e)%B; cnt[bucket]++; } for(int j=2;j<=B;j++) { cnt[j]+=cnt[j-1]; } for(int j=n;j>=1;j--) { int bucket=(a[j]/e)%B; tmp[--cnt[bucket]]=a[j]; } for(int j=1;j<=n;j++) { a[j]=tmp[j]; } } }
时间: 2024-03-14 10:45:05 浏览: 22
这段代码是基数排序的实现,其中最大值的获取和桶的数量计算有问题。可以将获取最大值的函数改为如下形式:
```c++
int getmax(int a[],int n) {
int maxx=a[1];
for(int i=2;i<=n;i++) {
if(a[i]>maxx) {
maxx=a[i];
}
}
int b=1;
while(maxx>=B) {
maxx/=B;
b++;
}
return b;
}
```
这样就能够正确地获取最大值了。同时,桶的数量应该是 $B+1$ 而不是 $B$,因为还有一个桶存放余数为 $0$ 的数。因此,应该将桶的数量定义为:
```c++
const int B=10;
const int bucket_num=B+1;
```
然后在代码中也要做相应的更改,比如:
```c++
int cnt[bucket_num];
```
```c++
for(int j=2;j<=bucket_num;j++) {
cnt[j]+=cnt[j-1];
}
```
相关问题
#include <iostream> using namespace std; int books[1001][2]; int f[1001]; int getmax(int a,int b){ if(a>b){ return a; }else{ return b; } } int getmin(int a,int b){ if(a<b){ return a; }else{ return b; } } int put(const int wide,int l,int r){ int roof,height=0,rest=wide; for(int i=l;i<=r;i++){ if(rest<books[i][1]){ rest=wide; height=height+roof; roof=0; } rest=rest-books[i][1]; roof=getmax(roof,books[i][0]); } return height+roof; } void geth(int n,int s){ f[0]=0; for(int i=1;i<=n;i++){ f[i]=999999; for(int j=0;j<=i-1;j++){ int p=put(s,j+1,i); f[i]=getmin(f[i],f[j]+p); } } } int main(){ int n,s; cin>>n>>s; for(int i=1;i<=n;i++){ cin>>books[i][0]>>books[i][1]; } geth(n,s); for(int i=0;i<=n;i++){ cout<<f[n]<<endl; } } 这段代码有什么问题
这段代码有几个问题:
1. 在函数`put`中,`roof`没有被初始化,应该加上`int roof=0;`。
2. 在函数`put`中,变量`height`应该被初始化为第一个书堆的高度,即`height=books[l][0];`。
3. 在主函数中,最后一个循环输出`f[n]`的语句应该放在循环体外,因为循环体内每次都输出,会导致输出多个值。
4. 在函数`geth`中,循环中的变量`p`没有被初始化,应该加上`int p=0;`。同时,`getmin`函数应该定义为返回两个数的最小值,而不是其中一个数。
5. 在主函数中,最后一个循环输出`f[n]`的语句应该改为输出`f[i]`。
修改后的代码如下:
```
#include <iostream>
using namespace std;
int books[1001][2];
int f[1001];
int getmax(int a,int b){
if(a>b){
return a;
}else{
return b;
}
}
int getmin(int a,int b){
return a<b ? a : b;
}
int put(const int wide,int l,int r){
int roof=0,height=books[l][0],rest=wide;
for(int i=l;i<=r;i++){
if(rest<books[i][1]){
rest=wide;
height=height+roof;
roof=0;
}
rest=rest-books[i][1];
roof=getmax(roof,books[i][0]);
}
return height+roof;
}
void geth(int n,int s){
f[0]=0;
for(int i=1;i<=n;i++){
f[i]=999999;
for(int j=0;j<=i-1;j++){
int p=put(s,j+1,i);
f[i]=getmin(f[i],f[j]+p);
}
}
}
int main(){
int n,s;
cin>>n>>s;
for(int i=1;i<=n;i++){
cin>>books[i][0]>>books[i][1];
}
geth(n,s);
for(int i=1;i<=n;i++){
cout<<f[i]<<endl;
}
return 0;
}
```
将以下程序补充完整 #include <iostream> #include <fstream> #include <iomanip> using namespace std; template <typename ElemType> class myArrayList { private: int mSize; int mLen; ElemType *mpArr; public: myArrayList(int n); myArrayList(ElemType *a,int n); void show(); ElemType getMax(); //以下函数由同学完成 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> & operator =(mymyArrayList<ElemType> &other) }; template <typename ElemType> myArrayList<ElemType>::myArrayList(int n) { this->mSize=n; this->mLen=0; this->mpArr=new ElemType[mSize]; } template <typename ElemType> myArrayList<ElemType>::myArrayList(ElemType *a,int n) { this->mSize=n; this->mLen=n; this->mpArr=new ElemType[mSize]; for(int i=0;i<mLen;i++) mpArr[i]=a[i]; } template <typename ElemType> void myArrayList<ElemType>::show() { for(int i=0;i<mLen;i++) cout<<setw(4)<<mpArr[i]; cout<<endl; } template <typename ElemType> ElemType myArrayList<ElemType>::getMax() { ElemType max; max=mpArr[0]; for(int i=1;i<mLen;i++) if(max<mpArr[i]) max=mpArr[i]; return max; } //Student.h class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friendbool operator <(Student &a1,Student &a2) { if(a1.height<a2.height) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl; return out; } }; //主程序 int main() { int a[]={1,2,3,5,7,9,12,8}; double b[]={1,2.5,3.6,5,7,9,12.8,8}; myArrayList <int> list1(a,8); list1.show(); cout<<"max="<<list1.getMax()<<endl; myArrayList <double> list2(b,8); list2.show(); cout<<"max="<<list2.getMax()<<endl; Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1; myArrayList <Student> list3(s,3); list3.show(); cout<<"max="<<list3.getMax()<<endl; } 说明: 将以下程序补充完整 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> operator =(mymyArrayList<ElemType> &other) //修改补充 Student类,要求按成绩排序(从高到低)并将完整的代码发出来
以下是补充完整的程序:
```
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
template <typename ElemType>
class myArrayList {
private:
int mSize;
int mLen;
ElemType *mpArr;
public:
myArrayList(int n);
myArrayList(ElemType *a,int n);
void show();
ElemType getMax();
void sort();
myArrayList(const myArrayList<ElemType> &other);
myArrayList<ElemType> & operator =(const myArrayList<ElemType> &other);
};
template <typename ElemType>
myArrayList<ElemType>::myArrayList(int n) {
this->mSize=n;
this->mLen=0;
this->mpArr=new ElemType[mSize];
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(ElemType *a,int n) {
this->mSize=n;
this->mLen=n;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=a[i];
}
template <typename ElemType>
void myArrayList<ElemType>::show() {
for(int i=0;i<mLen;i++)
cout<<setw(4)<<mpArr[i];
cout<<endl;
}
template <typename ElemType>
ElemType myArrayList<ElemType>::getMax() {
ElemType max;
max=mpArr[0];
for(int i=1;i<mLen;i++)
if(max<mpArr[i])
max=mpArr[i];
return max;
}
template <typename ElemType>
void myArrayList<ElemType>::sort() {
for(int i=0;i<mLen-1;i++) {
for(int j=0;j<mLen-i-1;j++) {
if(mpArr[j]<mpArr[j+1]) {
ElemType tmp=mpArr[j];
mpArr[j]=mpArr[j+1];
mpArr[j+1]=tmp;
}
}
}
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(const myArrayList<ElemType> &other) {
this->mSize=other.mSize;
this->mLen=other.mLen;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=other.mpArr[i];
}
template <typename ElemType>
myArrayList<ElemType> & myArrayList<ElemType>::operator =(const myArrayList<ElemType> &other) {
if(this==&other)
return *this;
delete []mpArr;
this->mSize=other.mSize;
this->mLen=other.mLen;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=other.mpArr[i];
return *this;
}
class Student {
private:
int mId;
float height;
int score;
public:
Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { }
friend bool operator <(Student &a1,Student &a2) {
if(a1.score<a2.score)
return true;
else
return false;
}
friend ostream &operator <<(ostream &out,Student &s) {
out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl;
return out;
}
};
int main() {
int a[]={1,2,3,5,7,9,12,8};
double b[]={1,2.5,3.6,5,7,9,12.8,8};
myArrayList <int> list1(a,8);
list1.show();
cout<<"max="<<list1.getMax()<<endl;
myArrayList <double> list2(b,8);
list2.show();
cout<<"max="<<list2.getMax()<<endl;
Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1;
myArrayList <Student> list3(s,3);
list3.show();
list3.sort();
cout<<"max="<<list3.getMax()<<endl;
list3.show();
return 0;
}
```
在这个程序中,我们添加了以下功能:
- `sort()`函数用于对数组进行排序
- 拷贝构造函数 `myArrayList(const myArrayList<ElemType> &other)`用于创建新的对象并将其初始化为与另一个对象相同的值
- 赋值运算符 `myArrayList<ElemType> & operator =(const myArrayList<ElemType> &other)`用于将一个对象的值赋给另一个对象
此外,我们还修改了`Student`类的实现,使其能够按照成绩从高到低进行排序。