OJ平台:http://t.cn/E9jqijR
题目大意:
N只火鸡的价格为$ _ XYZ _,火鸡的总数N在1~99之间。价格由五位数组成,两边的数字由于褪色而看不清,所以只能看到中间的三位数。假设第一位数字非零,每只火鸡的价格是整数,并且所有火鸡的价格相同。给定N, X, Y, Z,编写一个程序来猜测两边褪色的数字和单只火鸡的价格。如果有多个价格符合题意,那么输出最昂贵的那个。
输入描述:
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.
示例1
输入:
72
6 7 9
5
2 3 7
78
0 0 5
输出:
3 2 511
9 5 18475
0
代码如下:
#include<iostream>
using namespace std;
int main(){
int n,x,y,z,first,last;
while(cin>>n){
cin>>x>>y>>z;
int num=x*1000+y*100+z*10;
int result=0;
for(int i=1;i<=9;i++){
for(int j=0;j<=9;j++){
int count=i*10000+num+j;
if(count % n==0){
if(count>result){
first=i;
last=j;
result=count/n;
}
}
}
}
if(result==0) cout<<0<<endl;
else{
printf("%d %d %d\n",first,last,result);
}
}
return 0;
}
由于题目要求输出单价最大的值,那么可以两个数从9开始减循环,找到就输出,然后跳出循环; 找不到输出0。
跳出循环的方式可以是我下面代码中的方式,也可以是注释中的方式:用return 0;代替break;
#include<iostream>
using namespace std;
int main(){
int n,x,y,z,first,last;
while(cin>>n){
cin>>x>>y>>z;
int flag=false;
int num=x*1000+y*100+z*10;
for(int i=9;i>0;i--){
for(int j=9;j>=0;j--){
int count=i*10000+num+j;
if(count % n==0){
flag=true;
printf("%d %d %d\n",i,j,count/n);
break; //return 0;
}
}
if(flag) break; //删掉
}
if(!flag) cout<<0<<endl;
}
return 0;
}
注意:示例中有多个输入,所以要用 while 来输入