为什么循环递增而不是只打印循环数?
Why does cycle increment and not print out just the number of cycles?
请在下面提供我的代码。我正在尝试获取 8、3、1、8、1、1、1、1、1、1。对于我的代码输出中的循环,当用户输入 45 23 6 12 0 0 0 0 0 0 时。但出于某种原因,它将下一个数字加起来减去 1。所以我得到 8、10、10、17、17、17、17、17、17、17。我试图解决这个问题,但我所做的似乎无济于事.有人可以帮我指出发生这种情况的原因吗?
#include<stdio.h>
#include<stdlib.h>
int sum_divisors(int); //Function Prototype
/*Just wanted to do an example to show you don't need
*function prototypes if you write the function itself
*before main.
*/
int cycles(int x){
static int count = 1;
int j;
if(x == sum_divisors(x)){
printf("%d%13d Cycle(s) \n", x, count);
}else{
x = sum_divisors(x);
count++;
printf("%d, ",x);
cycles(x);
}
}
int main(void){ //Start of main
int count = 0;
int x, sum;
int i = 0;
int nums [9];
puts("Please enter 10 integers. Fill up the rest with 0s. Please only type in numbers if not you might not get the output you want.");
while(scanf("%d ", &x) != EOF){
if(x > 100){
puts("Wrong input");
exit(1);
}else{
count++;
nums[i] = x;
++i;
}
if(i == 9){
break;
}
}
for(i=0; i<count; i++){
sum = sum_divisors(nums[i]);
printf("%d, ", nums[i]);
cycles(nums[i]);
}
puts("");
return 0;
} //end of main
int sum_divisors(int n){
int i;
int sum = 0;
int prime_ind = 0;
if(n <= 1){
sum = 0;
return sum;
}
for(i=2; i<n; i++){
if(n%i == 0){
prime_ind = 1;
break;
}
}
if(prime_ind == 0){
sum = 1;
}else{
for(i=1; i<((n/2)+1); i++){
if(n%i == 0){
sum += i;
}
}
}
return sum;
}
int cycles(int x){
static int count = 1;
像这样的静态变量将在该行的第一次执行时被初始化为给定值仅。之后,它将在多个函数调用中保持其值,不仅是递归调用,还有来自 main()
的 "fresh" 调用。您只需保持 递增count
。它永远不会重置为 1
.
我认为这不是(完全)您想要的。真的很难说,因为您没有告诉我们您的功能实际上 打算 做什么——既没有在问题中也没有,正如我希望的那样,在评论中。我不打算对这些信息进行逆向工程。
那时我也停止检查您的代码,因为 static
很可能是您问题的核心。可能还有更多问题,例如评论中提示的越界访问。
我已经查看了您的程序。
你的问题出在函数
int cycles(int x){}
所以改成
int cycles(int x){
static int count = 1;
int j;
if(x == sum_divisors(x)){
printf("%d%13d Cycle(s) \n", x, count);
count=1; // add this line
}else{
x = sum_divisors(x);
count++;
printf("%d, ",x);
cycles(x);
}
}
此处调用 new cycle() 时未初始化静态变量。
请在下面提供我的代码。我正在尝试获取 8、3、1、8、1、1、1、1、1、1。对于我的代码输出中的循环,当用户输入 45 23 6 12 0 0 0 0 0 0 时。但出于某种原因,它将下一个数字加起来减去 1。所以我得到 8、10、10、17、17、17、17、17、17、17。我试图解决这个问题,但我所做的似乎无济于事.有人可以帮我指出发生这种情况的原因吗?
#include<stdio.h>
#include<stdlib.h>
int sum_divisors(int); //Function Prototype
/*Just wanted to do an example to show you don't need
*function prototypes if you write the function itself
*before main.
*/
int cycles(int x){
static int count = 1;
int j;
if(x == sum_divisors(x)){
printf("%d%13d Cycle(s) \n", x, count);
}else{
x = sum_divisors(x);
count++;
printf("%d, ",x);
cycles(x);
}
}
int main(void){ //Start of main
int count = 0;
int x, sum;
int i = 0;
int nums [9];
puts("Please enter 10 integers. Fill up the rest with 0s. Please only type in numbers if not you might not get the output you want.");
while(scanf("%d ", &x) != EOF){
if(x > 100){
puts("Wrong input");
exit(1);
}else{
count++;
nums[i] = x;
++i;
}
if(i == 9){
break;
}
}
for(i=0; i<count; i++){
sum = sum_divisors(nums[i]);
printf("%d, ", nums[i]);
cycles(nums[i]);
}
puts("");
return 0;
} //end of main
int sum_divisors(int n){
int i;
int sum = 0;
int prime_ind = 0;
if(n <= 1){
sum = 0;
return sum;
}
for(i=2; i<n; i++){
if(n%i == 0){
prime_ind = 1;
break;
}
}
if(prime_ind == 0){
sum = 1;
}else{
for(i=1; i<((n/2)+1); i++){
if(n%i == 0){
sum += i;
}
}
}
return sum;
}
int cycles(int x){
static int count = 1;
像这样的静态变量将在该行的第一次执行时被初始化为给定值仅。之后,它将在多个函数调用中保持其值,不仅是递归调用,还有来自 main()
的 "fresh" 调用。您只需保持 递增count
。它永远不会重置为 1
.
我认为这不是(完全)您想要的。真的很难说,因为您没有告诉我们您的功能实际上 打算 做什么——既没有在问题中也没有,正如我希望的那样,在评论中。我不打算对这些信息进行逆向工程。
那时我也停止检查您的代码,因为 static
很可能是您问题的核心。可能还有更多问题,例如评论中提示的越界访问。
我已经查看了您的程序。 你的问题出在函数
int cycles(int x){}
所以改成
int cycles(int x){
static int count = 1;
int j;
if(x == sum_divisors(x)){
printf("%d%13d Cycle(s) \n", x, count);
count=1; // add this line
}else{
x = sum_divisors(x);
count++;
printf("%d, ",x);
cycles(x);
}
}
此处调用 new cycle() 时未初始化静态变量。