首页 > 试题广场 >

Old Bill

[编程题]Old Bill
  • 热度指数:28346 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    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.
示例1

输入

72
6 7 9
5
2 3 7
78
0 0 5

输出

3 2 511
9 5 18475
0
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{   int N,X,Y,Z,sum;
    while(scanf("%d",&N)!=EOF){
            cin>>X>>Y>>Z;
            int flag=0;
            for(int i=9;i>=1;i--){
                for(int j=9;j>=0;j--){
                        sum=10000*i+1000*X+100*Y+10*Z+j;
                        if(sum%N==0){
                            cout<<i<<" "<<j<<" "<<sum/N<<endl;
                            flag=1;
                            break;
                        }
                }
                if(flag==1)
                    break;
            }
            if(flag==0)
                cout<<0;

    }

}

发表于 2020-07-07 20:58:46 回复(0)
/*
Old Bill
*/

#include <stdio.h>

int main() {
    int n,x,y,z;
    while(scanf("%d %d %d %d", &n, &x, &y, &z) != EOF) {
        int lnum,rnum,price,money,i=0;
        int isOutPut = 0;
        for(lnum = 9; lnum > 0; lnum--) {
            for(rnum = 9; rnum >=0; rnum--) {
                money = lnum*10000 + x*1000+ y*100 + z*10 + rnum;
                if(money % n == 0 ) {
                    price = money/n ;
                    i++ ;
                    if (i == 1) {
                        printf("%d %d %d\n",lnum,rnum,price);
                        isOutPut = 1;
                        break;
                    }
                }
            }
            if(isOutPut == 1)
                break;
        }
        if(i == 0) {
            printf("0\n");
        }
    }
}
发表于 2020-04-10 19:05:46 回复(0)
#include <stdio.h>
 
int main()
{
    int n, x, y, z, i, j, price;
 
    scanf("%d%d%d%d", &n, &x, &y, &z);
    for (i = 9; i > 0; i--)
        for (j = 9; j >= 0; j--) {
            price = i * 10000 + x * 1000 + y * 100 + z * 10 + j;
            if (!(price % n)) {
                printf("%d %d %d\n", i, j, price / n);
                return 0;
            }
        }
    printf("0\n");
    return 0;
}

编辑于 2020-04-05 10:56:24 回复(0)
枚举大法好hhh,9*9最大搜索时间还是可以接受的
#include<iostream>
using namespace std;
int main(){
    int a[5];
    int n;
    while(cin>>n>>a[1]>>a[2]>>a[3]){
        int total=0;
        int b[20][2],count=0;
        for(int i=1;i<=9;i++){
            a[0]=i;
            total=10000*a[0]+1000*a[1]+100*a[2]+10*a[3];
            for(int j=0;j<=9;j++){
                if((total+j)%n==0){
                    b[count][0]=a[0];
                    b[count++][1]=j;
                }
            }
        }
        if(count>0){
            cout<<b[count-1][0]<<" "<<b[count-1][1]<<" ";
            total=10000*b[count-1][0]+1000*a[1]+100*a[2]+10*a[3]+b[count-1][1];
            cout<<total/n<<endl;
        }
        else cout<<0<<endl;
    }
}

发表于 2019-02-09 22:07:05 回复(0)
#因为要找到最大的火鸡单价,所以从最大的火鸡总价开始找
while True:
    try:
        numTurkey = int(input())
        midNum = int(input().replace(' ',''))
        whetherFind = False                #设置一个bool值来确认是否找到
        for i in range(9,0,-1):            #最高位不能为0
            for j in range(9,-1,-1):       
                tempTotal = i*10000+midNum*10+j
                if tempTotal % numTurkey == 0:
                    leftNum = i
                    rightNum = j
                    whetherFind = True
                    price = tempTotal//numTurkey
                    break
            if whetherFind:
                break
        if whetherFind:
            print('%d %d %d'%(leftNum,rightNum,price))
        else:                             #未找到输出为0
            print(0)
    except Exception:
        break

编辑于 2018-10-14 13:19:47 回复(1)
#include <iostream>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        int d[3];
        for(int i=0;i<3;i++){
            cin>>d[i];
        }
        int max=0;
        int m=0;
        int start=0;
        int end=0;
        for(int i=1;i<=9;i++){
            for(int j=0;j<=9;j++){
                m=i*10000+d[0]*1000+d[1]*100+d[2]*10+j;
                if(max<m && m%n==0){
                    max=m;
                    start=i;
                    end=j;
                }
            }
        }
        if(max==0){
            cout<<0<<endl;
        }else
            cout<<start<<" "<<end<<" "<<max/n<<endl;
    }
} 

发表于 2018-09-15 19:19:02 回复(0)
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;
int n;
int digits[5];
int  getNum() {     int ret = 0;     int c = 1;     for (int i = 4; i >= 0; i--) {         ret += digits[i]*c;         c *= 10;     }     return ret;
}
int main()
{              while (cin >> n) {                  cin >> digits[1] >> digits[2] >> digits[3];         int flag = true;         for (int i = 9; i > 0&&flag; i--) {             for (int j = 9; j >= 0&&flag; j--) {                 digits[0] = i;                 digits[4] = j;                 int num = getNum();                 if (num%n == 0)                 {                     cout << i << " " << j << " " << num / n<<endl;                     flag = false;                     break;                 }             }         }         if (flag) {             cout << 0 << endl;         }     }
    return 0;
}

