4.1 ????????p???? (while??) - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

4.1 ????????p???? (while??)

Description:

Title: chap3 Author: yangqf Last modified by: zjh Created Date: 3/4/1998 2:16:10 PM Document presentation format: Other titles – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 38
Provided by: yang142
Category:
Tags: chap3

less

Transcript and Presenter's Notes

Title: 4.1 ????????p???? (while??)


1
? ? ? ????
  • 4.1 ????????p???? (while??)
  • 4.2 ????????? (do-while??)
  • 4.3 ???? (break ? continue ??)
  • 4.4 ?1!2!...100! (????)
  • 4.5 ????????

2
????
  • ?????? ????????? ???????
  • ?????,?????????????
  • ????while ?do-while?????????????
  • while ?do-while????????
  • ????break??????????
  • ?????????

3
4.1 ????????p????
  • ?????????p????,???????????????104?
  • 4.1.1 ????
  • 4.1.2 while??
  • 4.1.3 ?????????,?????

4
4.1.1 ????-?p????
pi 3.141613
  • include ltmath.hgt
  • void main( )
  • int fm, fz
  • double item, pi
  • fz1fm 1 item 1.0 pi 0
  • while(fabs (item) gt 0.0001)
  • item fz 1.0 /fm
  • pi pi item
  • fz -fz
  • fm fm 2
  • pi pi 4
  • printf ( pi lf\n, pi)

item0.0 ?
fabs(item) lt 0.0001
5
4.1.2 while ??
????
  • while (??)
  • ?????

????
???
6
while ????
  • while ???for??
  • ???????????
  • ?for?????while??
  • for(???1 ???2 ???3)
  • ?????

???1 while (???2) for?????? ???3
7
while ? for ???
  • for (i 1 i lt 10 i)
  • sum sum i

i 1 ??????? while (i lt 10) ????
sum sum i i
???????
???
8
4.1.3 ??????????????
  • ?4-2 ????????????,??????
  • ??
  • ????
  • ??????
  • ??????????,??????????
  • ?????????????????????,???????????????

