第二章:循环结构程序设计

2018-10-19-16:25:34

随学笔记

小计:

<1>: for语句   [for(初始化;条件;调整) ]   执行时先完成赋值操作,判断满足条件后执行循环体,之后的每次执行都是先完成调整操作,判断满足条件后继续执行循环体,不满足条件就退出循环。

 1 #include <cstdio>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     for(int i=0;i<0;i++)
 9       printf("%d\n",i);
10     return 0;
11 }
12 //程序无输出内容

 <2>: 判断一个数是否为完全平方数

  利用恒等式1+3+5+7+···+2*n-1=n*n;

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 bool isSqrt(int n);
 5 
 6 int main()
 7 {
 8     int n;
 9     while(~scanf("%d",&n)){
10       if(isSqrt(n)){
11         printf("isSqrt\n");
12         continue;
13       }
14       printf("isn't sqrt\n");
15     }
16     return 0;
17 }
18 bool isSqrt(int n){
19   for(int i=1;n>0;i+=2)
20     n-=i;
21   return 0==n;
22 }

 <3>: floor函数:其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整是直接取按照数轴上最接近要求值的左边值,即不大于要求值的最大的那个值。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     double c=3.3;
 9     printf("%.1f",floor(c));
10     return 0;
11 }

//程序输出为3.0
//double floor(double n);
//floor()函数头文件为#include<cmath>

 <4>: 浮点数运算可能存在误差,浮点数比较时要考虑到浮点误差。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 bool isSqrt(int n);
 6 
 7 int main()
 8 {
 9     int n;
10     while(~scanf("%d",&n)){
11       if(isSqrt(n)){
12         printf("isSqrt\n");
13         continue;
14       }
15       printf("Isn't sqrt\n");
16     }
17     return 0;
18 }
19 bool isSqrt(int n){
20     if(sqrt(n)==floor(sqrt(n)+0.5))//浮点运算误差较小,不会超过0.5
21       return true;
22     return false;
23 }

<5>: while语句和do-while语句用法相同,唯一的区别是while先判断条件是否成立,成立即开始循环,do-while则是先做一次再判断是否成立,随后便和while一样运作。

可以利用do-while的这一特性解决一些特殊的问题(循环终止判断在计算之后的情况很适用于do-while语句)。

例如求integer的位数:

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n;
 8     while(~scanf("%d",&n)){
 9       int cnt=0;
10       do{
11         n/=10;
12         cnt++;
13       }while(n);//do-while语句避免了需要考虑n==0的情况
14     printf("%d\n",cnt);
15     }
16     return 0;
17 }

<6>: int_32, int_64, uint_32,uint_64(C语言数据类型)

    C99规定int至少是16位,却没有具体的值,所以C99规定了以上数据类型,算法竞赛平台一般相对稳点,int为32位数。

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7 /*
 8 typedef int  int32_t;
  typedef unsigned int32_t uint32_t
9 typedef long long int int64_t;
  typedef unsigned long long int uint64_t;
10 */ 11 printf("%d\n",sizeof(int32_t));//put 4 12 printf("%d\n",sizeof(int64_t));//put 8 13 /* 14 int32_t    -2147483648~2147483647  15 uint32_t 0~4294967295 16 int64_t -9223372036854775808~9223372036854775807 17 uint64_t  0~18446744073709551615 18 */ 19 return 0; 20 }

<7>: C语言中long long int 在linux下输入格式为%lld,在windows下输入为%I64d。

<8>: 函数clock()返回程序目前为止的运行时间,这个时间除以常数CLOCKS_PER_SEC之后得到的值以秒为单位。

 1 #include <cstdio>
 2 #include <ctime>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     printf("Time is %.2f S\n",(double)clock()/CLOCKS_PER_SEC);
 9     return 0;
10 }

<10>: scanf函数在遇到空格,回车换行时会自动结束。

<11>: C语言文件读写。

   函数名:freopen 

声明:FILE *freopen( const char *path, const char *mode, FILE *stream ); 
所在文件: #include<cstdio>
参数说明: 
path: 文件名,用于存储输入输出的自定义文件名。 
mode: 文件打开的模式。和fopen中的模式(如r-只读, w-写)相同。 
stream: 一个文件,通常使用标准流文件。 
返回值:成功则返回一个path所指定文件的指针;失败返回NULL。(一般可以不使用它的返回值) 
功能:实现重定向,把预定义的标准流文件定向到由path指定的文件中。标准流文件具体是指stdin、stdout和stderr。其中stdin是标准输入流,默认为键盘;stdout是标准输出流,默认为屏幕;
stderr是标准错误流,一般把屏幕设为默认。
 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a,b;
 8     freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取       "r":为了输入数据,只读
 9     freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中    "w":为了输出数据,只写
10     while(~scanf("%d %d",&a,&b))
11       printf("%d\n",a+b); 
12     fclose(stdin);//关闭重定向输入
13     fclose(stdout);//关闭重定向输出
14     return 0;
15 }
//重定向使用完毕后记得要取消重定向

习题2-5 分数化小数

思路:分步输出

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a,b,c,Case=0;
 8     while(~scanf("%d %d %d",&a,&b,&c)&&(a|b|c)){
 9       printf("Case %d : %d.",++Case,a/b);
10       while(--c>0){
11         a*=10;
12         printf("%d",a/b);
13         a%=b;
14       }
15       a*=10;
16       printf("%d\n",a/b>5?(a/b+1):(a/b));
17     }
18     return 0;
19 }

 习题2-6 排列

简单将每一位初始化为1,最后将各位相加结果为9即输出。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int index[10];
 9     for(int i=123;i<=321;i++){
10       memset(index,0,sizeof(index));
11       index[i/100]=1;
12           index[i/10%10]=1;
13           index[i%10]=1;
14           int j=i*2;
15           index[j/100]=1;
16           index[j/10%10]=1;
17           index[j%10]=1;
18           int k=i*3;
19           index[k/100]=1;
20           index[k/10%10]=1;
21           index[k%10]=1;
22       int sum=0;
23       for(int h=1;h<10;h++)
24           sum+=index[h];
25       if(sum==9)
26         printf("%d\t%d\t%d\n",i,j,k);
27     }
28     return 0;
29 }
时间并不会因为你的迷茫和迟疑而停留,就在你看这篇文章的同时,不知道有多少人在冥思苦想,在为算法废寝忘食,不知道有多少人在狂热地拍着代码,不知道又有多少提交一遍又一遍地刷新着OJ的status页面…… 没有谁生来就是神牛,而千里之行,始于足下!
原文地址:https://www.cnblogs.com/bianjunting/p/9817171.html