大佬能不能帮我看看为什么这个答案输出总是循环的一个身份证,而不是多个有问题的身份证?
#include<cstdio>
#include<cstring>
char find[15] = {'1','0','X','9','8','7','6','5','4','3','2'} ;
int power[20] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int main()
{
int n;
int result = 0;
int number = 0;
char str[20];
char * wrong[20];
scanf("%d", &n);
for(int j = 0; j < n; j++)
{
int i = 0;
scanf("%s", str);
for(i = 0; i < 17; i++)
if(str[i] >= '0' && str[i] <= '9')
result += (str[i]-'0')*power[i];
else
break;
if(i < 17)
wrong[number++] = str;
else if(find[result % 11] != str[17])
wrong[number++] = str;
}
if(number == 0)
printf("All passed\n");
else
{
for(int j = 0; j < number; j++)
printf("%s\n", wrong[j]);
}
return 0;
}
采纳
// Q1052502.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<cstdio>
#include<cstring>
#include<stdlib.h>
char find[15] = {'1','0','X','9','8','7','6','5','4','3','2'} ;
int power[20] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int main()
{
int n;
int result = 0;
int number = 0;
char str[20];
char * wrong[20];
for (int i = 0; i < 20; i++)
wrong[i] = (char *)malloc(sizeof(char) * 100);
scanf("%d", &n);
for(int j = 0; j < n; j++)
{
int i = 0;
scanf("%s", str);
for(i = 0; i < 17; i++)
if(str[i] >= '0' && str[i] <= '9')
result += (str[i]-'0')*power[i];
else
break;
if(i < 17)
strcpy(wrong[number++], str);
else if(find[result % 11] != str[17])
strcpy(wrong[number++], str);
}
if(number == 0)
printf("All passed\n");
else
{
for(int j = 0; j < number; j++)
printf("%s\n", wrong[j]);
}
return 0;
}