gpygy @ 2022-01-15 16:28:58
#include<stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c,x;
int i = 1;
scanf("%d-%d-%d-%c",&a,&b,&c,&x);
double a1 = a;double b1 = b;double c1 = c;
b1 = b * 1.0 / 100;
double sum1 = 0;
for(i = 2;i <= 4;i++)
{
sum1 += (int)b1 % 10 * i;
b1 = b1 * 10;
}
c1 = c * 1.0 / 10000;
double sum2 = 0;
for(i = 5;i <= 9;i++)
{
sum2 += (int)c1 % 10 * i;
c1 = c1 * 10;
}
double sum = a + sum1 + sum2;
double x1 = int(sum) % 11;
if(x1 == 10 && x == 'X' || x1 == x - '0')
{
printf("Right");
}
else
{
char ch = x1 + 48;
printf("%d-%d-%d-%c",a,b,c,x1 == 10?'X':ch);
}
return 0;
}
by gpygy @ 2022-01-15 16:36:40
原来打错了一个x应该是字符类型的,刚才改了还是70分
by Mandel520 @ 2022-01-15 17:34:19
您好, 这是由于浮点数舍入误差导致的问题, 把b1和c1在求余运算前先加上0.1, 就可以AC了
修改后的程序如下:
#include<stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c,x;
int i = 1;
scanf("%d-%d-%d-%c",&a,&b,&c,&x);
double a1 = a;double b1 = b;double c1 = c;
b1 = b * 1.0 / 100;
double sum1 = 0;
for(i = 2;i <= 4;i++)
{
sum1 += (int)(b1 + 0.1) % 10 * i; //别忘了加0.1
b1 = b1 * 10;
}
c1 = c * 1.0 / 10000;
double sum2 = 0;
for(i = 5;i <= 9;i++)
{
sum2 += (int)(c1 + 0.1) % 10 * i;//别忘了加0.1
c1 = c1 * 10;
}
double sum = a + sum1 + sum2;
double x1 = int(sum) % 11;
if((x1 == 10 && x == 'X') || (x1 < 10 && x1 == x - '0'))
{
printf("Right\n");
}
else
{
char ch = x1 + 48;
printf("%d-%d-%d-%c\n",a,b,c,x1 == 10?'X':ch);
}
return 0;
}
by gpygy @ 2022-01-17 14:21:02
@Jeremy_Mandel 明白了,对了,感谢大佬的解答。