C基本数学运算 [英] C basic mathematical operation

查看:125
本文介绍了C基本数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,从用户(键盘)读取整数,即n,a和b。你的程序sohuld打印满足以下条件的所有正数:



*小于n的数字和它的正除数的数量和总和的比率等于a / b。



你应该按升序打印这个数字,然后在sam eline上打印他们的正数除数作为数字。



示例

如果用户输入n:10,a:1和b:2那么因为只有6满足条件,输出应为6它的正数除数如下:

6 1 2 3 6



(我是一流的工科学生。我不想错过Basic。作业,我必须在明天结束之前交付它。:/)



我尝试了什么:



Write a program that reads there integers from the user(keyboard) namely, n, a and b. Your program sohuld print all positive numbers satisfying the following condition:

*The number smaller than n and the ratio of the number and sum of its positive divisors is equal to a/b.

You should print the number in ascending order and print their positive divisors on the sam eline as the numbers.

Example
If the user entered n: 10, a: 1, and b:2 then since only 6 satisfies the condition the output should be 6 and it positive divisors as follows :
6 1 2 3 6

(I am a first class engineering student. I do not want to miss the Basic. Homework, I have to deliver it by the end of the day tomorrow. :/ )

What I have tried:

<pre>int a,b,n;

int main()
{
	printf("please enter a number for n");
	scanf("%d",n);
	printf("please enter a number for a");
	scanf("%d",a);
	printf("please enter a number for b");
	scanf("%d",b);
	
	int i = 1;
	for(i = 1; i<n; i++)
	
	return 0;
}

int sumofdivisors(int a)
{
	int i;
	int sum = 0;
	for(i=1;i<=a;i++)
	{
		if(a%i==0)
		sum += i;
	}
	return sum;
}

推荐答案

你应该为每个需要的输出写一个函数来传递它而不是输出。



您可以定义这样的数组,但您还需要释放内存:

You should write for every needed output a function which deliver it and than do the output.

You can define an array like this, but you also need to free the memory:
int *arr = 0;// define pointer

int count = fillArrayFunc( arr, a, b, n );

for( int i = 0; i <count; i++ ) {
  int number = arr[i];//access
  printf("The number is: %d", number);
}
free( arr );//release the memory

//somewhere else do math and calc the stuff
int count fillArrayFunc( int *arr, int n, int a, int b) 
{
   arr = malloc( sizeof(int) * count );// alloc on input pointer

  return count;// needed for the output
}



您错过了询问之前,你有现在问题。


以下代码没有意义。循环的第一次迭代将终止你的程序。

The following code makes no sense. The first iteration of your loop will terminate your program.
for(i = 1; i<n; i++)

return 0;


这篇关于C基本数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