练习一:计算机岁一个数,猜数大小。
number = random()%100 +1;
guessNumber = 90;
while (guessNumber != number){
if (guessNumber > number) {
printf("大了");
count++;
}else{
printf("小了");
count++;
}
}
printf("猜了%d次",++count);
}
练习2:求平均数
//
// main.m
// CTest
//
// Created by liushuang on 2023/4/4.
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int inputNumber = 0;//每次输入的数
int sum = 0;//总和
int count = 0;//猜次数
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
appDelegateClassName = NSStringFromClass([AppDelegate class]);
printf("hello world");
scanf("%d",&inputNumber);
while (inputNumber != -1){
sum += inputNumber;
count++;
scanf("%d",&inputNumber);
}
printf("平均数是%f",1.0*sum/count);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
练习3: log2 X 输入一个数a,log2的x次幂 是a,求x。
//
// main.m
// CTest
//
// Created by liushuang on 2023/4/4.
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int inputNumber;//每次输入的数
int ret = 0;//次幂数
int temp;
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
appDelegateClassName = NSStringFromClass([AppDelegate class]);
printf("hello world");
scanf("%d",&inputNumber);
temp = inputNumber;//小套路:提前存一下免得打印改变。
while (inputNumber > 1){
inputNumber /= 2;
ret++;
}
printf("%d是2的%d次幂",temp, ret);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}