这是我的程序 但出了问题 求教 谢谢
#include<iostream>
using namespace std;
void sort(int[],int);
int main()
{ int n,m;
cout<<"请输入a数组的大小:";
cin>>n;
cout<<"请输入b数组的大小:";
cin>>m;
int i, j , k = 0,a[n],b[m],c[n+m];
cout<<"请输入数组a的元素:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"请输入数组b的元素:"<<endl;
for(j=0;j<m;j++)
cin>>b[j];
sort(a,n);
cout<<"a数组元素升序排列为:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
sort(b,n);
cout<<"b数组元素升序排列为:"<<endl;
for(j=0;j<m;j++)
cout<<b[j]<<" ";
i=0,j=0;
while (i < n && j < m) {
if (a[i] > b[j]) c[k++] = a[i++];
else c[k++] = b[j++];
}
while (i < n) {
c[k++] = a[i++];
}
while (j < m) {
c[k++] = b[j++];
}
cout<<"合并后:"<<endl;
for (i = 0; i < n + m; i++)
cout << c[i] << " ";
cout<<endl;
return 0;
}
void sort(int x[],int y)
{int min;
for(int i=0;i<y-1;i++)
{for(int j=i;j<y;j++)
if(x[i]>x[j])
{min=x[i]; x[i]=x[j];x[j]=min;}
}
}
å¯ä»¥å®ä¹c[10],ä½æ¯ä¸è½å®ä¹m=10; c[m];
è¿ä¸ªCçæºå¶æå ³ï¼å¨å½æ°è¿è¡ä¹åï¼å°±ä¸ºè¿ä¸ªå½æ°å¨æ åºåé 好空é´ã
å¦æä½ ç¨mæ¥å£°ææ°ç»ï¼ç¼è¯å¨å°±æ æ³é¢å ç»æ°ç»è¿è¡åé 空é´
è¿æ ·è§å®è½å¤æé«è¿è¡æç
å¦æä½ å®æä½ çç®çï¼å°±å¿ 须使ç¨newæä½ç¬¦çææ°ç»ãnewæ¯å¨éæåºåé 空é´ã
newè¿åçæ¯ç³è¯·ç©ºé´çé¦å°åãæ¯ä¸ä¸ªæéãä½ å¯ä»¥åæ°ç»ä¸æ ·ä½¿ç¨å®ã
#include<iostream>
using namespace std;
void sort(int[],int);
int main()
{
int n,m;
cout<<"请è¾å ¥aæ°ç»ç大å°ï¼";
cin>>n;
cout<<"请è¾å ¥bæ°ç»ç大å°ï¼";
cin>>m;
int i, j , k = 0;
int* a = new int[n];
int* b = new int[m];
int* c = new int[m + n];
cout<<"请è¾å ¥æ°ç»açå ç´ ï¼"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"请è¾å ¥æ°ç»bçå ç´ ï¼"<<endl;
for(j=0;j<m;j++)
cin>>b[j];
sort(a,n);
cout<<"aæ°ç»å ç´ ååºæå为ï¼"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
sort(b,n);
cout<<"bæ°ç»å ç´ ååºæå为ï¼"<<endl;
for(j=0;j<m;j++)
cout<<b[j]<<" ";
i=0,j=0;
while (i < n && j < m) {
if (a[i] > b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < n) {
c[k++] = a[i++];
}
while (j < m) {
c[k++] = b[j++];
}
cout<<"å并åï¼"<<endl;
for (i = 0; i < n + m; i++)
cout << c[i] << " ";
cout<<endl;
return 0;
}
void sort(int x[],int y)
{
int min;
for(int i=0;i<y-1;i++)
{
for(int j=i;j<y;j++)
if(x[i]>x[j])
{
min=x[i];
x[i]=x[j];
x[j]=min;
}
}
}追é®
ä½ å¤ªæ£äº
æç¶ç¼è¯æå ä½ ä¸é¢çç¨åºå¿äºéæ¾ç©ºé´äºå§ï¼åµåµ
newæ¯å¨æå§ï¼ 使ç¨newæä½ç¬¦çææ°ç»ä¸ä¹æ¯ä¸ç¥é空é´å¤§å°åï¼ä¸ºä»ä¹å°±å¯ä»¥ï¼åå¦ä¸æ
è¿ææ§è¡æ¶ è¾å
¥æ°ç»aï¼bçå
ç´ ä¸ªæ°ç«ç¶å¯ä»¥è¶
è¿n,m个 è¿æ¯ä¸ºä»ä¹ï¼ 谢谢 æå å
é¦å
ï¼çç¡®æ¯å¿è®°éæ¾ç©ºé´äºãããdeleteä¸ä¸
ç¨åºè¿è¡çæ¶åï¼ç¼è¯å¨ä¼ç»å½æ°é¢å
åé
æ 空é´ï¼å¥½åé»è®¤æ¯2Mã
ç¶åï¼ç¨åºä¸ºå°å½æ°å
çåéåé
空é´ã
ç¶åæ§è¡ä»£ç ã
å¦æä½ ä¸ç¨å¸¸é声ææ°ç»å¤§å°ï¼ç¨åºå¨æ§è¡è¯å¥ä¹åï¼æ¯ä¸è½å¤å¾ç¥æ°ç»å¤§å°çãä¹å°±æ²¡æ³åé
空é´ã
ènewæ¯å¨å åºï¼ä¹åé£ä¸ªåçä¸ï¼æåéäºï¼
å åºå¾å¤§ï¼èä¸ä¸åæ åºé£æ ·è¦æ±å
è¿ååºãæ以使ç¨å¾èªç±ã
ä½ ä½¿ç¨newåé
ä¸ä¸ªæ°ç»ï¼æ§è¡ä»£ç çæ¶åï¼å°±ä¼ç³è¯·ä¸ä¸ªç©ºé´ã
è¶ç使ç¨å åºåæ åºæ¯é常å±é©çãç¨åºæ æ³æ£æµæ¯å¦è¶çãå½ç¶ä¹å°±ä¸ä¼æ¥éã
#include
void main()
{
int a = 5;
int b[5];
b[5] = 2;
printf("%d", a);
}
ä¸é¢è¿æ®µç¨åºå°±æ¯æ°ç»è¶ç书åã2008çç¼è¯å¨ä¼æ¥éãæäºç¼è¯å¨å°±ä¼æaæ¹æ2.
谢谢 è²ä¼¼æäº
ä½ ç æ§è¡çæ¶å å
ç´ ä¸ªæ°ç«ç¶å¯ä»¥å¤äºn,m个 è¿åºç°äºè¿æ ·çé®é¢
ä½ å¯ä»¥ç¨ç¼è¯å¨æä¾çåæ¥è°è¯ï¼æ¥çæ¯ä¸ªå ç´ çåå¨å°åãçä»æ¯æä¹å¤ççãCåC++çæçæ¯å¾å¤§çä¸é¨å¦é®åã
数组的大小不能是变量,因为a[n]等数组的长度n等都使变量所以出错。追问
但是之前不是有个输入—赋值—存储吗
using namespace std;
void sort(int[], int);
int main()
{
int n, m;
cout << "请输入a数组的大小:";
cin >> n;
cout << "请输入b数组的大小:";
cin >> m;
int i, j , k = 0, *a=new int[n], *b= new int[m], *c = new int[n + m];
cout << "请输入数组a的元素:" << endl;
for(i = 0; i < n; i++)
cin >> a[i];
cout << "请输入数组b的元素:" << endl;
for(j = 0; j < m; j++)
cin >> b[j];
sort(a, n);
cout << "a数组元素升序排列为:" << endl;
for(i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
sort(b, m);
cout << "b数组元素升序排列为:" << endl;
for(j = 0; j < m; j++)
cout << b[j] << " ";
i = 0, j = 0;
while (k< n + m)
{
if (k<n) c[k++] = a[i++];
else c[k++] = b[j++];
}
sort(c, n+m);
cout << "合并后:" << endl;
for (i = 0; i < n + m; i++)
cout << c[i] << " ";
cout << endl;
delete [] a;
delete [] b;
delete []c;
return 0;
}
void sort(int x[], int y)
{
int min;
for(int i = 0; i < y - 1; i++)
{
for(int j = i; j < y; j++)
if(x[i] > x[j])
{
min = x[i];
x[i] = x[j];
x[j] = min;
}
}
}追问
你这样想挺不错的 有创意
非常感谢 执行时 输入数组a,b的元素个数竟然可以超过n,m个 这是为什么?
a 的元素个数是n, b的元素个数是m, c的元素个数是n+m哦,a,b不能分别超过n, m的,输入过多可能不会错,但可能会有溢出的危险!
追问啊? 帮我用VC2008 看一下
怪怪的 我感觉 谢谢了 高手
vector容器? 什么东东