英文:
Distribute a user input among the predefined numbers
问题
我正在尝试在Java中构建一个简单的输入/输出程序。该程序应该从用户那里获取输入,然后将其分配给预定义的数字。例如,假设用户输入452,预定义的值是300、200、100、50。程序应该以字符串格式输出如下: "1x300,0x200,1x100,1x50,余数为2。" 但是,我无法弄清楚代码。
英文:
I am trying to build a simple input/output program in java. This program should take an input from a user and then distribute it among the predefined numbers. For example, let's say a user enters 452, and the predefined values are 300, 200, 100, 50. The program should give the output in a string format as follows: "1x300, 0x200, 1x100, 1x50, with a remainder of 2." However, I can't figure out the code.
答案1
得分: 2
这个解决方案应该是有效的:
int[] values = {300, 200, 100, 50};
int[] multipliers = new int[values.length];
int input = 452;
for(int i = 0; i < values.length; ++i)
{
if(input < values[i])
{
continue;
}
else
{
multipliers[i] = input / values[i];
input = input % values[i];
// 因为隐式转换,它会自动变成整数
}
}
for(int i = 0; i < multipliers.length; ++i)
{
System.out.println(multipliers[i]+"x"+values[i]);
}
System.out.println("剩余为:"+input);
# 解释:
假设值是按降序输入的,我们遍历 `values` 数组,将每个乘数的元素设置为 `values[i]` 可以整除输入的次数。然后我们使用取模(`%`)运算符将输入设置为 `values[i]` 的余数。
最后,所有的乘数将被正确设置,输入将等于余数。
<details>
<summary>英文:</summary>
This solution should work:
int[] values = {300, 200, 100, 50};
int[] multipliers = new int[values.length];
int input = 452;
for(int i = 0; i < values.length; ++i)
{
if(input < values[i])
{
continue;
}
else
{
multipliers[i] = input / values[i];
input = input % values[i];
// It will automatically be an integer because of implict conversion
}
}
for(int i = 0; i < multipliers.length; ++i)
{
System.out.println(multipliers[i]+"x"+values[i]);
}
System.out.println("Remainder is "+input);
# Explanation:
Assuming the values are inputted in descending order, we loop through `values` and set each element of multiplier equal to the number of times `values[i]` can divide the input. Then we set input to the remainder (using the modulo `%` operator) from `values[i]`.
At the end, all the multipliers will be set correctly, and input will be equal to the remainder
</details>
# 答案2
**得分**: 1
```java
public static void main(String[] args){
//应按降序排列
//键表示预定义值
//值表示预定义值的计数
Map<Integer, Integer> predefinedValues = new LinkedHashMap<>();
predefinedValues.put(300, 0);
predefinedValues.put(200, 0);
predefinedValues.put(100, 0);
predefinedValues.put(50, 0);
//获取最小的预定义值
int lowestPredef = 50;
//获取输入
int input = 655;
//在输入大于最小值的情况下进行迭代
while(input > lowestPredef){
//迭代所有预定义值
for(Map.Entry<Integer, Integer> entry : predefinedValues.entrySet()){
int key = entry.getKey();
int value = entry.getValue();
//找到并在第一个低于输入的值处停止
if(input > key){
//从输入中减去该值
input -= key;
//该值的计数加1
predefinedValues.put(key, value + 1);
//跳出循环
break;
}
}
}
//使用结果构建字符串
StringBuilder result = new StringBuilder("");
for(Map.Entry<Integer, Integer> entry : predefinedValues.entrySet()){
result.append(entry.getValue())
.append("x")
.append(entry.getKey())
.append(", ");
}
result.append("余数为")
.append(input);
System.out.print(result.toString());
}
英文:
Normally, you should provide us the code you have tried, to get help.
So I was free to think the solution in my way.
public static void main(String[] args){
//should be in descending order
//key represents predef value
//value represents the count of predef value
Map<Integer, Integer> predefinedValues = new LinkedHashMap<>();
predefinedValues.put(300, 0);
predefinedValues.put(200, 0);
predefinedValues.put(100, 0);
predefinedValues.put(50, 0);
//get lowestPredef
int lowestPredef = 50;
//get input
int input = 655;
//iterate until input is higher than lowest
while(input > lowestPredef){
//iterate all predefined values
for(Map.Entry<Integer, Integer> entry : predefinedValues.entrySet()){
int key = entry.getKey();
int value = entry.getValue();
//find and stop at first lower value than input
if(input > key){
//substract that value from input
input -= key;
//count +1 for that value
predefinedValues.put(key, value + 1);
//break for-loop
break;
}
}
}
//build a string with result
StringBuilder result = new StringBuilder("");
for(Map.Entry<Integer, Integer> entry : predefinedValues.entrySet()){
result.append(entry.getValue())
.append("x")
.append(entry.getKey())
.append(", ");
}
result.append("with a remainder of ")
.append(input);
System.out.print(result.toString());
}
答案3
得分: 0
这段代码适用于任何数字。
public static void main(String[] args) {
int value = 452;
Map<Integer, Integer> listofmultiples = new HashMap();
listofmultiples.put(300, 0);
listofmultiples.put(200, 0);
listofmultiples.put(100, 0);
listofmultiples.put(50, 0);
int lastKey = 0;
lastKey = (int) listofmultiples.keySet().toArray()[0];
int i = listofmultiples.size();
int valueforkey = 0;
while (value > lastKey) {
int key = (int) listofmultiples.keySet().toArray()[i - 1];
if (value > key) {
value %= key;
valueforkey += 1;
listofmultiples.put(key, valueforkey);
}
else{
i-=1;
valueforkey=0;
}
}
System.out.println(listofmultiples.toString());
System.out.println(value);
}
英文:
This code works for every number.
public static void main(String[] args) {
int value = 452;
Map<Integer, Integer> listofmultiples = new HashMap();
listofmultiples.put(300, 0);
listofmultiples.put(200, 0);
listofmultiples.put(100, 0);
listofmultiples.put(50, 0);
int lastKey = 0;
lastKey = (int) listofmultiples.keySet().toArray()[0];
int i = listofmultiples.size();
int valueforkey = 0;
while (value > lastKey) {
int key = (int) listofmultiples.keySet().toArray()[i - 1];
if (value > key) {
value %= key;
valueforkey += 1;
listofmultiples.put(key, valueforkey);
}
else{
i-=1;
valueforkey=0;
}
}
System.out.println(listofmultiples.toString());
System.out.println(value);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论