您的位置:首页 > 移动开发 > IOS开发

ios--c DAY_3

2015-08-03 08:09 417 查看
//

// main.m

// LessonThree

//

// Created by lanou3g on 15/7/30.

// Copyright (c) 2015年 lanou3g. All rights reserved.

//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

//分支:通过匹配条件,选择性的执行某一个分支

//循环:当循环条件为真的时候,一直执行,直到条件变为假.

int count=0;

//打印 一万遍“我爱你”

for (int i=0; i<10; i++) {

count++;

printf("我爱你\t");

if (count%5==0) {

printf("\n");

}

}

//while(条件表达式){ 语法;(循环体)}

//条件表达式的结果:真或者假。

//个人需要注意的地方就是,循环增量,自己经常忘记写了,会造成死循环

//循环变量

count=0;

//循环条件

while (count<=9) {

//循环语句

printf("我爱你\t");

//循环增量

count++;

//控制输出排列

if (count%5==0) {

printf("\n");

}

}

//用 while 打印1-10的数

count=1;

while (count<=10) {

printf("%d\t",count);

count++;

}

//用 while 打印6-12之间的数

count=6;

while (count<=12) {

printf("%d\t",count++);

}

printf("\n");

//倒序 打印 10-50 之间的数

count=50;

while (count>=10) {

printf("%d\t",count--);

if (count%10==0) {

printf("\n");

}

}

printf("\n");

//打印 1~100 之间7的倍数

count = 1;

int count7=0;

while (count<=100) {

if (count%7==0) {

count7++;

printf("%d\t",count);

if (count7%10==0) {

printf("\n");

}

}

count++;

}

printf("\n");

//代码位置不一样--输出形式不一样

count = 1;

count7=0;

while (count<=100) {

//count=0~6,count7=0,打印7个回车

//count=7,count7=1......

//count=70,count7=10.打印回车

//count=71~77,count7=10,打印7个回车

//以上分析就是,为什么会中间隔那么多空格的缘故

//这段代码和上一段代码就值一个if语句的位置不一样,就会造成输出的形式是完全不一样 的,所以,在写代码的过程中,一定要注意代码的位置,也就是逻辑思路一定要很清晰。

if (count%7==0) {

count7++;

printf("%d\t",count);

}

count++;

if (count7%10==0) {

printf("\n");

}

}

printf("\n");

//实现1-100之间,个位是7的数

count=1;

while (count<=100) {

if (count%10==7) {

printf("%d\t",count);

}

count++;

}

printf("\n");

//打印 1-100之间的十位为7的数

//做这样的题目一定要观察规律,除法在计算机中最终得到的一定是整数,利用这个就可以很快的计算出结果了。

count=1;

while (count<=100) {

if (count/10==7) {

printf("%d\t",count);

}

count++;

}

//计算1-5之间的和

count=1;

int sum=0;

while (count<=5) {

sum+=count;

count++;

}

printf("sum=%d\n",sum);

//随机数:

arc4random();

//返回一个无符号的32位整型数字,如果用int(%d)来接收,int 的最高位为符号位,当符号位为1的时候,得到的就是负数。随机数用%u打印。

printf("%u\n",arc4random());

//获取某个范围的随机数

//1)、获取[0,b]范围的随机数,arc4random()%(b+1),余数一定会小于除数

//获取0--9之间的一个随机数

printf("%d\n",arc4random()%10);

//获取1---9之间的一个随机数

printf("%d\n",(arc4random()%10)+1);

//获取[a,b]范围的随机数,arc4random()%(b-a+1)+a;

//输出[20,40]之间的数

printf("%d\n",arc4random()%(40-20+1)+20);

//从控制台输入一个数n,打印n个[10,20]之间的随机数

int n=0;

printf("请输入一个数n:\n");

scanf("%d",&n);

while (n>0) {

printf("%d\t",arc4random()%11+10);

n--;

}

printf("\n");

// 从控制台输入一个数,产生随机数,输出其中最小的数

n=0;

printf("输入一个数:\n");

scanf("%d",&n);

int numberSort[8]={0};

int i=0,min=13;

while (n>0) {

numberSort[i]=arc4random()%30;

printf("%d\t",numberSort[i]);

if (numberSort[i]<min) {

min=numberSort[i];

}

i++;

n--;

}

printf("\n%d\n",min);

//break语句作用:提前结束循环

// 1)、可以用来从循环体内跳出循环体,即提前结束循环,接着执行循环外面的语句;

// 2)、使流程跳出switch结构

// 注意:break语句不能用于循环语句和switch语句之外的任何语句。

//continue语句作用:提前结束本轮循环

