赞
踩
C+±021-数组-2020-3-8
大多数的C++编译器对数组下标检查不是非常严格,即使数组元素超过了该数字的下标范围,编译仍热可以通过,但程序运行后的结果却可能是错误的。这是C++语言自由度较大的弊端。
1.全局数组与局部数组
//全局数组与局部数组 #include<iostream> using namespace std; int a[10]; int fun() { a[0]=0;//a[]为全局变量,可以被调用 //b[0]=0; 错误调用,因为b[]为局部变量 } int main() { int i,b[10]; for(i=0;i<=9;i++) cout<<a[0]<<' ';//输出的值均为0 for(i=0;i<=9;i++) cout<<b[i]<<' ';//输出的值未知 return 0; }
0 0 0 0 0 0 0 0 0 0 7012016 7012072 7012300 1970982080 -53171883 -2 7012072 1970958317 4250256 7012224
--------------------------------
Process exited with return value 0
Press any key to continue . . .
开辟数组时要注意比赛环境的限制,例如256MB内存空间,大约可以开辟六千万左右的整数数组,如果过大,则程序会崩溃。计算方法是:60 000 000 X4字节/1024/1024=228MB
2.冒泡排序
//用冒泡法对10个数排序(由小到大) #include<iostream> using namespace std; int main() { int a[11],i,j,temp; for(i=1;i<11;i++) cin>>a[i]; cout<<endl; for(j=1;j<=10-1;j++)//大循环共九次 for(i=1;i<=10-j;i++) //每个小循环的步数逐次递减 if(a[i]>a[i+1]) //比较两数,如第一个大于第二个元素,则小数上浮,,大数下沉 {temp=a[i];a[i]=a[i+1];a[i+1]=temp; } for(i=1;i<11;i++) cout<<a[i]<<" ";//打印 return 0; }
9
7
6
5
8
3
2
1
4
5
1 2 3 4 5 5 6 7 8 9
--------------------------------
Process exited with return value 0
Press any key to continue . . .
3.冒泡排序的改进法
//冒泡排序的改进法 #include<iostream> #include<cstdio> using namespace std; #define N 100000+1 int i,j,n,lastchange; int a[N+1]; int main() { freopen ("sort.in","r",stdin); freopen ("sort.out","w",stdout); cin>>n; for(i=1;i<=n;i++) cin>>a[i]; i=2;//从最后向前比较,起始最前面的位置,即n到i间数分别与前一个数比较 while(i<n) { lastchange=n;//先设定这一轮扫描最后交换位置为n for(j=n;j>=i;j--)//从后向前扫描 if(a[j]<a[j-1]) //从相邻前面大于后面的,小的上浮,并记住交换位置j { a[j]=a[j]^a[j-1];//两实数交换,此处用的是位运算符 a[j-1]=a[j-1]^a[j]; a[j]=a[j]^a[j-1]; lastchange=j;//随时记录最后的变更位置 } i=lastchange; //下一轮,只需要从最后比较到上一轮最后交换的位置lastchange } for(i=1;i<n;i++)//输出 cout<<a[i]<<' '; cout<<a[n]<<'\n'; return 0; }
sort.in
9 3 4 5 1 2 6 9 8 7
sort.out//执行后
1 2 3 4 5 6 7 8 9
4.统计各数据个数
由0~20以内的整数N个,N不大于10万,计算出N的个数及不同数的个数,中间以空格间隔。
//统计不同数据的个数的优化算法,利用下标 #include<iostream> #include<cstdio> using namespace std; int b[20+1]; int i,temp,sum; int main() { freopen("number.in","r",stdin); freopen("number.out","w",stdout); while(cin>>temp) { b[temp]++; ++sum; } cout<<sum<<endl; for(i=0;i<=20;i++) cout<<b[i]<<' '; return 0; }
number.in
3 2 3 1 5
number.out
5
0 1 1 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
int a[3] [4]={1,2,3,4,5,6,7,8,9,10,11,12};
int b[ ] [4]={1,2,3,4,5,6,7,8,9,10,11,12};
int c[] [4]={{0,0,3},{},{0,10}};
行列互换
//二维数组的行和列元素互换 #include<iostream> using namespace std; int main() { int a[2][3]={{1,2,3},{4,5,6}}; int b[3][2],i,j; cout<<"a数组元素为:"<<endl;//打印a的元素 for(i=0;i<=1;i++) { for(j=0;j<=2;j++) { cout<<a[i][j]<<" "; b[j][i]=a[i][j];//元素行列互换 } cout<<endl; } cout<<"b数组元素为:\n"; for(j=0;j<=2;j++) { for(i=0;i<=1;i++) cout<<b[j][i]<<" "; cout<<endl; } return 0; }
a数组元素为:
1 2 3
4 5 6
b数组元素为:
1 4
2 5
3 6
--------------------------------
Process exited with return value 0
Press any key to continue . . .
用来存放字符数据的数组是字符数组。字符数组中的一个元素存放一个字符。如:
char[5];
c[0]=‘H’;c[1]=‘E’,c[2]=‘L’;c[3]=‘L’;c[4]=‘O’;
其保存形式如图
| H | E | L | L | O |
|---|
由于字符型与整型是互相通用的,因此上面的定义也可改为:
int c[5];//合法,但浪费空间
字符数组的初始化方式如下:
char c[5]={‘H’,‘E’,‘L’,‘L’,‘O’};
如果提供的初值个数与预定的数组长度相同,在定义时可以省略数组长度,系统会自动根据初值个数确定数组长度。如:
char c[]={‘H’,‘E’,‘L’,‘L’,‘O’};
也可以定义和初始化一个二维字符数组,如:
char a [3] [3] ={{‘a’,‘b’,‘c’},{‘1’,‘2’,‘3’},{’*’,’","}};
例:输出一个字符数组。
//输出一个字符数组
#include<iostream>
using namespace std;
int main()
{
char c[5]={'H','e','l','l','o'};
int i;
for(i=0;i<5;i++)
cout<<c[i];
return 0;
}
Hello
--------------------------------
Process exited with return value 0
Press any key to continue . . .
我们以前曾用过以下的语句:
cout<<“I am happy\n”;
即输出一个字符串,该字符串在内存存储时系统会自动在最后一个字符‘\n’的后面加一个’\0‘作为字符串结束标志,在执行cout函数时,每输出一个字符检查一次,看下一个字符是否’0‘,遇’\0’就停止输出。所以,该字符串的长度实际应是"I am happy\n"的长度再加上’\0’的长度即11+1=12;
因此我们可用字符串常量来使字符数组初始化。如:
char c[]={“I am here"};
也可以省略大括弧,直接写成
char c[]=“I am here”;
这两个初始化与下面的初始化等价;
char c[]={‘I’, ’ ’ , ‘a’,‘m’,’ ‘,‘h’,‘e’,‘r’,‘e’,’\0’};
而不与下面的等价;
char c[]={‘I’,’ ‘,‘a’,‘m’,’ ',‘h’,‘e’,‘r’,‘e’};
前者的长度为10,后者的长度为9.
开辟动态数组1
//开辟动态数组1 #include<iostream> #include<cstdlib> using namespace std; int main() { int *p; int n,i; cin>>n;//动态开辟数组p[n]; while((p=(int*)malloc(sizeof(int)*n))==NULL); for(i=0;i<=n-1;i++) p[i]=2*i; for(i=0;i<=n-1;i++) cout<<p[i]<<" "; return 0; }
8
0 2 4 6 8 10 12 14
--------------------------------
Process exited with return value 0
Press any key to continue . . .
开辟动态数组2
//开辟动态数组2
#include<iostream>
using namespace std;
int main()
{
int *a,n;
cin>>n;
a=new int [n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cout<<a[i]<<' ';
return 0;
}
8
2
3
4
5
8
9
7
1
2 3 4 5 8 9 7 1
--------------------------------
Process exited with return value 0
Press any key to continue . . .
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。