C 编写一个程序,求正奇数和正偶数的乘积小于或等于30
我在为这个问题编写C程序时遇到了一些问题。也许我看错了问题,并且用了错误的方法。有人能帮我吗?这就是我尝试的方式C 编写一个程序,求正奇数和正偶数的乘积小于或等于30,c,types,int,C,Types,Int,我在为这个问题编写C程序时遇到了一些问题。也许我看错了问题,并且用了错误的方法。有人能帮我吗?这就是我尝试的方式 #include<stdio.h> void main(void) { int j, sum=0; long int product=1; for(j=1;j<=30;j=j+2) { sum=sum+j; } for(j=2;j<=30;j=j+2) { product=
#include<stdio.h>
void main(void)
{
int j, sum=0;
long int product=1;
for(j=1;j<=30;j=j+2)
{
sum=sum+j;
}
for(j=2;j<=30;j=j+2)
{
product=product*j;
}
printf("\nThe sum of positive odd numbers is: %d", sum);
printf("\nThe product of positive even numbers is: %d", product);
}
我把产品零件弄错了。我尝试过使用无符号long int、long-long、无符号long-long。没有任何效果。尝试在
printf
中使用%ld
而不是%d
:
printf(“\n正偶数的乘积为:%ld”,product)代码>
因为它是一个long int
而不是int
如果使用long-long-int
,则需要%lld
。考虑到这是一款非常大的产品,您可能需要长款。我不知道您的平台的long int
是32位还是64位,但您肯定需要一个64位的数字
根据具体的平台和编译器,long-long
格式字符串可能会有所不同,但现在大部分内容已在%lld
上标准化。特别是,旧的Microsoft编译器有时使用%I64d
尝试在printf中使用%ld
而不是%d
:
printf(“\n正偶数的乘积为:%ld”,product)代码>
因为它是一个long int
而不是int
如果使用long-long-int
,则需要%lld
。考虑到这是一款非常大的产品,您可能需要长款。我不知道您的平台的long int
是32位还是64位,但您肯定需要一个64位的数字
根据具体的平台和编译器,long-long
格式字符串可能会有所不同,但现在大部分内容已在%lld
上标准化。特别是,旧的Microsoft编译器有时使用%I64d
就所有奇数之和小于30而言没有问题,因为它只是225
。但是所有偶数(或奇数)的乘积小于30是一个巨大的数字。为此,您需要一个容量更大的数据类型。在下面的程序中,对于产品
,我简单地使用了double
而不是long int
,并且我使用了%e
格式说明符以整洁的方式在prinf()
中显示产品,尽管您也可以使用%f
#include<stdio.h>
int main(void) //Return type of main() is "int",not "void" as you've used
{
int j, sum=0;
double product=1; //Change type of "product" to "double"
for(j=1;j<=30;j=j+2)
{
sum=sum+j;
}
for(j=2;j<=30;j=j+2)
{
product=product*j;
}
printf("The sum of positive odd numbers is: %d\n", sum);
printf("The product of positive even numbers is: %e",product); //Use %e
}
对于小于30的所有奇数之和没有任何问题,因为它只是225
。但是所有偶数(或奇数)的乘积小于30是一个巨大的数字。为此,您需要一个容量更大的数据类型。在下面的程序中,对于产品
,我简单地使用了double
而不是long int
,并且我使用了%e
格式说明符以整洁的方式在prinf()
中显示产品,尽管您也可以使用%f
#include<stdio.h>
int main(void) //Return type of main() is "int",not "void" as you've used
{
int j, sum=0;
double product=1; //Change type of "product" to "double"
for(j=1;j<=30;j=j+2)
{
sum=sum+j;
}
for(j=2;j<=30;j=j+2)
{
product=product*j;
}
printf("The sum of positive odd numbers is: %d\n", sum);
printf("The product of positive even numbers is: %e",product); //Use %e
}
计算使用非单精度整数(32位)
#包括
#包括
#包括
typedef无符号短UInt16;
typedef无符号UInt32;
类型定义结构{
大小;
UInt16*nums;//数组
}联合国大学;
void UNums_init(UNums*num,UInt16 n){
num->nums=malloc(sizeof(UInt16));
num->nums[0]=n;
num->size=1;
}
void UNums_mul(UNums*num,UInt16 n){
UInt16进位=0;
尺寸i;
对于(i=0;isize;++i){
UInt32 wk=n;
wk=wk*num->nums[i]+进位;
num->nums[i]=wk%10000;
进位=wk/10000;
}
如果(携带){
num->size+=1;
num->nums=realloc(num->nums,num->size*sizeof(UInt16));
num->nums[i]=进位;
}
}
作废UNums\U打印(UNums*num){
大小i=num->size;
int w=0;
做{
--一,;
printf(“%0*hu”,w,num->nums[i]);
如果(!w)w=4;
}而(i!=0);
}
作废未使用的数据(未使用的*num){
自由(num->nums);
num->nums=NULL;
}
内部主(空){
联合国大学;
UInt16Ⅰ;
断言(sizeof(UInt32)==4);//32位
断言(sizeof(UInt16)==2);//16位
UNums_init(&n,1);
对于(i=2;i计算,使用非单精度整数(32位)
#包括
#包括
#包括
typedef无符号短UInt16;
typedef无符号UInt32;
类型定义结构{
大小;
UInt16*nums;//数组
}联合国大学;
void UNums_init(UNums*num,UInt16 n){
num->nums=malloc(sizeof(UInt16));
num->nums[0]=n;
num->size=1;
}
void UNums_mul(UNums*num,UInt16 n){
UInt16进位=0;
尺寸i;
对于(i=0;isize;++i){
UInt32 wk=n;
wk=wk*num->nums[i]+进位;
num->nums[i]=wk%10000;
进位=wk/10000;
}
如果(携带){
num->size+=1;
num->nums=realloc(num->nums,num->size*sizeof(UInt16));
num->nums[i]=进位;
}
}
作废UNums\U打印(UNums*num){
大小i=num->size;
int w=0;
做{
--一,;
printf(“%0*hu”,w,num->nums[i]);
如果(!w)w=4;
}而(i!=0);
}
作废未使用的数据(未使用的*num){
自由(num->nums);
num->nums=NULL;
}
内部主(空){
联合国大学;
UInt16Ⅰ;
断言(sizeof(UInt32)==4);//32位
断言(sizeof(UInt16)==2);//16位
UNums_init(&n,1);
对于(i=2;i)您的产品超出了存储类型的限制。请尝试将long int
替换为更大的存储类型。2*4*8…*28给出了大约30位数字。您几乎需要一个浮点类型来保存它。值得的是:N个连续奇数的总和(从1开始)给出N的平方,因此您可以更快速、更轻松地计算该部分。使用模将代码压缩为1个循环。伪代码-for(j=1,j)您的答案应该正好适合64位整数。这个简单乘积的绝对大小是
The product of positive even numbers is: 4.284987e+16
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef unsigned short UInt16;
typedef unsigned UInt32;
typedef struct _unums {
size_t size;
UInt16 *nums;//array
} UNums;
void UNums_init(UNums *num, UInt16 n){
num->nums = malloc(sizeof(UInt16));
num->nums[0] = n;
num->size = 1;
}
void UNums_mul(UNums *num, UInt16 n){
UInt16 carry = 0;
size_t i;
for(i=0;i<num->size;++i){
UInt32 wk = n;
wk = wk * num->nums[i] + carry;
num->nums[i] = wk % 10000;
carry = wk / 10000;
}
if(carry){
num->size += 1;
num->nums = realloc(num->nums, num->size * sizeof(UInt16));
num->nums[i] = carry;
}
}
void UNums_print(UNums *num){
size_t i = num->size;
int w = 0;
do{
--i;
printf("%0*hu", w, num->nums[i]);
if(!w) w = 4;
}while(i!=0);
}
void UNum_drop(UNums *num){
free(num->nums);
num->nums = NULL;
}
int main( void ){
UNums n;
UInt16 i;
assert(sizeof(UInt32) == 4);//32bit
assert(sizeof(UInt16) == 2);//16bit
UNums_init(&n, 1);
for(i=2;i<=30;i+=2)
UNums_mul(&n, i);
UNums_print(&n);//42849873690624000
UNum_drop(&n);
return 0;
}