今天遇到的奇特操作
(错解)
#include<stdio.h>
#include<math.h>
int main(){
int year,capital;
float rate;
char x;
printf("Input rate, year, capital:");
scanf("%f,%d,%d",&rate,&year,&capital);
printf("Compound interest (Y/N)?");
getchar();
scanf("%c",&x);
if(x=='Y')
printf("deposit = %.4f", capital * pow((1 + rate),year));
if(x=='N')
printf("deposit = %.4f" , capital * (1 + rate * year));
return 0;
}
(正解)
#include <math.h>
int main()
{
int year;
double mon,rate;
char choice;
printf("Input rate, year, capital:");
scanf("%lf,%d,%lf",&rate,&year,&mon);
printf("Compound interest (Y/N)?");
scanf(" %c",&choice);
if(choice=='y' || choice=='Y')
{
printf("deposit = %.4f\n",mon*pow((1+rate),year));
}
else printf("deposit = %.4f\n",mon*(1+rate*year));
return 0;
}
但二者似乎没有本质上的区别
但其实里面有换行(\n)
以及一些用法的不同(不一定错,但是可能不符合题目要求)
甚至有不可间的字符(一般是复制粘贴带来的)
导致了程序的运行结果的不同