9
  • include ltstdio.hgt
  • void main( )
  • int num
  • double grade, total
  • num 0 total 0
  • printf(Enter grades \n")
  • scanf("lf", grade) / ???1??/while
    (mark gt 0) / ????,???? /
  • total total grade
  • num
  • scanf (lf, grade)
  • if(num ! 0)
  • printf(Grade average is .2f\n",
    total/num)
  • else
  • printf("Grade average is 0\n")

Enter grades -1 67 88 73 54 82 Grade average
mark is 0
Enter grades 67 88 73 54 82 -1 Grade average is
72.80
10
4.2 ?????????
  • ?????????,????????
  • 4.2.1 ????
  • 4.2.2 do - while??
  • 4.2.3 ???????

11
4.2.1 ????-?????????
Enter a number 12534 It contains 5 digits.
  • void main( )
  • int count, number
  • count 0
  • printf(Enter a number ")
  • scanf ("d", number)
  • if (number lt 0) number -number
  • do
  • number number / 10
  • count
  • while (number ! 0)
  • printf("It contains d digits.\n", count)

Enter a number -99 It contains 2 digits.
Enter a number 0 It contains 1 digits.
while (number ! 0) number number /
10 count
12
4.2.2 do - while ??
  • do
  • ?????
  • while (???)

???
?????
?
???
?
???
do-while??????
13
while ? do-while ???
  • while ??????,???????
  • do-while ????????,???????????????????

14
4.2.3 ???????
  • if(??????)
  • ??for??
  • else / ?????? /
  • if (????????????)
  • ??while??
  • else / ????????????? /
  • ??do-while??

15
4.3 ????
  • ???????m,?????????
  • 4.3.1 ????
  • 4.3.2 break?? ?continue??

16
4.3.1 ????-????
  • ????1?m,?????????
  • ? i ?? 2, m-1
  • ??m???????????????,????i,mi ???0,?m???
  • ??????i,?mi?0,?m??????
  • m 2 3 4 5
    (m-1)
  • ???? 0 0
  • ??? !0 !0
  • m?????? m/2 ????
  • i ?? 2, m-1 ? 2, m/2 ? 2,

for(i 2 i lt m/2 i) if(m i 0)
break if(i gt m/2) printf("yes\n") else
printf("no\n)
17
?4-4???-????
Enter a number 9 No
  • void main( )
  • int i, m
  • printf(Enter a number ")
  • scanf ("d", m)
  • for (i 2 i lt m/2 i)
  • if (m i 0) break
  • if (i gt m/2 )
  • printf("d is a prime number! \n", m)
  • else
  • printf("No!\n")

Enter a number 11 11 is a prime number!
????? ????????
  • for (i 2 i lt m/2 i)
  • if (m i 0) printf("No!\n")
  • else printf("d is a prime number! \n", m)

18
break ??
for (i 2 i lt m/2 i) if (m
i 0) break if (i gt m/2 ) printf("Yes")
else printf("No!\n")
  • while(exp)
  • ??1
  • if (expb) break
  • ??2
  • ?????????
  • ??????
  • ??????

19
continue ??
  • while(exp)
  • ??1
  • if (expb) continue
  • ??2

??continue?????,???????
20
break ? continue
abc? efgh ? 123 ?
  • include "stdio.h"
  • void main( )
  • char c
  • int i
  • for (i 0 i lt 10 i)
  • c getchar()
  • if (c '\n') break
  • putchar(c)

abc
continue
abcefgh1
21
4.4 ?1! 2! . 100!
  • for (i 1 i lt 100 i)
  • item i !
  • sum sum item
  • 4.4.1 ????
  • ???? fact(i) ?? i ???
  • 4.4.2 ????
  • ????? i ???

22
4.4.1 ???? ?1! 2! . 100!
  • include ltstdio.hgt
  • double fact (int n)
  • void main( )
  • int i
  • double sum
  • sum 0
  • for(i 1 i lt 100 i )
  • sum sum fact (i)
  • printf("1! 2! 3! 100! e\n",
    sum)
  • double fact (int n)
  • int i
  • double result 1
  • for (i 1 i lt n i)
  • result result i
  • return result

23
4.4.2 ????
  • for (i 1 i lt 100 i)
  • item i !
  • sum sum item

for(i 1 i lt 100 i) item 1
for (j 1 j lt i j) item item j
sum sum item
24
?4-6 ???
  • include ltstdio.hgt
  • void main( )
  • int i, j
  • double item, sum / item
    ???? /
  • sum 0
  • for(i 1 i lt 100 i)
  • item 1 /
    ???????1?? /
  • for (j 1 j lt i j) / ??????
    item i! /
  • item item j
  • sum sum item
  • printf("1! 2! 3! 100! e\n", sum)

25
????????
?1! 2! . 100!
  • for(i 1 i lt 100 i)
  • item 1
  • for (j 1 j lt i j)
  • item item j
  • sum sum item

item 1 for(i 1 i lt 100 i) for (j
1 j lt i j) item item j
sum sum item
?1! 1!2! 1!2!100!
26
???????????
  • for(i 1 i lt 100 i)
  • item 1
  • for (j 1 j lt i j)
  • item item j
  • sum sum item
  • ?????? i ????
  • ?????? j ??????
  • ???????????
  • ??? i ? j

27
for (i 1 i lt 100 i) for (j 1 j lt
i j) printf ("d d\n", i, j )
28
4.5 ??????
  • ?????????
  • ?????????????? ???
  • ??????????????? ????
  • ?????????
  • for while do-while
  • ?????????(????)
  • ????????,??for
  • ??????????,??while?do-while

29
?4-7 ?????????,????(for)
Enter n 5 Enter 5 maks67 88 73 54 82 Max 88
  • include ltstdio.hgt
  • void main( )
  • int i, mark, max, n
  • printf("Enter n ") scanf ("d", n)
  • printf("Enter d marks ", n)
  • scanf ("d", mark) / ???????
    /
  • max mark / ??????????? /
  • for (i 1 i lt n i )
  • scanf ("d", mark)
  • if (max lt mark)
  • max mark
  • printf("Max d\n", max)

max
mark
Enter n 0
30
?4-7 ?????????,????(while)
  • include ltstdio.hgt
  • void main( )
  • int mark, max
  • printf(Enter marks")
  • scanf ("d", mark) / ??????? /
  • max mark / ?????????? /
  • while (mark gt 0)
  • if(max lt mark)
  • max mark
  • scanf ("d", mark )
  • printf("Max d\n", max)

Enter marks67 88 73 54 82 -1 Max 88
Enter marks-1
31
?4-7 ?????????,????(do-while)
  • include ltstdio.hgt
  • void main( )
  • int mark, max
  • max -1 / ?max?????? /
  • printf(Enter marks ")
  • do
  • scanf ("d", mark )
  • if (max lt mark)
  • max mark
  • while(mark gt 0)
  • printf("Max d\n", max)

Enter marks 67 88 73 54 82 -1 Max 88
Enter marks -1
32
?4-8 ??????????
  • ??????????(?????)

12345 5 4 3 2 1 12345 10
5 12345 / 10 1234 1234 10 4
1234 / 10 123 123 10 3 123 /
10 12 12 10 2 12 / 10 1
1 10 1 1 / 10 0 ??
?do-while???
scanf( d, x) while (x ! 0) digit x
10 x x/10 printf( "d ",
digit)
????? x10 xx/10 ?????? x0
33
?4-9 ?500???????,????10?
  • for (m 2 m lt 500 m)
  • if (m???) printf( "d", m)

n sqrt(m) for(i 2 i lt n i) if(m
i 0) break if(i gt n) printf("yes\n") else
printf("no\n)
for (m 2 m lt 500 m) nsqrt(m)
for(i 2 i lt n i) if(m i 0)
break if(i gt n) printf("d", m)
34
?4-9 ???
  • include ltstdio.hgt
  • include ltmath.hgt
  • void main( )
  • int count, i, m, n
  • count 0
  • for (m 2 m lt 500 m)
  • for (i 2 i lt sqrt(m) i)
  • if(m i 0) break
  • if(i gt n) / ??m??? /
  • printf("6d", m)
  • count
  • if (count 10 0) printf(\n)

35
?4-10 ?Fibonacci??1,1,2,3,5,8,13,
x1 x2 1 x x1 x2 x1 x2 x2 x
  • 1, 1, 2, 3, 5, 8, 13,
  • x1 x2 x
  • x1 x2 x

x1 1 x2 1 printf ("6d6d", x1, x2 )
/ ????? / for (i 1 i lt 8 i)
/ ?????8? / x x1 x2
/ ???? / printf("6d", x)
x1 x2 /
??x1?x2 / x2 x
36
?4-11??????-???
  • ????????,???????3?,?????2?,????????
  • ??45????45??,???????

for (men 0 men lt 45 men) for (women
0 women lt 45 women) for (child 0
child lt 45 child) if
((menwomenchild45) (men3women2child0.5
45)) printf("mend womend
childd\n", men, women, child)
37
?4-11 ???(2)
  • for (men 0 men lt 15 men)
  • for (women 0 women lt 22 women)
  • child 45 women men
  • if (men 3 women 2 child 0.5
    45)
  • printf("mend womend
    childd\n", men, women, child)

??????
for (men 0 men lt 45 men) for (women
0 women lt 45 women) for (child 0
child lt 45 child) if
((menwomenchild45) (men3women2child0.5
45)) printf("mend womend
childd\n", men, women, child)
Write a Comment
User Comments (0)
About PowerShow.com