英文:
How can i print the count number in first?
问题
I have solved the problem. But the output is printing this way:
15470 15471 15472 15473 15474 15475 6
I can't figure out how to print this way:
6 15470, 15471, 15472, 15473, 15474, 15475
I tried to take another array of count size to keep the output value but didn't work.
How to print the count number first?
Here's a modified version of your code that prints the count number first and then the Dementor Numbers:
#include <stdio.h>
int main() {
int n1, n2;
scanf("%d %d", &n1, &n2);
int count = 0;
int dementorNumbers[100]; // Assuming a maximum of 100 Dementor Numbers
int index = 0;
for (int i = n1; i <= n2; i++) {
int digit = 0;
int temp = i;
while (temp != 0) {
temp = temp / 10;
digit++;
}
if (digit > 2 && digit < 6) {
int a[digit];
int num = i;
int idx = digit - 1;
while (num != 0) {
a[idx] = num % 10;
num = num / 10;
idx--;
}
int sum = 0;
for (int j = 0; j < digit; j++) {
if (j % 2 == 1) {
sum += a[j];
}
}
if (sum > 8 && (sum % 4 == 0)) {
dementorNumbers[index++] = i;
count++;
}
}
}
printf("%d ", count);
for (int i = 0; i < count; i++) {
printf("%d", dementorNumbers[i]);
if (i != count - 1) {
printf(", ");
}
}
return 0;
}
This code will first print the count of Dementor Numbers and then list them separated by commas.
英文:
Harry Potter is worried about the recent outbreak of Dementor from the Prison of Azkaban. Every Dementor is marked with a specific number. Write a Program to Assist Harry identify all of the Dementor Numbers between two ranges. Dementor numbers are those numbers which have following
CONDITIONS:
a. Number of Digits is greater than 2 and less than 6
b. Sum of the digits in EVEN Positions is greater than 8 and divided by 4
For Example: 1319 is a Dementor Number since
· Number of Digits 4 is greater than 2 and less than 6
· Sum of the digits in EVEN Positions: 3 + 9 = 12 is greater than 8 and divided by 4
Sample Input
1300 1350
Sample Output
5: 1309, 1319, 1329, 1339, 1349
Sample Input
15450 15475
Sample Output
6: 15470, 15471, 15472, 15473, 15474, 15475
#include <stdio.h>
int main()
{
int n1, n2;
scanf("%d %d", &n1, &n2);
int count = 0;
for (int i = n1; i <= n2; i++)
{
int digit = 0;
int temp = i;
while (temp != 0)
{
temp = temp / 10;
digit++;
}
if (digit > 2 && digit < 6)
{
int a[digit];
int num = i;
int index = digit - 1;
while (num != 0)
{
a[index] = num % 10;
num = num / 10;
index--;
}
int sum = 0;
for (int j = 0; j < digit; j++)
{
if (j % 2 == 1)
{
sum += a[j];
}
}
if (sum > 8 && (sum % 4 == 0))
{
for (int j = 0; j < digit; j++)
{
printf("%d", a[j]);
}
printf(" ");
count++;
}
}
}
printf("%d ", count);
return 0;
}
I have solved the problem.
But the output is printing this way:
15470 15471 15472 15473 15474 15475 6
I can't figure out how to print this way:
6 15470, 15471, 15472, 15473, 15474, 15475
I tried to take another array of count size to keep the output value but didn't work
how to print count number in first?
Note: using only if-else,loop,array
and it is not programming contest problem.
答案1
得分: 0
以下是您要求的翻译:
"执行算法的方式决定了打印的顺序。这是有原因的。你事先不知道在范围内会找到多少个整数,所以在获取所有数字之前无法打印它。
为了应对这个问题,你需要存储(而不是立即打印)这些数字(例如,可以使用数组,像下面建议的list
一样),而它们都在计算中,直到结束循环,然后你可以打印count
,并创建第二个循环来遍历数组list
,然后打印所有数字。
打印机有一个臭名昭著的习惯,即从上到下,每行从左到右打印,因此打印机决定了你必须使用的打印数字的顺序。如果你首先打印数字列表(在循环中),你将在末尾打印计数(打印头所在的位置)。
对于没有显示完整代码的问题,我要为此道歉,但看起来这似乎是一个学术作业示例,所以你需要自己解决,但基本上的想法是要有一个额外的变量:
int list[A_BIG_NUMBER_HERE];
用于存储单个列表元素。A_BIG_NUMBER_HERE
常量应足够大,以允许你管理大量数字的大列表(有可能使列表维度可变,这样你就不会受到数组大小的限制,但这超出了题目范围,所以我不会在这里使用这种方法)。尽管如此,如果你想要谨慎一些,因为问题规定候选人的数字永远不会等于或超过6位数,你可以将数组中的元素数量上限设置为1,000,000,这样你就可以:
#define A_BIG_NUMBER_HERE 1000000
这样就永远不会截断列表。
然后,将你在第一个循环中的printf()
调用更改为以下内容:
if (list_cnt < A_BIG_NUMBER_HERE) /* 如果数组中仍有空间 */
list[count++] = copy_of_num; /* 你应该在计算数字时复制num,因为它在计算数字时被破坏了。 */
然后,在打印count
之后,只需使用另一个循环逐个打印list
数组中的每个数字。记得在循环开始时保存num
的值,因为你会破坏它(在第一个循环中获取所有数字之后,你会得到num == 0
),所以像这样的声明会很有用:
const int copy_of_num = num;
就在获取存储在num
中的候选数字后,会很有趣使它正确运行。
还有一件事,如果你传递的范围使你找到的数字多于可以存储在list
中的数字,最好打印一个警告消息,指示列表将被截断,因为你的程序仅支持不超过A_BIG_NUMBER_HERE
个元素。
在分析了您的代码后,我看到了几个地方可以优化计算,通过重复使用你已经获取的关于考虑数字的数据。
...
int digit = 0;
int temp = i;
while (temp != 0)
{
temp = temp / 10;
digit++;
}
可以通过考虑一个2位数或更少的数字是小于100的数字,以及一个6位数或更多的数字可以测试是否大于或等于1000000来优化这一步。所以可以像这样进行测试:
if (i < 100 || i >= 1000000) {
continue; /* 继续下一个数字 */
}
if (digit > 2 && digit < 6)
{
int a[digit];
尽量不要使用可变长度数组。这需要编译器做更多的计算来适应局部变量(如果任何局部变量需要调整长度,那么跟踪位于其后的所有其他变量将变得复杂,因为它们的位置必须在运行时计算,这会使你的代码变得更慢),而整数永远不会需要超过9位数(你的问题不允许你处理大于6位数的数字),一个很好的替代方案是给a
一个固定大小,比如10位数:
int a[10]; /* 更好 */
接下来:
int num = i;
int index = digit - 1;
while (num != 0)
{
a[index] = num % 10;
num = num / 10;
index--;
}
你的解决方案很聪明,我试图看看你是如何解决从右到左获取数字的要求,同时又必须从左到右处理它们的问题(奇数和偶数根据总位数在考虑从左到右时会改变位置)。另一种不需要将数字保存在数组中的替代方法是在并行中进行奇数和偶数的求和,然后在最后交换它们,这使得算法不依赖于实际位数。
int sum = 0;
for (int j = 0; j < digit; j++)
{
if (j % 2 == 1)
{
sum += a[j];
}
}
正确!做得好。
在这里:
if (sum > 8 && (sum % 4 == 0))
{
for (int j = 0; j < digit; j++)
{
printf("%d", a[j]);
}
printf(" ");
count++;
}
你需要更
英文:
The way you execute your algorithm imposes you the order of printing. There's a reason for that. You don't know a priori the number of integers you will find in the range, so you cannot print it before you have got all the numbers.
To cope with this, you need to store (and never to print) the numbers (e.g. in an array, like list
is suggested below) while they are all being computed, until you end the loop, so you can then print the count
, and make a second loop to navigate the array list
, and print all the numbers then.
Printers have the infamous custom of printing top to bottom, and left to right for each line, so the printer imposes you the order you have to use to print the numbers. If you print first the list of numbers (in the loop), you will print the count at the end (where the printer head is)
Let me apologize for not showing complete code to do this, but it seems to be an academic homework example, so you have to work it out, but basically the idea is to have one more variable:
int list[A_BIG_NUMBER_HERE];
to store the individual list elements. TheA_BIG_NUMBER_HERE
constant should be large enough to allow you to manage a big list of numbers (there is a possibility of making the list dimension variable so you are not limited by the size of the array, but escapes to the subject, so I'll not use that approach here). Although, if you wan to be conservative, as the problem states that candidates will never be of equal or more than 6 digits, you can set an upper bound of 1,000,000 elements in the array, so you can
#define A_BIG_NUMBER_HERE 1000000
and that will make never truncate a list.
Then change the printf()
calls you do in the first loop to print the list elements into something like:
if (list_cnt < A_BIG_NUMBER_HERE) /* if there's still room in the array */
list[count++] = copy_of_num; /* you should have made a copy of num as you destroyed it on the computation of the digits. */
Then, after you print the count
, just use another loop to print, one by one, each number in list
array. Remember that you need to save the num
value at the beginning of the loop, because you destroy it (after the first loop to get all the digits, you get num == 0
) so, a declaration like:
const int copy_of_num = num;
just after getting the candidate number stored in num
, would be interesting to make it work correctly.
One more thing, if you pass a range that makes you find more numbers than can be stored in list
it should be nice to print a warning message indicating that the list will be truncated, as no more than A_BIG_NUMBER_HERE
elements are supported by your program.
After analizing your code a bit, I've seen sevaral places wher you can optimize your computation, by reusing the data you have acquired about the considered number.
...
int digit = 0;
int temp = i;
while (temp != 0)
{
temp = temp / 10;
digit++;
}
this step can be optimized by considering that a number that is 2 digit or less is a number that is less than 100, and a number that is 6 digits or more can be tested to be larger or equal to 1000000. So a test like:
if (i < 100 || i >= 1000000) {
continue; /* continue to the next number */
}
if (digit > 2 && digit < 6)
{
int a[digit];
try not to use a variable length array. This requires the compiler to do far more calculations to accomodate local variables (if any of the local variables has to be adjusted in length, tracking all the other variables that afe located after it, becomes a mesh, as their positions have to be calculated at runtime, making your code slower) and an integer will never require more than 9 digits (your problem doesn't allow you to process numbers bigger than 6 digits) a good alternative is to give a
a fixed size of, let's say, 10 digits:
int a[10]; /* far better */
next:
int num = i;
int index = digit - 1;
while (num != 0)
{
a[index] = num % 10;
num = num / 10;
index--;
}
your solution here was clever, I was trying to see how did you solve the requirement of getting the digits from right to left, while having to process them left ro right (odds and evens change positions depending on the total number of digits, when considered left to right). Another alternative that doesn't require to save the digits in an array is to carry the odds and evens sums in parallel, and exchange them at the end, which makes the algorithm independent of the actual number of digits you consider.
int sum = 0;
for (int j = 0; j < digit; j++)
{
if (j % 2 == 1)
{
sum += a[j];
}
}
correct! good job.
and here
if (sum > 8 && (sum % 4 == 0))
{
for (int j = 0; j < digit; j++)
{
printf("%d", a[j]);
}
printf(" ");
count++;
}
}
}
you need to change the computation you made by appending the number
to `list` above, instead of printing it.
and this piece of code:
printf("%d ", count);
will need to be changed into:
printf("%d:", count);
for (int i = 0; i < count; i++) {
printf(" %d", list[i]);
}
printf("\n");
Other considerations:
Your code looks good, but I'd not implement the modification you ask for, but only if it has been included as an output requirement. It requires extra processing, considering storing temporarily a list of the computed numbers and the code is a bit more complex than, e.g. you just print the list in one line, and the number of found elements in the next line (or not to print the number of found elements at all if it is not required) As an academic exercise the problem is good, but it is also a good things to show compact and readable code, than a complex thing that requires to do a lot of coding just to write the output in a format that is neither required nor needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论