gpt4 book ai didi

c - 如何使一个变量等于C中其他变量的字符串

转载 作者:行者123 更新时间:2023-11-30 21:40:45 26 4
gpt4 key购买 nike

这是完整的代码,挖掘变量是用户输入

#include <stdio.h>
#include <stdlib.h>

int main()
{
int num;
int dig1=0;
int dig2=0;
int dig3=0;
num=dig1,dig2,dig3;
fflush(stdin);
printf("Please enter the first digit of your three digit number:");
scanf("%d", &dig1);
fflush(stdin);
printf("Please enter the second digit of your three digit number:");
scanf("%d", &dig2);
fflush(stdin);
printf("Please enter the third digit of your three digit number:");
scanf("%d", &dig3);

if (num==(dig1*dig1*dig1)+(dig2*dig2*dig2)+(dig3*dig3*dig3))
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}

system("pause");
return 0;
}

如何使变量“num”等于“dig1”、“dig2”和“dig3”的所有输入。就像如果 dig 1 是 2、dig 2 是 4、dig 3 是 6 一样,num 将是 246。请帮忙!

最佳答案

您的问题可以这样解决:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int num;
int dig[3];
int i,j = 100;
int result = 0;
for(i = 0;i<3;i++){
fflush(stdin);
printf("Please enter the %d digit of your three digit number:",i+1);
scanf("%d", &dig[i]);
}

for(i = 0;i<3;i++){
result += pow(dig[i],3);
num += dig[i] * j;
j/=10;
printf("%d * %d = %d\n",dig[i],j,num);
}

if (num == result)
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}

system("pause");
return 0;
}

关于c - 如何使一个变量等于C中其他变量的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com