美文网首页
oc习题合集二

oc习题合集二

作者: 阿狸演绎 | 来源:发表于2017-07-20 20:49 被阅读0次

import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
//1 随机产生20个10~50的正整数存放到数组中,并求数组中的元素最大值、最小值、平均值及各元素之和。

int array[20] = {0};
int max = 0;
int min = 51;
int sum = 0;
for (int i = 0; i < 20; i++) {
    array[i]=arc4random()%(50-10+1)+10;
    sum += array[i];
    if (array[i]>10) {
        max = max > array[i]?max:array[i];
    }
    min = min < array[i]? min : array[i];
   printf("数组的数%d\n",array[i]);
}

printf("最大值为:%d,最小值为:%d",max,min);
printf("数组的和为:%d,平均值为:%2f\n",sum,sum/20.0);


//2编写整型值数组排序程序(冒泡排序—升序)

int a[] = {3,-9,32,77,63,-24,14,0,21,45};
for (int i = 0; i < 10 - 1; i++) {
    for (int j = 0; j < 10 - 1 - i; j++) {
        if (a[j]>a[j+1]) {
            int temp = a[j];
            a[j] = a[j+1];
            a[j+1] = temp;
        }
    }
}
for (int j = 0; j < 10; j++) {
    printf("%d\n",a[j]);
}
 
//3找出下列整型数组中最大和最小值及其所在位置i。

int a[] = {3,-9,32,77,63,-24,14,0,21,45};
int max=-25;
int min=78;
int j=0,k=0;
for (int i = 0; i < 10; i++) {
  //max = max > a[i] ? max : a[i];
    if (max < a[i]) {
            max = a[i];
           j = i;
        }
    if (min>a[i]) {
        min = a[i];
        k = i;
    }
    
}

 printf("最大值为:%d,最小值为:%d,最大值位置:%d,最小值位置为:%d",max,min,j,k);
 
//4给定某年某月某日,输出其为这一年的第几天

int year = 0;
int month = 0;
int day = 0;
int sumday = 0;
printf("请输入一个日期:(xxxx xx xx)");
scanf("%d %d %d",&year,&month,&day);
int monthes[ ] = { 31,28, 31,30,31,30,31,31,30,31,30,31 };

for (int i = 0 ; i < month - 1; i++) {
    sumday += monthes[i];
}
sumday += day;

//闰年
if ((year % 4 == 0 && year % 100 != 0 )|| (year % 400 == 0)) {
    
    if (month > 2 ) {
        sumday ++;
    }
}
printf("这是第%d天",sumday);

//5把str1, str2, str3合并到result数组中。

// char result[50] = {0};
// char str1[] = "Lanou ";
// char str2[] = "BJS170702_class ";
// char str3[] = " is niu best!";
// 结果:“Lanou BJS170702_class is niu best!”

char result[50] = {0};
char str1[] = "Lanou ";
char str2[] = "BJS170702_class ";
char str3[] = " is niu best!";
char str4[50] = "";
strcat(str4, str1);
strcat(str4, str2);
strcat(str4, str3);
//printf("%s\n",str4);
for (int i = 0 ; i < sizeof(str4) - 1 ;i++ )//-0====\0
{
    result[i] = str4[i];
}
for (int i = 0 ; i < sizeof(str4) ; i++) {
    printf("%c",result[i]);
}

return 0;

}

相关文章

网友评论

      本文标题:oc习题合集二

      本文链接:https://www.haomeiwen.com/subject/qmbjkxtx.html