//忽略循环体中continue语句下面尚未执行的语句,接着进行下一次是否执行循环的决定

//注意:continue语句不能用于循环语句之外的其他语句中。

//3、continue和break的区别:

//1)、continue语句只结束本次循环,而不是终止整个循环的执行;

//2)、break语句则是结束整个循环过程,不再判断执行循环的条件是否成立。break语句可以用在循环语句和switch语句中。在循环语句中用来结束内部循环;在switch语句中用来跳出switch语句。

//4、注意:循环嵌套时,Break和continue只影响包含它们的最内层循环,与外层循环无关。

int nBreak=5;

while (nBreak--) {

printf("%d\t",nBreak);

}

printf("\n");

//输出1-50之间的数,当数为40的时候,停止输出

int numberWhile=50;

while (numberWhile--) {

if (numberWhile>=40&&numberWhile<=50) {

continue;

}

printf("%d\t",numberWhile);

}

//[1,50]当遇到10的倍数的时候不输出

i=1;

while (i<=50) {

if (i%10==0) {

i++; //为甚要在这增加循环增量,continue是跳出本次循环,遇到continue,循环体内continue后面的语句就不执行了,转向去判断循环条件,再继续下一次循环

printf("\n");

continue;

}

printf("%d\t",i);

i++;

}

printf("\n");

//运行逻辑:

//先执行一次,再判断

// do {

// 语法; ----循环体

// } while (条件表达式);

do {

printf("你打我啊!\n");

} while (2>6);

//执行顺序1-2-3-4--2-3-4-2-3-4-2(w为假)-5

// for (初始值(可省略)1; 判断条件2; 循环增量4) {

// 语法;---循环体3

// }5

printf("奇数:");

for (int i=0; i<8; i++) {

if (i%2==0) {

continue;

}

printf("%d\t",i);

}

printf("\n");

//练习:

printf("7的倍数:\n");

int countFor=0;

for (int i=1; i<101; i++) {

if (i%7==0) {

countFor++;

printf("%d\t",i);

if (countFor%10==0) {

printf("\n");

}

}

}

printf("1、循环结束.\n\n");

printf("个位为7的数:\n");

countFor=0;

for (int i=1; i<101; i++) {

if (i%10==7) {

printf("%d\t",i);

countFor++;

if (countFor%10==0) {

printf("\n");

}

}

}

printf("2、循环结束.\n");

printf("十位是7的数:\n");

countFor=0;

for (int i=1; i<101; i++) {

if (i/10==7) {

countFor++;

printf("%d\t",i);

if (countFor%10==0) {

printf("\n");

}

}

}

printf("3、循环结束.\n");

countFor=0;

printf("不是7的倍数,并且不包含7的数:\n");

for (int i=1; i<101; i++) {

if (i%7!=0) {

if (i%10!=7) {

if (i/10!=7) {

printf("%d\t",i);

countFor++;

if (countFor%10==0) {

printf("\n");

}

}

}

}

}

printf("4、循环结束.\n");

//打印出

// 1 2 3 4

// 1 2 3 4

// 1 2 3 4

// 1 2 3 4

for (int i=1; i<=4; i++) {//控制行数

for (int i=1; i<=4; i++) {//控制列数

printf("%d\t",i);//第一列1 第二列2 第三列3 第四列4

}

printf("\n");//当所有的列都输出完毕之后,换行

}

// 1 ---第一行 第一列打印1个数字

// 1 2 ---第二行 第二列打印2个数字

// 1 2 3 ---大三行 第三列打印3个数字

//最终总结 第i行 就打印i个数字

//所以控制列数的j,条件判断的时候就跟行数i有关系了。

//总之一句话,就是要仔细观察

for (int i=1; i<=3; i++) {

for (int j=1; j<=i; j++) {

printf("%d\t",j);

}

printf("\n");

}

//练习:

//打印

// 2

// 2 3

// 2 3 4

// 2 3 4 5

for (int i=2; i<6; i++) {

for (int j=2; j<=i; j++) {

printf("%d\t",j);

}

printf("\n");

}

//打印乘法口诀

for (int i=1; i<=9; i++) {

for (int j=1; j<=i; j++) {

printf("%d\t",j*i);

}

printf("\n");

}

//打印三个数字(1-9)的组合的所有可能[三位数]

int countForNumber=0;

for (int i=1; i<=9; i++) {

for (int j=1; j<=9; j++) {

for (int m=1; m<=9; m++) {

printf("%d%d%d\t",i,j,m);

countForNumber++;

if (countForNumber%20==0) {

printf("\n");

}

}

}

}

printf("\n一共有%d种组合.\n",countForNumber);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: