大佬教程收集整理的这篇文章主要介绍了必须打印数字序列 15, 12, 24, 21, 42, 39, 78, 75, 150, 147,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我看来,数字序列由 2 个单独的序列组成。这是我到目前为止的代码。我不确定您是否必须使用 while 或 for 循环。我在编码方面相当新,所以如果有人可以帮助我。 如果输入的值为 10,它必须给出序列的前 10 项,如果我输入 5,它必须给出序列的前 5 项。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a,n = 1,t,y = 1; // First Numerical Sequence
int b,m = 2,s,x = 2; // Second Numerical Sequence
int d,r,z; // Extra
printf("Enter A Tn : ");
scanf(" %d",&z);
printf("\n");
while (n <= z) {
a = 15;
r = pow(2,n - y);
d = (9 * (r - 1)) / (2 - 1);
t = a + d;
printf("%d\n",t);
n += 2;
y++;
}
while (m <= z) {
b = 12;
r = pow(2,m - X);
d = (9 * (r - 1)) / (2 - 1);
s = b + d;
printf("%d\n",s);
m += 2;
x++;
}
printf("\n");
return 0;
}
这将完成工作。
#include <stdio.h>
int main(){
int val,ic; //iteration count,will print ic*2 number
scanf("%d %d",&val,&ic);
for(int i = 0;i<ic;i++){
printf("%d ",val);
val-=3;
printf("%d ",val);
val*=2;
}
printf("\n");
}
如何编译和运行:
C:\Users\stike\Desktop>rem assume you saved it in a.c
C:\Users\stike\Desktop>gcc -o a a.c
C:\Users\stike\Desktop>a
15
5
15 12 24 21 42 39 78 75 150 147
,
如果要打印从 15 和 o 到用户输入的某个数字的相同序列,可以按照以下代码进行操作。
希望你理解了给定数字时的序列模式,将数字减3,然后打印,然后将数字加倍打印,再减3,同样,它继续。
>#include <stdio.h>
int main() {
int endNum;
int beginNum = 15;
printf("Enter the end: ");//(lineA) here we initialize the variables with beginNum as 15
scanf("%d",&endNum); //(Line B) let the user to input endNum of the sequence,in the example it is 147
while ((beginNum-3) <= endNum) { // checks the condition
printf("%d ",beginNum);
if(beginNum==endNum) return 0; //check whether you print the end number.
beginNum -= 3; // reduce by 3
printf("%d ",beginNum);
beginNum *= 2; // multiply by 2
}
return 0;
}
如果您不需要用户输入 endNum,只需将值 147 初始化为变量 endNum。 并删除 A 和 B 行。
,这是使用 static
变量的另一种方法
#include <stdio.h>
int next(void) {
static int last,n = 0;
if (n++ == 0) return last = 15; // 1st element of sequence
if (n % 2) return last = last * 2; // odd elements
return last = last - 3; // even elements
}
int main(void) {
for (int k = 0; k < 10; k++) {
printf("%d ",next());
}
puts("");
return 0;
}
以上是大佬教程为你收集整理的必须打印数字序列 15, 12, 24, 21, 42, 39, 78, 75, 150, 147全部内容,希望文章能够帮你解决必须打印数字序列 15, 12, 24, 21, 42, 39, 78, 75, 150, 147所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。