发表于 2018-07-03 22:39:02 回复(0)
__author__ = 'Yaicky'

while True:
    try:
        n = input()
        qian, bai, shi = map(int, raw_input().strip().split())
        wan, ge, price = 0, 0, 0
        flag = True
        while flag:
            for i in range(9, 0,-1):
                num = i*10000 + qian*1000 + bai*100 + shi*10 + 9
                rest = num%n
                if rest < 10:
                    price = (num - rest)/n
                    wan = i
                    ge = 9 - rest
                    flag = False
                    break
                if i == 1:
                    flag = False

        #print n, qian,bai,shi
        if price == 0:
            print '0'
        else:
            print "%d %d %d" %(wan, ge, price)
    except:
        break

发表于 2016-06-24 16:33:08 回复(0)
tips:1.两重循环,用单价为整数判断
         2.加bool判断找得到找不到,找单价最大值那么两个数就从9开始减循环,找到就输出并返回0保证输出最大值; 找不到输出0;
         3.注意多组样例输入,用while 输入
#include<iostream>
#include<cstring>
using namespace std;
int fun(int N, int X, int Y, int  Z) {
	bool find = false;
	int sum = 0;
	for (int i = 9; i >= 1; i--) {
		for (int j = 9; j >= 0; j--) {
			sum = i * 10000 + X * 1000 + Y * 100 + Z * 10 + j;
			if (sum%N == 0) {
				find = true;
				cout << i << " " << j << " " << (sum / N) << endl;
				return 0;
			}
		}
	}
	if (!find) {
		cout << 0 << endl;
	}
	return 0;
}
int main() {
	int N, X, Y, Z;
	while (cin >> N >> X >> Y >> Z) {
		fun(N, X, Y, Z);
	}
	return 0;
}

发表于 2020-02-16 00:46:07 回复(1)
#include <stdio.h>
#include <stdlib.h>


int main(){
    int n,x,y,z;
    while((scanf("%d%d%d%d",&n,&x,&y,&z))!=EOF){
        int i=0,j=0,num=0,value = 0,ci=0,cj=0;
        num+=(1000*x+100*y+10*z);
        for(i=1;i<10;++i)
            for(j=0;j<10;++j){
                value=(num+10000*i+j);
                if(value%n==0){
                        ci=i;
                        cj=j;
                    }
                }
        if(ci==0&&cj==0)
            printf("0\n");
        else
            printf("%d %d %d\n",ci,cj,(num+10000*ci+cj)/n);
    }
    return 0;
}
/*一些人生经验,记得加\n,少用全局变量,需要直接定义值的时候直
接赋值少用+=,注意每个变量的初始化时间*/


编辑于 2017-01-13 00:42:00 回复(7)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x,y,z;
    while(cin>>n)
    {
        cin>>x>>y>>z;
        int temp = x*1000+y*100+z*10;
        int total; //记录总价格
        int MAX = -99999; //记录蕞大价格
        bool find = false; //是否有符合条件的结果
        int first,last; // 第一位 与 最后一位
        for(int i=1;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                total = temp + 10000*i + j;
               
                if(total%n==0)
                {
                    find = true;
                    if(total/n>MAX)
                    {
                        MAX = total/n;
                        first = i;
                        last = j;
                    }
                }
            }
        }
        if(!find)
        {
            cout<<0<<endl;
        }else{
            cout<<first<<" "<<last<<" "<<MAX<<endl;
        }
            
        
    }
    return 0;
}

