如果输入是整数[不是数组]:6977。我需要每 9 到 7 更改一次,因此结果将是:6777。
这是我写的代码,这是我在想如何解决问题的想法,但这是错误的。我作业的重点是更改数字的特定数字[元素],但我不知道该怎么做。该函数应该做到这一点。更多示例:输入:12345555 [将每 5 更改为 3] 结果应为 12343333。
int function (int *n,int i){
int temp=n[i]; //tepmorary veraible of the passed number
int result=10;
int devided;
while (temp!=0){
devided=temp%10;//get the last number example 123 = 3;
if (devided==9){ //if its 9 change the last didigt to 7
devided=7;
}
result*=devided;//multiply the last didgit with 10
temp/=10;
}
return result;
}
void main (void){
int a[100],i=0;
while (scanf_s ("%d",&a[i])){
printf ("%d",function(a,i));
i++;
}
}
最佳答案
你的result*=devided;
逻辑是错误的。您需要将数字(乘以 10 位)添加到结果中。这是您问题的解决方案。
int function (int *n, int i){
int temp=n[i]; //tepmorary veraible of the passed number
int result=0;
int mult=1;
int devided;
while (temp!=0){
devided=temp%10;//get the last number example 123 = 3;
if (devided==9){ //if its 9 change the last didigt to 7
devided=7;
}
result+=(devided * mult);//multiply the last didgit with 10
temp/=10;
mult*=10;
}
return result;
}
关于c - 如何转换一个数字的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57874155/