博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Old Bill(上海交通大学复试上机题)以及此题解决时,学到了一些知识。
阅读量:3938 次
发布时间:2019-05-23

本文共 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;}

学到的知识总结:

  1. c++ 的cin 直接从命令行去读取数据,不会去在乎换行符和空格的,不需要去进行额外的处理。使用cin 或者 scanf()来处理输出

    scanf("%d%d%d%d", &n, &x, &y, &z);

  2. 如何跳出嵌套循环,看到博客使用 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/

你可能感兴趣的文章
Linux下简单Makefile文件的编写
查看>>
linux下配置JDK JAVA环境
查看>>
解决Ubuntu 14.04 grub选择启动项10秒等待时间
查看>>
Python函数操作集锦之字符串测试、判断函数
查看>>
Python字符串操作集锦之字符串映射表
查看>>
Python字符串操作集锦之字符串编码解码函数
查看>>
Python字符串类型转换函数
查看>>
Python有用的命令
查看>>
Python条件语句
查看>>
Python eval()函数
查看>>
Linux rz和sz命令详解
查看>>
Python 集合set
查看>>
Python时间模块之Time模块解析
查看>>
Python 文件操作
查看>>
Python 实现队列
查看>>
Python 实现栈
查看>>
Python 冒泡排序
查看>>
MySQL数据文件及常用工具
查看>>
Linux下解决命令未找到的问题
查看>>
Linux下MySQL的安装
查看>>