生活随笔
收集整理的這篇文章主要介紹了
算法学习之循环结构程序设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
for循環
打印1,2,3,...,n每個占一行。
#include <conio.h>
#include<stdio.h>
int main(){int i,n;scanf("%d",&
n);for(i=
1;i<=n;i++
){printf("%d\n",i);}getch();return 0;
}
分支結合循環,威力很強大
輸出所有形如aabb的四位完全平方數。
分析:先全部列出來,然后開根,看是否為整數。其中a可以從1到9,
b可以從0到9。如何組成aabb呢?
a*1100+b*11即可。
偽代碼可以開拓思路。
下面來實現它
#include <conio.h>
#include<stdio.h>
#include<math.h>
int main(){int a,b,n;double m;for(a=
1;a<=
9;a++
){for(b=
0;b<=
9;b++
){n = a*
1100+b*
11;m =
sqrt(n);if((
int)m == m){
//判斷是否為整數printf(
"%d\n",n);}}}getch();return 0;
} 另一個思路是枚舉平方根x,從而避免開平方操作。
#include <conio.h>
#include<stdio.h>
#include<math.h>
int main(){int x,n,high,low;for(x =
1;;x++
){n = x*
x;if(n<
1100)
continue;
//跳過本次循環if(n>
9999)
break;
//結束本次循環,這一句很重要high = n /
100;
//獲取高位,技巧low = n %
100;
//獲取低位,技巧啊,余下的不滿足100的就是低位的數字if(high/
10 == high%
10&&low/
10 == low%
10){
//前兩位相同,后兩位相同,則滿足條件printf(
"%d\n",n);}}getch();return 0;
} 這個效率貌似不如第一個高!執行的時間比較久,如果沒有終止操作,不知道要執行多久呢!
這個大概就是算法的復雜度吧!一定要有意識提高代碼的效率!
3n+1問題
猜想:對于任意大于1的自然數n,若n為奇數,則將n變為3n+1,否則變為n的一般。經過若干次這樣的變換,
一定會使n變為1。例如3->10->5->16->8->4->2->1。
輸入n,輸出變換的次數。n<=10的9次方。
樣例輸入:3
樣例輸出:7
分析:程序完成工作依然是重復性勞動,循環的次數是不確定的,而且n不是遞增的循環。可以考慮用
while條件式的循環來實現。
#include <conio.h>
#include<stdio.h>
int main(){int n,count =
0;
//定義變量n,count并為count賦初值scanf(
"%d",&
n);while(n>
1){
//只要n不等于1,這里n>0的整數,也就是只要n大于1,就執行操作if(n%
2 ==
1){
//奇數n =
3*n+
1; }else{n = n/
2;}count++
; }printf("%d\n",count);
//不要忘記測試,一個看上去正確的程序可能隱含錯誤。//斷點輸出是很好的排錯方式//printf("%d\n",n);
getch();return 0;
} for與while的關系
"
for(初始化;條件;調整){
?? ?循環體
}
"
等價于
"
初始化
while(條件)
{
? 循環體;
? 調整;
}
"
階乘之和
輸入n,計算S = 1!+2!+3!+...+n!的末6位(不含前導0)。n<=10的6次方。
樣例輸入:10
樣例輸出:37913(其實總和為4037913)
分析:如何實現階乘呢?用一次循環唄。
#include <conio.h>
#include<stdio.h>
int main(){int n,i,j,S=
0;scanf("%d",&
n);for(i=
1;i<=n;i++
){int factorial =
1;
//定義階乘,每次循環都被重新賦初值for(j=
1;j<=i;j++
){factorial *=
j;}S +=
factorial;}S = S%
1000000;
//有幾個0就會剩下幾位//S = int(S);printf(
"%d\n",S);getch();return 0;
} 每步取模,加一個“計時器”,可以計算出時間復雜度。
輸入一些整數,求出它們的最小值、最大值和平均值(保留3位小數)。輸入保證這些數都是不超過1000的整數。
樣例輸入:2 8 3 5 1 7 3 6
樣例輸出:1 8 4.375
#include <conio.h>
#include<stdio.h>
#define INF 1000000000
int main(){int x,n =
0,min = INF,max = -INF,s=
0;while(n!=
8&&scanf(
"%d",&x) ==
1){s +=
x;if(x<min) min =
x;if(x>max) max =
x;n++
;}printf("%d %d %.3lf\n",min,max,(
double)s/
n);getch();return 0;
} 下面將其改造成讀文件,寫文件的形式
#define LOCAL
#include <conio.h>
#include<stdio.h>
#define INF 1000000000
int main(){//讀取文件
#ifdef LOCALfreopen("in.txt",
"r",stdin);freopen("out.txt",
"w",stdout);#endifint x,n =
0,min = INF,max = -INF,s=
0;while(n!=
8&&scanf(
"%d",&x) ==
1){
//如果前面不通過,就不執行后面的內容了s +=
x;if(x<min) min =
x;if(x>max) max =
x;/*printf("x = %d,min = %d,max = %d\n",x,min,max);*/n++
;}printf("%d %d %.3lf\n",min,max,(
double)s/
n);getch();return 0;
} 讀取當前文件夾下的in.txt
并將結果輸出到out.txt中
輸出2,4,6,8...2n
方法1
#include <conio.h>
#include<stdio.h>
int main(){int i,n;scanf("%d",&
n);for(i=
2;i<=
2*n;i+=
2){printf("%d\n",i);}getch();return 0;
}
方法2
#include <conio.h>
#include<stdio.h>
int main(){int i,n;scanf("%d",&
n);for(i=
1;i<=n;i++
){printf("%d\n",
2*
i);}getch();return 0;
} 浮點陷阱
#include <conio.h>
#include<stdio.h>
int main(){double i;for(i=
0;(
int)i!=
10;i+=
0.1)
//如果不加(int)就永遠不會等于10,程序永遠不停止!
{printf("%.1lf\n",i);}getch();return 0;
}
C++中的輸入輸出
方式1
#include <conio.h>
//C++中保留了C語言中常用的頭文件,如果你愿意,可以繼續使用stdio.h
#include<cstdio>
//功能和C中的stdio.h很接近
using namespace std;
//C++特有的單行注釋,可以和C中傳統注釋/**/混合使用
int main(){int a,b;while(scanf(
"%d%d",&a,&b)==
2){printf("%d\n",a+
b);}getch();return 0;
}
方式2(用iostream)
#include <conio.h>
//C++中保留了C語言中常用的頭文件,如果你愿意,可以繼續使用stdio.h
#include<iostream>
//C++中的輸入輸出頭文件
using namespace std;
//C++特有的單行注釋,可以和C中傳統注釋/**/混合使用
int main(){int a,b;while(cin>>a>>b){
//張嘴送給cincout<<a+b<<
"\n";
//張嘴送給a+b
}getch();return 0;
}
C++中讀取文件
#include <conio.h>
#include<fstream>
using namespace std;
ifstream fin("in.txt");
ofstream fout("out.txt");
int main(){int a,b;while(fin>>a>>b){
//張嘴送給cinfout<<a+b<<
"\n";
//張嘴送給a+b
}getch();return 0;
}
總結
以上是生活随笔為你收集整理的算法学习之循环结构程序设计的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。