发表于 2019-04-14 09:40:22 回复(1)
#include<stdio.h>
int main(){
 int n;
 while(scanf("%d",&n)!=EOF){
  int a,b,c,flag=0;
  scanf("%d%d%d",&a,&b,&c);
  for(int i=9;i>=1;i--)
  {
   for(int j=9;j>=0;j--)
   {
    int m=i*10000+a*1000+b*100+c*10+j;
    if(m%n==0)
    {
     printf("%d %d %d\n",i,j,m/n);
     flag=1;
     break;
    }
   }
   if(flag==1) break;
  }
  if(flag==0) printf("0\n");
 }
 return 0;
}
发表于 2018-04-08 21:40:41 回复(3)
#include <stdio.h>
int main()
{
    int n,x,y,z,a,b,p,i,j,f;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%d%d%d",&x,&y,&z);
        for(i=9;i>0;i--)
        {
            f=0;
            for(j=9;j>=0;j--)
            {
                if((i*10000+j+x*1000+y*100+z*10)%n==0)
                {
                    p=(i*10000+j+x*1000+y*100+z*10)/n;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        if(i!=0)
            printf("%d %d %d\n",i,j,p);
        else
            printf("0\n");
    }
    return 0;
}

发表于 2021-03-10 22:41:34 回复(1)
#include <stdio.h>

//using namespace std;

int main(){
	int n,x,y,z;
	while(scanf("%d\n%d %d %d",&n,&x,&y,&z)!=EOF){
		int i,j;
		int max=0,maxi,maxj;
		int price;
		for(i=1;i<10;i++){
			for(j=0;j<10;j++){
				price = i*10000+x*1000+y*100+z*10+j;
				if((price%n==0) && price>(max*n)){
					max = price/n;
					maxi = i;
					maxj = j;
				}
			}
		}
        if(max==0)
            printf("0\n");
		else
			printf("%d %d %d\n",maxi,maxj,max);
	}
	return 0;
}


发表于 2020-04-24 15:14:11 回复(0)
#include<stdio.h>
int main()
{
    int n,x,y,z,i,j;
    int wan=0,ge=0,danjia;
    scanf("%d",&n);
    scanf("%d%d%d",&x,&y,&z);
    for(i=1;i<=9;i++)//万位
        for(j=0;j<=9;j++)//个位
            if((i*10000+x*1000+y*100+z*10+j)%n==0)//能够整除
                wan=i;ge=j;danjia=(i*10000+x*1000+y*100+z*10+j)/n;//由于要最高单价,所以正常向上循环就是最大单价不需处理
    if(wan==0)//表示未能被n整除或者不是五位数
        printf("0");
    else
        printf("%d %d %d",wan,ge,danjia);
}

发表于 2020-03-23 15:46:02 回复(0)
#include <iostream>
#include <algorithm>

using namespace std;

void judge(int n,int m1,int m2,int m3){
	for(int first=9;first>0;first--){
		for(int last=9;last>=0;last--){
			int num = first*10000+m1*1000+m2*100+m3*10+last;
			if(num % n == 0){
				cout<<first<<" "<<last<<" "<<num/n<<endl;
				return;
			}
		}
	}
	cout<<0;
	return;
}

int main(){
	int n,m1,m2,m3;
	while(cin>>n>>m1>>m2>>m3){
		judge(n,m1,m2,m3);
	}
	return 0;
}

发表于 2019-12-31 20:03:44 回复(0)
#include<stdio.h>
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int x,y,z;
        int a,b;
        scanf("%d%d%d",&x,&y,&z);
        int price=0;
        int mid=x*1000+y*100+z*10;
        for(int i=1;i<10;i++){
            for(int j=0;j<10;j++){
                int num=i*10000+mid+j;
                if(num%n==0&&price<num/n){
                        price=num/n;
                        a=i;b=j;
                }
            }
        }
        if(price==0)printf("0\n");
        else printf("%d %d %d\n",a,b,price);    
    }
    return 0;
}

发表于 2018-03-18 16:06:58 回复(0)
#include<stdio.h>

int main()
{
    int N,X,Y,Z,a,b;

   
    while((scanf("%d%d%d%d",&N,&X,&Y,&Z))!=EOF){
        int max=0;
    for(int i=1;i<=9;i++)
        for(int j=0;j<=9;j++)
        {
            int price=i*10000+X*1000+Y*100+Z*10+j;
            if(price%N==0)
            {
                int h=price/N;
                if(h>max)
                {
                    max=h;
                    a=i;
                    b=j;
                }
            }
        }
    if(max!=0)
        printf("%d %d %d\n",a,b,max);
    else
        printf("%d\n",max);
}
}
编辑于 2024-01-23 20:57:13 回复(0)
暴力求解
#include<iostream>
using namespace std;

int main() {
	int N = 0; int X = 0; int Y = 0; int Z = 0; int I = 0; int J = 0; int temp = 0;
	cin >> N >> X >> Y >> Z;
	for (int i = 1; i <= 9; ++i) {
		for (int j = 0; j <= 9; ++j) {
			int TotallPrice = (i * 10000 + X * 1000 + Y * 100 + Z * 10 + j);
			if (TotallPrice % N == 0 && TotallPrice / N > temp) {
					temp = (TotallPrice / N);
					I = i;
					J = j;
			}
		}
	}
	if (temp == 0) {
		cout << 0;
	}
	else {
		cout << I << " " << J << " " << temp;
	}
	return 0;
}

编辑于 2024-01-04 19:29:13 回复(0)
#include <stdio.h>
int main() {
    int N, X, Y, Z;
    while (scanf("%d\n%d%d%d", &N, &X, &Y, &Z) != EOF) {
        int $_XYZ_ = 0;
        int wan, ge;
        int mid = X * 1000 + Y * 100 + Z * 10;
        int rewan, rege;
        for (wan = 1; wan < 10; wan++) {
            for (ge = 0; ge < 10; ge++) {
                int sum = (wan * 10000 + mid + ge);
                if (sum > $_XYZ_ && sum / N * N == sum) {
                    $_XYZ_ = sum;
                    rewan = wan;
                    rege = ge;

                }

            }
        }

        if ($_XYZ_ == 0) {
            printf("%d\n", 0);
        } else
            printf("%d %d %d\n", rewan, rege, $_XYZ_ / N);






    }







    return 0;
}

发表于 2023-02-28 19:51:31 回复(0)