[英]Write a program to generate the first n terms in the series — 34,18,10,6,4,
Input is n which is a single integer. 输入为n ,它是一个整数。
Output consists of the series separated by blank space. 输出由用空格分隔的系列组成。
Sample Input: 输入样例:
6 6
Sample Output: 样本输出:
34 18 10 6 4 3 34 18 10 6 4 3
For this program I had initially declared two variables and initialized them to 34 and 18 respectively. 对于该程序,我最初声明了两个变量并将它们分别初始化为34和18。 From there on it was easy to calculate the next values and keep printing them. 从那里开始,很容易计算下一个值并继续打印它们。 But this was done assuming the series starts from only 34 and the series only consists of 6 terms. 但这是在假设系列仅从34开始且系列仅包含6个术语的情况下完成的。 This output wasn't accepted as it may have not satisfied all the test cases. 不接受此输出,因为它可能不满足所有测试用例。 So I wrote the following program to find out series based on the value of n entered by the user whereas the program will find out where the series starts and calculate the series from there onward. 因此,我编写了以下程序,根据用户输入的n值找出序列,而该程序将找出序列的起始位置,并从此开始计算序列。
#include <stdio.h>
int main()
{
int a=34,b=18,n,i,sub=16;
scanf("%d",&n);
if(n>6)
{
for(i=0;i<n-6;i++)
{
sub=sub*2;
a=a+sub;
b=b+(sub/2);
}
}
printf("%d %d",a,b);
for(i=0;i<n-2;i++)
{
sub=sub/2;
b=b-sub;
printf(" %d",b);
}
return 0;
}
This way I calculate what the first term of the series is and then start the pattern from there. 这样,我计算出系列的第一项是什么,然后从那里开始模式。 But it is still not being accepted. 但是它仍然没有被接受。 Is there some way this code is not satisfying the test cases or is it supposed to be improved? 此代码有某种方式不能满足测试用例,还是应该加以改进?
I suggest using std::generate
我建议使用std::generate
Example: 例:
int main()
{
std::size_t const N = 6;
std::vector<int> v(N);
auto start = 0;
std::cin >> start;
std::generate(v.begin(), v.end(), [x = start * 2 - 1]() mutable {
return x = (x / 2 + 1);
});
for (auto const& i : v) std::cout << i << ' ';
}
This is quite old, but if anybody stumbles open it one day: 这已经很老了,但是如果有人偶然打开它的话:
There was several little things that were wrong or could be better in the code : 代码中有些小错误或可能会更好:
1 - Don't assume that the user will not enter a number greater than 6 and run the code either way, even if result will be abcde 0 0 0 ... (this is even more important if your code is to be submitted to a machine, as the machine will try a lot more possibilities to break your code) 1-即使结果将为abcde 0 0 0 ...,也不要假设用户不会输入大于6的数字并以任何一种方式运行代码(这对于将代码提交到更为重要。一台机器,因为该机器将尝试更多的可能性来破坏您的代码)
2 - If you are going to initialize a and b (two first numbers), you'll have to include in your code the possibility of the user only wanting those two numbers. 2-如果要初始化a和b(两个前两个数字),则必须在代码中包括用户仅需要这两个数字的可能性。
3 - As you already initialize your first two numbers, don't start your loop at i=0
3-因为您已经初始化了前两个数字,所以请勿在i=0
开始循环
4 - There could be more things to be pointed out but, mainly, as commented before : 4-可能还有更多要指出的事情,但主要是如前所述:
The pattern in the series is that T(n+1) = T(n)/2+1. 系列中的模式是T(n + 1)= T(n)/ 2 + 1。 Your code can be simplified quite a bit. 您的代码可以简化很多。
Here is an easier version of the code (there is always better ones, but this ones is easy to understand at all levels): 这是一个简单的代码版本(总是有更好的代码,但是这些代码在所有级别上都很容易理解):
Using a while
loop : 使用while
循环:
#include <stdio.h>
int main() {
int n, a = 34, i = 2;
scanf("%d", & n);
if (n == 1)
printf("%d", a);
else {
printf("%d ", a);
while (i <= n) {
a = (a / 2) + 1;
printf("%d ", a);
i++;
}
}
return 0;
}
Another version of the same code using a for
loop 使用for
循环的同一代码的另一个版本
#include <stdio.h>
int main() {
int n, a = 34, i;
scanf("%d", & n);
if (n == 1)
printf("%d", a);
else {
printf("%d ", a);
for (i = 2; i <= n; i++) {
a = (a / 2) + 1;
printf("%d ", a);
}
}
return 0;
}
I hope this helps ! 我希望这有帮助 !
I think the problem statement is still missing some constraints. 我认为问题陈述仍然缺少一些约束。
You either need the starting number like 34 along side number of terms ie 6 form the user. 您要么需要像术语旁边的数字那样的起始编号(例如34) ,即用户形成6。
Or you should provide more examples to support the fact that starting number is equal to num*num - 2
. 或者,您应该提供更多示例以支持起始编号等于num*num - 2
的事实。
Moreover you can shorten your code length to some extend. 此外,您可以缩短代码长度。
#include <stdio.h>
int main(void) {
int noOfTerms = 6, number=34;
for(int i=0;i<noOfTerms;i++){
printf("%d ",number);
number = number - (number/2) + 1;
}
return 0;
}
Output:
34 18 10 6 4 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.