首页 > 其他分享 >一种典型的不知循环次数的c语言循环问题

一种典型的不知循环次数的c语言循环问题

时间:2022-12-08 22:12:08浏览次数:28  
标签:count 典型 temp max scanf 次数 循环 输入

问题如图

 

 代码如下

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include<stdio.h>
 3 int main()
 4 {
 5     puts("输入正整数,以-1为结束标志\n");
 6     int temp, count, max = 0;
 7     scanf("%d", &temp);
 8     if (temp == -1)
 9     {
10         puts("输入数据错误!!\n");
11         return 0;//这样才不会一开始输入错误还进入下面继续执行
12     }
13     else  
14     {
15 
16         while (temp != -1)//靠他和他包含的scanf来完成多次输入
17         {
18             if (temp > max)
19             {
20                 max = temp;
21                 count = 1;//这样能排除前面有两次更小的数出现,而后面还有大数
22             }
23             else if (temp == max)
24             {
25                 count++;
26             }
27             scanf("%d", &temp);
28         }
29     }
30     printf("max = %d, count = %d\n", max, count);
31 
32     return 0;
33 }

结果如下

 

标签:count,典型,temp,max,scanf,次数,循环,输入
From: https://www.cnblogs.com/yxyfj/p/16967537.html

相关文章