The use of C language while loop

The general form whlie cycle
while (expression) statement
whenever the loop expression is true (i.e., a given condition is satisfied) block is executed statements.
Application 2:
Here Insert Picture Description
the other side do not want to talk to you, and you throw a bunch of number ...... and you have to find the "250" number on the tall moving from a string of numbers.
Input format: input given not know how many integer absolute value is not more than 1000 in a row, which guarantee the existence of at least one "250."
Output formats: on a single line output the first occurrence of "250" is the other side threw the first few numbers (count starts at 1). Title ensure that the output of the digital integer within the range.
This question is crucial is that the number of input numbers we do not know, so then we can use a while loop to solve.

#include <stdio.h>
int main ()
{
int x,k = 1;
//只要输入的形式符合,就循环下去
while(scanf("%d",&x) == 1){
if(x == 250){
break;
}
//k计数
k++;
}
printf("%d\n",k);
return 0;
}

Application of Two **: ** unknown number of cycles
to program, and a calculation sequence portion - 1/4 + 1/7 - 1/10 + ... until the absolute value is not greater than the last one given accuracy eps. Input format:
input line in a given positive real number eps.
Output format:
output section and the value S in accordance with the "sum = S" format in a row, accurate to six decimal places. Title ensure that the calculation result does not exceed the double precision.

#include<stdio.h>
int main()
{
double eps,sum = 0;
scanf("%lf",&eps);
int i = 1;
while(1){
//减去第偶数个数
if(i % 2 == 0){
sum = sum - 1.0 / (3.0 * i - 2);
}else{//加上第奇数个数
sum = sum + 1.0 / (3.0 * i - 2);
}
//当达到题上要求条件时,用break跳出循环
if(1.0 / (3.0 * i - 2) <= eps){
break;
}
i++;
}
printf("%lf",sum);
return 0;

Application three:
given four kinds of fruit, namely apples (apple), pear (pear), orange (orange), grapes (grape), corresponding to the unit price of 3.00 yuan / kg, 2.50 yuan / kg, 4.10 yuan / kg, 10.20 yuan / kg.
The following menu is first displayed on the screen:
[1] Apple
[2] PEAR
[. 3] Orange
[4] Grape
[0] Exit
user can enter the number corresponding to 1 to 4 monovalent query fruit. When more than five times the number of continuous queries, the program should automatically exit query; less than five times the user input 0 to exit; enter another number, displayed price is zero.
Input format:
input user ID number given continuously inputted in a row.
Output Format:
First, display the menu on the screen. Then each input corresponding to a user, in a row in the format "price = price" output of query results, where the price of two decimal places. When a user queries over five consecutive times, or take the initiative to enter 0, the program ends.

#include<stdio.h>
int main()
{
 printf("[1] apple\n");
 printf("[2] pear\n");
 printf("[3] orange\n");
 printf("[4] grape\n");
 printf("[0] exit\n");
 int a, t = 0;
 //t记录输入次数,大于五次即结束循环
 while(t < 5){
  t = t + 1;
  scanf("%d",&a);
  
  switch(a){
   case 1:
    printf("price = 3.00\n");
    break;
   case 2:
    printf("price = 2.50\n");
    break;
   case 3:
    printf("price = 4.10\n");
    break;
   case 4:
    printf("price = 10.20\n");
    break;
   case 0:
    //输入0,让t为任意一个大于等于5的数字,即可让循环结束
    t = 5;
    break;
   default:
    printf("price = 0.00\n");  
  }
 }
}

Welcome to leave comments Oh!

Released four original articles · won praise 6 · views 80

Guess you like

Origin blog.csdn.net/qq_46127363/article/details/105070450
Recommended