小学生都看得懂的C语言入门(2): 判别 循环的一些应用实例
1.bool 类型
定义bool类型之前需要导入#include <stdbool.h>
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool b=>;
bool t=true;
printf("%d\n",b); //仍然是整数形式输出 得到1
return ;
}
2.逻辑运算
非! 或|| 和&& x>4 && x<6 !age<20 永远成立,!age=0或1 优先级顺序 ! && ||
赋值运算等级最低
// 短路 逻辑运算时从左向右的,左边已经可以觉得结果了,右边就不做了
//条件运算符 m<n? x:a+5 表示m<n 则 取x, 否则为a+5 ,这种表达式少用,会搞错 !!
//逗号 等级最低的
#include <stdio.h>
#include <stdbool.h>
int main()
{
int i=+, +; //出错了 为什么
printf("%d\n",i); //
int j=(+,+); // 正确
printf("%d\n",j); //
return ;
}
逗号什么用? 主要在for 中使用,可以放多个计算. for(i=0,j=10;i<j:i++,j--)...
3.分段函数
#include <stdio.h>
int main()
{
int x=;
int f=;
if (x<){
f=-;
}
else if(x==){
f=;
}
else{
f=*x;
}
printf("%d\n",f); //20
return ;
}
if else 后面只有一个执行语句的话,可以不加{}
#include <stdio.h>
int main()
{
int x=;
int f=;
if (x<)
f=-;
else if(x==)
f=;
else
f=*x;
printf("%d\n",f); //20
return ;
}
4.switch
为什么已经有了if 还需要用switch?
#include <stdio.h>
int main()
{
int type;
scanf("%d",&type); if (type==)
printf("nihao");
else if(type==)
printf("zaoshanghao");
else if(type==)
printf("wanshanghao") ;
else
printf("no") ; // 上述当type=4 需要一个一个判别过来,不太好 ,因此引入switch
return ;
}
用switch() {case 1:...; break; case 2:... ;break;...default:...;break;}
#include <stdio.h>
int main()
{
int type;
scanf("%d",&type);
switch(type){
case : //tpye=10 就直接做这个,
printf("nihao");
break; //一定要有break, 阻止语句往下执行
case :
printf("zaoshanghao");
break;
case : //tpye=30 就直接做这个,不用判别前面两种了
printf("wanshanghao");
break;
default:
printf("no");
return ;
}
注意: switch 只能用整数型, type不能是double!
#include <stdio.h>
int main()
{
//成绩转化
int grade;
scanf("%d",&grade) ;
grade/=;
switch(grade){
case :
case :
printf("A\n");
break;
case :
printf("B\n");
break;
case :
printf("C\n");
break;
case :
printf("D\n");
break;
default:
printf("F\n");
break;
}
return ;
}
5.简单小例子
#include <stdio.h>
int main()
{
// 计算log2的整数部分
int x;
int ret=;
scanf("%d",&x);
int t=x;
while(x>){
x/=;
ret++;
}
printf("log2 of %d is %d\n",t,ret);
return ;
}
12
log2 of 12 is 3
求平均数,输入数字, 以-1 结束,求出-1之前的平均数
#include <stdio.h>
int main()
{
int num;
int sum=;
int count=; scanf("%d",&num); while(num!=-){
sum+=num;
count++;
scanf("%d",&num);
} printf("%f\n",1.0*sum/count);
return ;
}
猜数字游戏 (1-100) 过程中需要提示:大了 小了,记录猜数字次数.
#include <stdio.h>
#include <stdlib.h> //rand 可以用了
#include <time.h>
int main()
{ srand(time()); //让随机数能够像随机数一样
int num=rand()%+; //产生一个1-100之间的随机数
int a=;
int count=; //猜数次数
printf("我已经想好了一个1到100内的数\n"); do{
printf("please guess a number:\n");
scanf("%d",&a);
count++; //无论对错都算一次
if (a>num)
printf("大了");
else if (a<num)
printf ("小了");
}while(a!=num); printf("bingo!,你用了%d次就成功了\n",count);
return ;
}
数字逆序输出 ,整数x逆序 作用x%10 得到个位数 x/10 去掉个位数,
#include <stdio.h>
int main()
{
int x=;
int t=x;
int num;
int ret=;
while(x>){
num=x%;
ret=ret*+num;
x/=;
}
printf("the reverse of %d is %d\n",t,ret);
return ;
}
the reverse of 500 is 5 , 没有得到005 有缺陷!
注意:容易出错的地方, if后面没有加{} 如果执行语句只有一句,可以不加,有多句一定要{} .
if() 后面不用加; 否则if 直接结束了,执行后面语句
#include <stdio.h>
int main()
{
int age=;
if (age>); //加了;
{
printf("can be treat carefully");
}
return ;
}
得到:
can be treat carefully 这个结果就是 if 在不执行设么就直接结束了,进入printf 语句
好的风格的代码.
#include <stdio.h>
int main()
{
int x=;
int f=;
if (x<)
{
f=-;
}
else if (x==)
{
f=;
}
else
{
f=*x;
}
return ;
}
上述优点在于如要注释几行不会影响其他行. 特别是{ }最会受注释行的影响.
//判断素数
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
int i;
int isprime=;
for(i=;i<x;i++)
{
if(x%i==)
{
isprime=;
// break; break也可以放在外面
} break;
}
if(isprime==)
printf("不是素数");
else
printf("是素数");
return ;
}
13
是素数
//输出30以内的素数 双重循环
#include <stdio.h>
int main()
{
int x;
for(x=;x<;x++)
{
int i;
int isprime=;
for(i=;i<x;i++)
{
if(x%i==)
{
isprime=;
} break;
}
if(isprime==)
printf("%d ",x);
}
return ;
}
2 3 5 7 9 11 13 15 17 19 21 23 25 27 29
// 输出前20个素数
#include <stdio.h>
int main()
{
int x=;
int cnt=;
while(cnt<)
{
int i;
int isprime=;
for(i=;i<x;i++) //x还是会输出2的
{
if(x%i==)
{
isprime=;
} break;
}
if(isprime==)
{
printf("%d ",x);
cnt++;
}
x++;
}
return ;
}
2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
//另一种方法, 不用while, 用for
#include <stdio.h>
int main()
{
int x;
int cnt=;
for(x=;cnt<;x++)
{
int i;
int isprime=;
for(i=;i<x;i++) //x还是会输出2的, 但是只有一次循环
{
if(x%i==)
{
isprime=;
} break;
}
if(isprime==)
{
printf("%d ",x);
cnt++;
}
}
return ;
}
//凑硬币 1 2 5元 凑成100元以下的金额 (输出第一个符合条件的即可) 三重循环
#include <stdio.h>
int main()
{
int x;
int one ,two,five;
int exit=;
scanf("%d",&x);
for (one=;one<x;one++){
for (two=;two<x/;two++){
for(five=;five<x/;five++){
if(one+*two+five*==x){
printf("用%d个一元 %d个两元 %d个五元得到%d元\n",
one,two,five,x);
exit=;
break; //只能结束当前循环,如何跳出三个循环 ?增加exit变量 接力break
}
}
if (exit==)break;
}
if (exit==)break;
}
}
23
用1个一元 6个两元 2个五元得到23元
//上述称为接力break, 还有一种方法 用goto, 可以免去写很多个break
#include <stdio.h>
int main()
{
int x;
int one,two,five; scanf("%d",&x);
for (one=;one<x;one++)
{
for (two=;two<x/;two++)
{
for(five=;five<x/;five++){
if(one+*two+five*==x){
goto out; //后面定out的位置
}
}
} }
out:
printf("用%d个一元%d个两元 %d个五元得到%d元\n",one,two,five,x);
}
12
用1个一元3个两元 1个五元得到12元
//求和 1+1/2+1/3+1/4...+1/n
#include <stdio.h>
int main()
{
int n;
int i;
double sum=0.0;
scanf("%d",&n);
for(i=;i<=n;i++){
sum+=1.0/i;
}
printf("f(%d)=%f\n",n,sum) ;
}
10
f(10)=2.928968
//求和 1-1/2+1/3-1/4...+(-1)^(n-1)*1/n, 增加sign符号
#include <stdio.h>
int main()
{
int n;
int i;
int sign=;
double sum=0.0;
scanf("%d",&n);
for(i=;i<=n;i++){
sum+=sign*1.0/i;
sign=-sign;
}
printf("f(%d)=%f\n",n,sum) ;
}
4
f(4)=0.583333
//求两个数的最大公约数
方法1: 先找到两数的较小者min , 循环 i=1:min找公因子, 循环结束得到的就是最后一个(最大公因数)
#include <stdio.h>
int main()
{
int a,b;
int min ;
scanf("%d %d",&a,&b);
if (a<b){
min=a;
} else{
min=b;
}
int ret=;
int i;
for(i=;i<min;i++){
if (a%i== && b%i==){
ret=i;
}
}
printf("%d and %d 的最大公约数是%d",a,b,ret);
}
10 25
10 and 25 的最大公约数是5
//方法2: 辗转相除法 a=12, b=18 ,t记录a%b 然后a=b, b=t, 继续做下去
思路:
// a b t
//12 18 12
//18 12 6
//12 6 0
//6 0 b是0的时候结束, 此时a是最小公约数
#include <stdio.h>
int main()
{
int a,b;
int t;
scanf("%d %d",&a,&b) ;
while(b!=){
t=a%b;
a=b;
b=t;
}
printf("gcd=%d\n",a);
}
//正序分解整数 12345 输出 1 2 3 4 5
//先回顾逆序输出... x%10 得到最后一个数字. x/10去掉最后一个数
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
//x=12345;
do{
int d=x%; //得到个数数字
printf("%d ",d);
x/=;
} while(x>);
}
1200
0 0 2 1
// 正序可以通过逆序两次得到
#include <stdio.h>
int main()
{
int x;
int t=;
scanf("%d",&x);
//x=12345;
do{
int d=x%; //得到个数数字
t=t*+d;
x/=;
} while(x>);
printf("t=%d\n",t);
x=t;
do{
int d=x%; //得到个数数字
printf("%d ",d);
x/=;
} while(x>);
}
1329
t=9231
1 3 2 9
但是输入600 得到的 t=6 最后结果也是6 不是 006,修改思路如下
// 思路 : x=1234
//1234/1000 1 ..
//1234%1000 234
//1000/10 100
//234/100 2. ..
//234%100 34
//100/10 10
//34/10 3. ..
//34%10 4
//10/10 1
//4/1 4 ...
//4%1 0
//1/10 0
#include <stdio.h>
#include<math.h>
int main()
{
int x;
scanf("%d",&x);
int t=x;
int cnt=; //计算位数
do{
x/=;
cnt++;
} while(x>);
printf("%d\n",cnt);
int m=pow(,cnt-);// pow没有定义?? 增加#include<math.h>
printf("%d\n",m);
x=t;
do{
int d=x/m;
printf("%d ",d);
x%=m;
m/=;
}while(m>);
}
123
3
100
1 2 3
上述没必要用pow求
#include <stdio.h>
#include<math.h>
int main()
{
int x;
scanf("%d",&x);
int m=;
int t=x;
while(x>){
x/=;
m*=;
} x=t;
do{
int d=x/m;
printf("%d ",d);
x%=m;
m/=;
}while(m>);
}
小学生都看得懂的C语言入门(2): 判别 循环的一些应用实例的更多相关文章
- 小学生都看得懂的C语言入门(1): 基础/判别/循环
c基础入门, 小学生也可以都看得懂!!!! 安装一个编译器, 这方面我不太懂, 安装了DEV-C++ ,体积不大,30M左右吧, 感觉挺好用,初学者够了. 介绍下DEV 的快键键: 恢复 Ctrl+ ...
- 小学生都看得懂的C语言入门(5): 指针
现在已经学到C语言的后面了, 快学完咯.... (一)取地址运算 先来看一下sizeof 计算所占字节 #include<stdio.h> int main() { int a; a=; ...
- 小学生都看得懂的C语言入门(6): 字符串
1.字符用 char 表示 #include<stdio.h> int main() { char c; char d; c=; d='; if (c==d){ printf(" ...
- 小学生都看得懂的C语言入门(4): 数组与函数
// 之前判断素数, 只需要到sqrt(x)即可,//更加简单的, 判断能够比已知的小于x的素数整除, 运行更快 #include <stdio.h> // 之前判断素数, 只需要到sqr ...
- 小学生都看得懂的C语言入门(3): 数组与函数
#include <stdio.h> int main() { int x; ; ; scanf("%d",&x){ sum+=x; cnt++; scanf( ...
- 小学生都能读懂的网络协议之:WebSocket
目录 简介 webSocket vs HTTP HTTP upgrade header websocket的优点 webScoket的应用 websocket的握手流程 WebSocket API 总 ...
- 胖虎都看得懂的CSS入门
CSS入门 CSS是什么 摘自维基百科 层叠样式表(英语:Cascading Style Sheets,简写CSS),又称串样式列表.级联样式表.串接样式表.层叠样式表.階層式樣式表,一种用来为结构化 ...
- 小白都看得懂的Javadoc上手教程
Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...
- 小白都看得懂的Javadoc使用教程
Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...
随机推荐
- 使用Docker部署javaWeb应用
1. 安装Dcoker http://www.cnblogs.com/zhangqian27/p/9089815.html 2. 查看镜像 $ docker images 3. 搜索镜像 $ dock ...
- 转:Java项目开发规范参考
Java项目开发规范参考 - KevinLee的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/u011383131/article/details/51227860 ...
- Python笔记 【无序】 【二】
序列list() ——把一个可迭代对象[可以是字符串,元组]转化为列表,可不带参数——生成空列表,或者带一个迭代器作为参数tuple() ——可迭代对象转化为元组str(obj) ——把obj对象转换 ...
- 【Ubuntu】如何修改IP
前几天有幸捣鼓了一下Ubuntu系统,和Linux系统差不多,在这里说说如何修改IP 1,首先使用命令ifconfig查看当前IP,如图 2,编辑文件,输入命令 ...
- Linux内核调试:kdump、vmcore、crash、kernel-debuginfo【转】
转自:https://blog.csdn.net/guowenyan001/article/details/19807555 一.简介 linux内核发送崩溃时,kdump会生成一个内核转储文件vmc ...
- zookeeper3.4.6配置实现自动清理日志【转】
在使用zookeeper过程中,我们知道,会有dataDir和dataLogDir两个目录,分别用于snapshot和事务日志的输出(默认情况下只有dataDir目录,snapshot和事务日志都保存 ...
- CLR via C#关于泛型(Generics )的摘录
泛型,是CLR和编程语言提供的一种特殊机制,它支持另一种形式的代码重用,即“算法重用”. 简单的说,开发人员先定义好一个算法,比如排序.搜索.交换.比较或者转换等.但是,定义算法的开发人员并不设改算法 ...
- nginx 负载 问题
1 如果使用ip_hash,nginx必须为最前端负载均衡,如果大网环境部署,基本无法实现,内网还可以使用 2 如果不使用ip_hash,则要考虑session问题,可以使用memcached与tom ...
- centos6.5环境wget报错Unable to establish SSL connection
centos6.5环境wget报错Unable to establish SSL connection [root@centossz008 src]# wget --no-check-certific ...
- Thread Synchronization Queue with Boost
介绍:当开发一个多线程程序时,同步是一个很大的问题.如果你的程序需要数据流包,那么用队列是个好办法. 你可以在 http://www.boost.org/ 发现 boost 库和文档,从它的网站可以看 ...