程序設(shè)計(jì)導(dǎo)論-2 函數(shù)與控制流 偽代碼實(shí)現(xiàn)

分支結(jié)構(gòu)

循環(huán)結(jié)構(gòu)

求近似的圓周率

C

#include<stdio.h>
#include<math.h>
int main(){
    int n;
    scanf("%d",&n);
    double pi = 0;
    double cnt = 0;
    double item = 1;
    while(fabs(item)>pow(0.1,n)){
        item = pow(-1,cnt)/(2*cnt+1);
        pi = pi + item *4;
        cnt = cnt + 1;
    }
    printf("%lf",pi);
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int n;
    cin>>n;
    double pi = 0;
    double cnt = 0;
    double item = 1;
    while(fabs(item)>pow(0.1,n)){
        item = pow(-1,cnt)/(2*cnt+1);
        pi = pi + item *4;
        cnt = cnt + 1;
    }
    cout<<pi<<end;
    return 0;
}

Java

public class piapprox{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double pi = 0;
        double cnt = 0;
        double item = 1;
        while(Math.abs(item)>Math.pow(0.1,n)){
            item = Math.pow(-1,cnt)/(2*cnt+1);
            pi = pi + item *4;
            cnt = cnt + 1;
        }
        System.out.println(pi);
    }
}

Python

n = int(input())
pi = 0.
cnt = 0.
item = 1
while abs(item) > pow(0.1,n):
    item = pow(-1,cnt)  /(2*cnt+1)
    pi = pi + item*4
    cnt = cnt +1
print(pi)

求近似的自然對(duì)數(shù)e

C

