- //
- // main.m
- // 递归
- //
- // Created by Mac on 15/10/25.
- // Copyright (c) 2015年 Mac. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- float aaa(float score[5]);
- int getAge(int n){
- //后一个人的年龄是前一个人的年龄+2
- int age=10;
- if (n==1) {
-
- age=10;
- }else{
-
- age=getAge(n-1)+2;
-
- }
- return age;
- }
- //求阶乘
- int jiecheng(int n){
- int result;
- if (n==1)
- {
-
- result=1;
-
- }else
- {
- //前一个数与现在的数相乘
- result=jiecheng(n-1)*n;
-
- }
- return result;
- }
- //求x的y次方
- int xy(int x,int y){
-
- int result;
- if (y==1) //当y=1时 例如2的1次方就等于1
- {
- result=x;
- }else //当y不为1时 结果就是x * (y-1)一直减到y为1结束
- {
-
- result=xy(x,y-1)*x;
- }
- return result;
- }
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- int a=getAge(5);
- NSLog(@"%D",a);
- int b=jiecheng(1);
- NSLog(@"%D",b);
- int c=xy(2,3);
- NSLog(@"%D",c);
-
- int number[10];
- //总字节数除以类型的单个字节数
- int len=sizeof(number)/sizeof(int);
- for (int i=0; i<len; i++) {
-
- printf("请输入第%d个数字\n",i+1);
- scanf("%d",&number[i]);
- }
-
- for (int i=0; i<len; i++) {
-
- printf("%d\t",number[i]);
-
- }
-
- //假设数组第一元素为最大值
- int max=number[0];
- for(int i=1;i<len;i++){
-
- if (number[i]>max) {
-
- max=number[i];
-
- }
-
- }
-
- printf("最大值为:%d",max);
- }
-
- //float f1[5]=[2.1,44.3,66.5,6.1,5.4];
- float f1[5]={2.1,44.3,66.5,6.1,5.4};
- float result=aaa(f1);
- NSLog(@"平均值是%f",result);
-
- return 0;
- }
- float aaa(float score[5]){
-
- float sum=0.0f;
- for (int i=0; i<5; i++)
- {
-
- sum+=score[i];
- }
- return sum/5;
-
- }
复制代码
|
|