【王道机试】枚举-old bill-上交大

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 来输入

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值