Old Bill(枚举)

题目描述:

    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?

两位数是啥?  1个Turkeys的单价是啥?   (turkey.火鸡 土耳其
    We want to write a program that solves a general version of the above problem.

写个程序 解决总的版本 of上述问题


    N turkeys $_XYZ_
    The total number of turkeys, N, is between 1 and 99, including both.

N个火鸡  n:1~99

The total price originally consisted of five digits, but we can see only the three digits in the middle.

中间三个数字可见,一共5位数

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.

我们假设第一个数字非0,5位数是整数(非double!!!),火鸡单价一致
    Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price.

给你NXYZ,猜

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.

样例输入:

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

样例输出:

3 2 511
9 5 18475
0

来源:

2007年上海交通大学计算机研究生机试真题

 

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

bool isInt(double x) {
	int a = int(x);
	double res = x - a;
	if (res <= 0.000001) {
		return true;
	}
	return false;
}


int main() {


	int n;
	int x, y , z;
	while (cin >> n >> x >> y >> z) {

		int maxPrice = 0;
		double price, total;
		int firstDigit, lastDigit;

		for (int i = 1; i <= 9; i++) {
			for (int j = 0; j <= 9; j++) {
				total = i * 10000 + x * 1000 + y * 100 + z * 10 + j;
				price = total / n;
				if (isInt(price) && price > maxPrice) {
					maxPrice = price;
					firstDigit = i;
					lastDigit = j;
				}
			}
		}
		if (maxPrice > 0) {
			cout << firstDigit <<"  " << lastDigit << "  " << maxPrice << endl;

		} else {
			cout << 0 << endl;
		}
	}

	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的枚举类型是一种特殊的类,它可以用于定义一组常量。枚举类型在Java中是静态的,即在编译时就已经确定了枚举值。因此,如果需要动态添加、删除枚举值,就需要使用动态枚举。 Java中实现动态枚举的方式有多种,其中比较常见的方式是使用Java反射机制。通过反射机制,可以在运行时动态地创建枚举值,并将其添加到枚举类型中。 以下是一个使用反射机制实现动态枚举的示例代码: ```java import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class DynamicEnumDemo { public enum Fruit { APPLE, BANANA, ORANGE } public static void main(String[] args) throws Exception { List<String> newValues = new ArrayList<>(); newValues.add("PEAR"); newValues.add("PEACH"); Class<?> enumClass = Fruit.class; Field valuesField = enumClass.getDeclaredField("$VALUES"); valuesField.setAccessible(true); Object[] oldValues = (Object[]) valuesField.get(null); Object[] newValuesArray = new Object[oldValues.length + newValues.size()]; System.arraycopy(oldValues, 0, newValuesArray, 0, oldValues.length); for (int i = 0; i < newValues.size(); i++) { String name = newValues.get(i); Object[] enumConstants = {name, oldValues.length + i}; newValuesArray[oldValues.length + i] = enumClass.getDeclaredConstructor(String.class, int.class) .newInstance(enumConstants); } valuesField.set(null, newValuesArray); for (Fruit fruit : Fruit.values()) { System.out.println(fruit); } } } ``` 在以上示例代码中,我们使用了Java反射机制,动态地向枚举类型Fruit中添加了两个新的枚举值PEAR和PEACH,并输出了所有的枚举值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值