本文共 2305 字,大约阅读时间需要 7 分钟。
题目描述
Among grandfather’s papers a bill was found. 72 turkeys $679 The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey? We want to write a program that solves a general version of the above problem. N turkeys $XYZ The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price. Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.输入描述:
The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $XYZ.输出描述:
For each case, output the two faded digits and the maximum price per turkey for the turkeys.#include#include using namespace std;int main(){ int n,x,y,z,a,b; bool flag=false; while(cin>>n>>x>>y>>z){ for(int a=9;a>0;a--){// 循环的结束条件一定要多次确认 for(int b=9;b>=0;b--){ if((a*10000+x*1000+y*100+z*10+b)% n==0){ flag=true; printf("%d %d %d \n",a,b,(a*10000+x*1000+y*100+z*10+b)/n); break; // 找到了最佳答案之后,如何退出多重循环; } } if(flag) break; } if(!flag){ printf("%d\n",0); } } return 0;}
c++ 的cin 直接从命令行去读取数据,不会去在乎换行符和空格的,不需要去进行额外的处理。使用cin 或者 scanf()来处理输出
scanf("%d%d%d%d", &n, &x, &y, &z);
如何跳出嵌套循环,看到博客使用 break + label 的方式:
// 例如这样,但是我不建议这种使用方式,降低代码的可理解性;outer: for(int i=0; i<3; ++i) { for(int j=0; j<3; ++j) { cout< <
附上break 和contine 的区别:
break:跳出该循环体,这个之后的循环遍历都不做了 contine:跳出此次循环,直接到下一个i循环。转载地址:http://xjywi.baihongyu.com/