C语言-循环结构程序设计.pptVIP

  • 2
  • 0
  • 约1.07万字
  • 约 52页
  • 2019-11-06 发布于广东
  • 举报
程序 百钱买百鸡问题。 main ( ) { int x , y , z ; for ( x = 1 ; x 20 ; x ++ ) for ( y = 1 ; y 33 ; y ++ ) { z = 100 – x – y ; /*计算小鸡个数*/ if ( z ! = 0 5 * x + 3 * y + z / 3.0 = = 100 )  break ; } end : printf ( 公鸡=%d, 母鸡=%d, 小鸡=%d \n , x , y , z ); } 5.7 break语句与continue 语句 5.7.2 continue语句 1. continue的语句格式: continue ; 2.功能:结束本轮循环,即跳过循环体中continue下面的语句,执行下一次循环。 continue语句只适用于循环语句中。 注意:continue与break在功能上有很大的区别。 程序 把1~100之间的能被7整除的数输出。 main ( ) { int n; for ( n = 1 ; n = 100 ; n ++ ) { if ( n % 7 != 0 ) continue ; printf ( “ %d ” , n ) ; } } 程序 把1~100之间的能被7整除的数输出。 main ( ) { int n; for ( n = 1 ; n = 100 ; n ++ ) { if ( n % 7 != 0 ) break ; printf ( “ %d ” , n ) ; } } main( ) { int i; for ( i = 1 ; i 6 ; i ++ ) { if ( i % 2 ) printf ( “ 1 ” ) ; else continue ; printf ( “ 2 ” ) ; } } 输出结果:121212 main( ) { int i; for ( i = 1 ; i 6 ; i ++ ) { if ( i % 2 ) { printf ( “ 1 ” ) ; continue ; } else printf ( “ 2 ” ) ; } } 输出结果:12121 main( ) { int i; for ( i = 1 ; i 6 ; i ++ ) { if ( i % 2 ) { printf ( “ 1 ” ) ; continue ; printf ( “ 2 ” ) ; } } } 输出结果:111 5.8 程序举例 1.最大最小问题 2.分段(多条件)问题 3.函数公式套用 4.求素数问题 5.求累加或累乘问题 6.译码问题 7.最大公倍数和最小公约数问题 程序 .读入三个数,找出并打印其中的最大数 程序如下: main( ) { int a, b , c, max; scanf(“%d,%d,%d”,a,b,c); if ((ab)(ac)) max=a; else if ((ba)(bc)) max=b; else if ((ca)(cb)) max=c; printf(“max=%d\n”,max); } 请问:有什么问题吗? 假若:a=b 但 ca 呢? 例如:a=5 b=5 c=4 ab ac Y Y max=a max=c N bc max=b N Y N max=c main( ) { int a, b , c, max; scanf(“%d,%d,%d”,a,b,c); if (ab) { if (ac) max=a;

文档评论(0)

1亿VIP精品文档

相关文档