#include<stdio.h>
#include<math.h>
int prod(int n){
    int prd = 1;
    for(int i = 1; i<= n;i++){
        prd = prd * i;
    }
    return prd;
}
int main(){
    int n;
    scanf("%d",&n);
    double e = 0;
    int cnt = 0;
    double item = 1;
    while(fabs(item)>pow(0.1,n)){
        item = 1.0 / prod(cnt);
        e = e + item;
        cnt = cnt + 1;
    }
    printf("%lf",e);
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int prod(int n){
    int prd = 1;
    for(int i = 1; i<= n;i++){
        prd = prd * i;
    }
    return prd;
}
int main(){
    int n;
    cin>>n;
    double e = 0;
    int cnt = 0;
    double item = 1;
    while(fabs(item)>pow(0.1,n)){
        item = 1.0 / prod(cnt);
        e = e + item;
        cnt = cnt + 1;
    }
    cout<<e<<endl;
    return 0;
}

Java

public class eapprox{
    public static int prod(int n){
        int prd = 1;
        for(int i = 1; i<= n;i++){
            prd = prd * i;
        }
        return prd;
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double e = 0;
        int cnt = 0;
        double item = 1;
        while(Math.abs(item)>Math.pow(0.1,n)){
            item = 1.0 / prod(cnt);
            e = e + item;
            cnt = cnt + 1;
        }
        System.out.println(e);
    }
}

Python

def prod(n):
    prd = 1.0
    for i in range(1,n+1):
        prd = prd * i
    return prd
n = int(input())
e = 0
cnt = 0
item = 1
while abs(item) > pow(0.1,n):
    item = 1.0  /prod(cnt)
    e = e + item
    cnt = cnt +1
print(e)

遞歸

斐波那契數(shù)列

C

#include<stdio.h>
#include<math.h>
int fib(int n){
    if(n==1){
        return 1;
    }
    if(n==2){
        return 1;
    }
    return fib(n-1)+fib(n-2);
}
int main(){
    int n;
    scanf("%d",&n);
    printf("%d",fib(n));
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int fib(int n){
    if(n==1){
        return 1;
    }
    if(n==2){
        return 1;
    }
    return fib(n-1)+fib(n-2);
}
int main(){
    int n;
    cin>>n;
    cout<<fib(n)<<endl;
    return 0;
}

Java

public class fib{
    public static int fib(int n){
        if(n==1){
            return 1;
        }
        if(n==2){
            return 1;
        }
        return fib(n-1)+fib(n-2);
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(fib(n));
    }
}

Python

nin = int(input())
def fib(n):
    if n == 1:
        return 1
    if n == 2:
        return 1
    return fib(n-1) + fib(n-2)
print(fib(nin))

分治

二分搜索

C

#include<stdio.h>
#include<math.h>
int binarysearch(int arr[],int l,int r,int x){
    int mid = l+r/2;
    if (arr[mid]==x){
        return mid;
    }
    if (l >= r)
        return -1;
    else if(arr[mid] >x){
        return binarysearch(arr,l,mid-1,x);
    }
    else
        return binarysearch(arr,mid+1,r,x);
}
int main(){
    int n;
    int arr[1000];
    int x;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    scanf("%d",&x);
    printf("%d",binarysearch(arr,0,n,x));
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int binarysearch(int arr[],int l,int r,int x){
    int mid = l+r/2;
    if (arr[mid]==x){
        return mid;
    }
    if (l >= r)
        return -1;
    else if(arr[mid] >x){
        return binarysearch(arr,l,mid-1,x);
    }
    else
        return binarysearch(arr,mid+1,r,x);
}
int main(){
    int n;
    int arr[1000];
    int x;
    cin>>n;
    for(int i = 0;i<n;i++){
        cin>>arr[i];
    }
    cin>>x;
    cout<<binarysearch(arr,0,n-1,x)<<endl;
    return 0;
}

Java

class binarysea{
    public static int binarysearch(int arr[],int l,int r,int x){
        int mid = (l+r)/2;
        if (arr[mid]==x){
            return mid;
        }
        else if (l >= r){
            return -1;
        }
        else if(arr[mid] >x){
            return binarysearch(arr,l,mid-1,x);
        }
        else
            return binarysearch(arr,mid+1,r,x);
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[] = new int[1000];
        for(int i = 0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        int x = sc.nextInt();
        System.out.println(binarysearch(arr,0,n,x));
    }
}

Python

arr = input().split(" ")
x = input()
def binarysearch(arr,l,r,x):
    mid = int((l + r)/2)
    if arr[mid] == x:
        return mid
    elif l >= r:
        return -1
    elif arr[mid] > x:
        return binarysearch(arr,l,mid-1,x)
    else :
        return binarysearch(arr,mid+1,r,x)
print(binarysearch(arr,0,len(arr)-1,x))

回溯--圖的遍歷

輸入
6 6 //圖的大小
# # # # # #
# * * # * #
# # * * * #
# * * # * #
# # # * * #
# # # # # #
3 3 // 起始點(diǎn)

C

#include<stdio.h>
int visited[1000][1000];
char graph[1000][1000];
int detect(int x,int y){
    if(visited[x][y]!= 1 && graph[x][y] != '#')    return 1;
    else return 0;
}
void huisu(int x,int y){
    visited[x][y] = 1;
    printf("%d %d\n",x,y);
    int arr[4][2] = {{x+1,y},{x-1,y},{x,y+1},{x,y-1}};
    for(int i = 0;i < 4;i++){
        if(detect(arr[i][0],arr[i][1])){
            huisu(arr[i][0],arr[i][1]);//l挪省X移摺!
        }
    }
}
int main(){
    int m,n;
    scanf("%d %d",&m,&n);
    for(int i = 0;i < m;i++){
        for(int j = 0;j<n;j++){
            scanf(" %c",&graph[i][j]);
        }
    }
    int st_x,st_y;
    scanf("%d %d",&st_x,&st_y);
    huisu(st_x,st_y);
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int visited[1000][1000];
char graph[1000][1000];
int detect(int x,int y){
    if(visited[x][y]!= 1 && graph[x][y] != '#')    return 1;
    else return 0;
}
void huisu(int x,int y){
    visited[x][y] = 1;
    cout<<x<<" "<<y<<endl;
    int arr[4][2] = {{x+1,y},{x-1,y},{x,y+1},{x,y-1}};//=ぁW蛞洹!
    for(int i = 0;i < 4;i++){
        if(detect(arr[i][0],arr[i][1])){
            huisu(arr[i][0],arr[i][1]);
        }
    }
}
int main(){
    int m,n;
    cin>>m>>n;
    for(int i = 0;i < m;i++){
        for(int j = 0;j<n;j++){
            cin>>graph[i][j];
        }
    }
    int st_x,st_y;
    cin>>st_x>>st_y;
    huisu(st_x,st_y);
}

Java

import java.util.Scanner;
public class graphgothr{
    public static int visited[][] = new int[1000][1000];
    public static char graph[][] = new char[1000][1000];
    public static boolean detect(int x,int y){
        if(visited[x][y]!= 1 && graph[x][y] != '#')    return true;
        else return false;
    }
    
    public static void huisu(int x, int y) {
        visited[x][y] = 1;
        System.out.println(x + " " + y);
        int arr[][] = { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } };//;贰H忧丁限府!
        for (int i = 0; i < 4; i++) {
            if (detect(arr[i][0], arr[i][1])) {
                huisu(arr[i][0], arr[i][1]);
            }
        }
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                graph[i][j] = sc.next().toCharArray()[0];
            }
        }
        int st_x = sc.nextInt();
        int st_y = sc.nextInt();
        huisu(st_x, st_y);
    }
}

Python

m,n = [ int(i) for i in input().split()]
graph = [ [j for j in input().split()] for i in range(m)]
st_x ,st_y =  [ int(i) for i in input().split()]
visited = [[0 for j in range(n)]for i in range(m)]
def detect(x,y):
    global visited,graph,m,n
    if visited[x][y] != 1 and graph[x][y] != '#':
        return True
    else:
        return False
def huisu(x,y):
    global visited,graph,m,n
    visited[x][y] = 1
    print(x,y)
    diec = [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]#6岵!胁勺!
    for i in range(4):
        if detect(diec[i][0],diec[i][1]):
            huisu(diec[i][0],diec[i][1])
huisu(st_x,st_y)

通用額外問(wèn)題

  1. 掌握控制流世澜。
  2. 掌握全局變量的使用。
  3. 掌握遞歸署穗。
  4. 了解程序中標(biāo)注了A攘选!案疲!的地方的使用技巧封恰。
  5. 了解如何用遞歸樹(shù)和主定理判斷程序復(fù)雜度。
  6. 了解分治和回溯褐啡。

語(yǔ)言特性問(wèn)題

C

  1. 了解math.h里的內(nèi)容诺舔。
  2. 掌握浮點(diǎn)數(shù)的使用及浮點(diǎn)數(shù)運(yùn)算時(shí)會(huì)出現(xiàn)的問(wèn)題(提示:2進(jìn)制表示方法)。
  3. 理解scanf(" %c",&graph[i][j])這段" %c"的處理方式。
  4. 了解C語(yǔ)言的字符處理低飒。
  5. 掌握C語(yǔ)言利用函數(shù)返回值作為判斷條件的使用许昨。

C++

  1. 了解cmath里的內(nèi)容。
  2. 掌握浮點(diǎn)數(shù)的使用及浮點(diǎn)數(shù)運(yùn)算時(shí)會(huì)出現(xiàn)的問(wèn)題(提示:2進(jìn)制表示方法)褥赊。
  3. 了解C++的字符處理糕档。
  4. 了解C++的泛型。
  5. 掌握C++里的Boolean變量拌喉。

Java

  1. 了解java.Math的內(nèi)容速那。
  2. 掌握浮點(diǎn)數(shù)的使用及浮點(diǎn)數(shù)運(yùn)算時(shí)會(huì)出現(xiàn)的問(wèn)題(提示:2進(jìn)制表示方法)。
  3. 理解"graph[i][j] = sc.next().toCharArray()[0]"這段的處理方式司光。
  4. 掌握java里的靜態(tài)方法琅坡。
  5. 了解java里的String對(duì)應(yīng)的方法。
  6. 掌握java里的Boolean變量残家。

Python

  1. 掌握python的浮點(diǎn)數(shù)處理榆俺。
  2. 掌握其中的列表推導(dǎo)式。
  3. 掌握python的global關(guān)鍵字坞淮。
  4. 掌握python的True茴晋、False表示。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末回窘,一起剝皮案震驚了整個(gè)濱河市诺擅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌啡直,老刑警劉巖烁涌,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異酒觅,居然都是意外死亡撮执,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)舷丹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)抒钱,“玉大人,你說(shuō)我怎么就攤上這事颜凯∧北遥” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵症概,是天一觀的道長(zhǎng)蕾额。 經(jīng)常有香客問(wèn)我,道長(zhǎng)彼城,這世上最難降的妖魔是什么诅蝶? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任逼友,我火速辦了婚禮,結(jié)果婚禮上秤涩,老公的妹妹穿的比我還像新娘帜乞。我一直安慰自己,他們只是感情好筐眷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布黎烈。 她就那樣靜靜地躺著,像睡著了一般匀谣。 火紅的嫁衣襯著肌膚如雪照棋。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,443評(píng)論 1 302
  • 那天武翎,我揣著相機(jī)與錄音烈炭,去河邊找鬼。 笑死宝恶,一個(gè)胖子當(dāng)著我的面吹牛符隙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播垫毙,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼霹疫,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了综芥?” 一聲冷哼從身側(cè)響起丽蝎,我...
    開(kāi)封第一講書(shū)人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎膀藐,沒(méi)想到半個(gè)月后屠阻,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡额各,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年国觉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片臊泰。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蛉加,死狀恐怖蚜枢,靈堂內(nèi)的尸體忽然破棺而出缸逃,到底是詐尸還是另有隱情,我是刑警寧澤厂抽,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布需频,位于F島的核電站,受9級(jí)特大地震影響筷凤,放射性物質(zhì)發(fā)生泄漏昭殉。R本人自食惡果不足惜苞七,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挪丢。 院中可真熱鬧蹂风,春花似錦、人聲如沸乾蓬。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)任内。三九已至撵渡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間死嗦,已是汗流浹背趋距。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留越除,地道東北人节腐。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像摘盆,于是被迫代替她去往敵國(guó)和親铜跑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容