ios学习记录 day7
1.类型 数组名[第一维][第二维] = 初始值;//第一维 第二维 是常量表达式
2.二维数组定义时,可以不指定第一维的长度,但必须给定第二维. 元素个数不足,自动补齐.//int a[][3] = {1,2,3,4,5};
3.字符串 字符型一维数组 | 字符串数组 字符型二维数组
课上练习:
1).将一个二维数组的行和列交换,存储到另外一个数组中去.
int a[3][4] = {0};
int b[4][3] = {0};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = arc4random() % (20 - 10 + 1) + 10;
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
b[i][j] = a[j][i];
printf("%d ",b[i][j]);
}
printf("\n");
}
2).一个3行4列的二维数组,找出最大元素,输出所在的行和列.
int a[3][4] = {0};
int row = 0,col = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = arc4random() % (20 - 10 + 1) + 10;
printf("%d ",a[i][j]);
if (a[row][col] < a[i][j]) {
row = i;
col = j;
}
}
printf("\n");
}
printf("max = %d\n行%d,列%d\n",a[row][col],row,col);
(3)char str[3][20] = {"iphone","Android","winPhone"};
for (int i = 0; i < 3; i++) {
int length = 0;
while (str[i][length] != '\0') {
printf("%c",str[i][length]);
length++;
}
printf("\n");
}
(4)char str[3][20] = {"iphone","Android","winPhone"};
char str1[20] = "";
strcpy(str1, str[1]);
strcpy(str[1], str[2]);
strcpy(str[2], str1);
for (int i = 0; i < 3; i++) {
printf("%s\n",str[i]);
}
5).输入三个字符串,输出最大的那个.
char str[3][20] = {0};
int maxIndex = 0;
for (int i = 0; i < 3; i++) {
scanf("%s",str[i]);
}
for (int i = 0; i < 3; i++) {
if (strlen(str[maxIndex]) < strlen(str[i])) {
maxIndex = i;
}
}
printf("\n%s\n",str[maxIndex]);
6).通讯录有10个人名,按照姓的拼音缩写排列输出
char str[10][20] = {0};
char temp[20] = {0};
// 随机生成5-15的名字
for (int i = 0; i < 10; i++) {
int rLength = arc4random() % (15 - 5 + 1) + 5;//随机产生名字的长度
for (int j = 0; j < rLength; j++) {
int r = arc4random() % 2;//真 假
str[i][j] = r ? arc4random() % ('z'-'a'+1)+'a' : arc4random() % ('Z'-'A'+1)+'A';//真随机小写 假随机大写字母
}
str[i][rLength] = '\0';//名字结束
}
// for (int i = 0; i < 10; i++) { /*名字键盘输入
// scanf("%s",str[i]);
// } */
for (int i = 0; i < 10 - 1; i++) {
for (int j = 0; j < 10 - i - 1; j++) {
if (strcmp(str[j],str[j + 1]) > 0) {
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
for (int i = 0; i < 10; i++) {
printf("%s\n",str[i]);
}
作业:
1.输入6个字符串,并对它们按从小到大的顺序排序后输出。
char a[6][20] = {0};
char temp[20] = {0};
for (int i = 0; i < 6; i++) {
scanf("%s",a[i]);
}
for (int i = 0; i < 6 - 1; i++) {
for (int j = 0; j < 6 - i - 1; j++) {
if (strcmp(a[j], a[j + 1]) > 0) {
strcpy(temp, a[j]);
strcpy(a[j], a[j + 1]);
strcpy(a[j + 1], temp);
}
}
}
printf("\n");
for (int i = 0; i < 6; i++) {
printf("%s\n",a[i]);
}
2.找出一个二维数组中的“鞍点”,即该位置上的元素在该行中最大,在该列中最小(也可能没鞍点),打印出有关信息。
int a[3][4] = {
5,5,5,5,
1,9,9,9,
1,9,9,9};
int have = -1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++) {
int mini = 0,maxj = 0;
for (int j = 0; j < 4; j++) {
if (a[i][maxj] <= a[i][j]) {
maxj = j;
}
}
for (int j = 0; j < 4; j++) {
if (a[i][maxj] == a[i][j]) {
for (int k = 0; k < 3; k++) {
if (a[mini][j] >= a[k][j]) {
mini = k;
}
}
if (mini == i) {
printf("a[%d][%d] = %d 是鞍点\n",mini,j,a[mini][j]);
have = 0;
}
}
}
}
if (have) {
printf("没有鞍点\n");
}
1.反转一个数组内的元素(例如一个数组内元素为1,2,3,4,5,反转后为5,4,3,2,1)
int a[5] = {0};
int b[5] = {0};
for (int i = 0; i < 5; i++) {
scanf("%d",&a[i]);
}
printf("\n");
for (int i = 0; i < 5; i++) {
b[i] = a[4 - i];
printf("%d ",b[i]);
}//(1)
int a[5] = {1,2,3,4,5};
for (int i = 0; i < 5 / 2; i++) {
int temp = a[i];
a[i] = a[5 - i - 1];
a[5 - i - 1] = temp;
}
for (int i = 0; i < 5; i++) {
printf("%d ",a[i]);
}//(2)
2.编写一个程序,输入两个包含5个元素的数组,先将两个数组升序排序,然后将这两个数组合并成一个升序数组。
int a1[5] = {12,48,71,77,88};
int a2[5] = {21,32,40,55,73};
int a[10] = {0};
int i = 0,j = 0,k = 0;
while (i < 5 && j < 5) {
// if (a1[i] > a2[j]) {
// a[k] = a2[j];
// k++;
// j++;
// }else{
// a[k++] = a1[i++];
// }
a[k++] = a1[i] < a2[j] ? a1[i++] : a2[j++];
}//往a里装完了一个数组
while (i < 5) {
a[k++] = a1[i++];
}
while (j < 5) {
a[k++] = a2[j++];
}//往a里装剩下的
for (int i = 0; i < 10; i++) {
printf("%d ",a[i]);
}
3.给定某年某月某日,将其转换成这一年的第几天并输出。
int Mday[11] = {31,28,31,30,31,30,31,31,30,31,30};
int year = 0,month = 0,day = 0;
int sum = 0;
scanf("%d,%d,%d",&year,&month,&day);
for (int i = 0; i < month - 1; i++) {
sum += Mday[i];
}
sum += day;
if (month > 3) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
sum++;
}
}
printf("是第%d天\n",sum);
4.char result[50] = {0};
char str1[] = "Lanou ";
char str2[] = “dlios8”;
char str3[] = " is best!";
把str1, str2, str3合并到result数组中。
“Lanou dlios8 is best!”
char result[50] = {0};
char str1[] = "Lanou ";
char str2[] = "dlios8";
char str3[] = " is best!";
strcat(result, str1);
strcat(result, str2);
strcat(result, str3);
printf("%s\n",result);
5.模拟n个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用A、B、C、D表示,当选某候选人时,直接输入其编号(编号由计算机随机产生),
若输入的不是A、B、C、D则视为无效票,选举结束后按得票数从高到低输出候选人编号和所得票数。
//// int i=0,n=0,A=0,B=0,C=0,D=0,abandon=0,ran=0;
//// printf("输入参与投票的人数:");
//// scanf("%d",&n);
//// for(i = 0;i < n;i++)
//// {
//// ran = (arc4random() % (4 - 0 + 1) + 0) + 65;
//// switch(ran)
//// {
//// case 'A':
//// A++;
//// break;
//// case 'B':
//// B++;
//// break;
//// case 'C':
//// C++;
//// break;
//// case 'D':
//// D++;
//// break;
//// default :
//// abandon++;
//// }
////
//// }
//// printf("\nA=%d,B=%d,C=%d,D=%d,abandon=%d\n\n",A,B,C,D,abandon);
//// i = 4;
//// while(i-- != 0)
//// {
//// if(A>=B&&A>=C&&A>=D&&A!=-1)
//// {
//// printf("A=%d,",A);
//// A=-1;
//// }
//// if(B>=A&&B>=C&&B>=D&&B!=-1)
//// {
//// printf("B=%d,",B);
//// B=-1;
//// }
//// if(C>=A&&C>=B&&C>=D&&C!=-1)
//// {
//// printf("C=%d,",C);
//// C=-1;
//// }
//// if(D>=A&&D>=B&&D>=C&&D!=-1)
//// {
//// printf("D=%d,",D);
//// D=-1;
//// }
//// }
////
//// printf("abandon=%d, \n\n",abandon);
6.编写一个程序,要求用户输入一个美金数量, 然后显示出如何用最少的20美元、10美元、5美元和1美元来付款
int money = 0;
scanf("%d",&money);
int array[4] = {20,10,5,1};
for (int i = 0; i < 4; i++) {
printf("%-2d元:%2d张\n",array[i],money / array[i]);
money %= array[i];
}