计算机算法的题Develop an algorithm which takes as input a single 9-digit integer representing a Social Insurance Numbers (SIN) and determines whether or not it is valid.The algorithm should output if the SIN is valid or not.The rules determini
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/30 13:44:42
计算机算法的题Develop an algorithm which takes as input a single 9-digit integer representing a Social Insurance Numbers (SIN) and determines whether or not it is valid.The algorithm should output if the SIN is valid or not.The rules determini
计算机算法的题
Develop an algorithm which takes as input a single 9-digit integer representing a Social Insurance Numbers (SIN) and determines whether or not it is valid.The algorithm should output if the SIN is valid or not.The rules determining the validity of a SIN are best described in an example.Consider the number 620187252 as a possible SIN number.Positions specified below are read from right to left and counting starts from 1.
1.Multiply every even positioned digit by 2.
(2x2=4,1x2=2,7x2=14,5x2=10)
2.For each result from step 1 add all the digits from each result together.
(4+2+1+4+1+0 = 12)
3.Add the last 4 odd positioned digits of the SIN together.
(6+0+8+2 = 16)
Page 4 of 4
4.Add the results of steps 2 and 3 together.
(12+16 = 28)
5.Subtract the units digit of the total from step 4 from 10,unless the units digit is 0,in which case use it.
(10-8 = 2)
6.A SIN is valid if the digit from step 5 is the same as the least significant digit in the SIN and the most significant digit matches a used region digit according to the following list:
0 not used
1 Atlantic Provinces
2 Quebec
3 not used
4 Ontario
5 not used
6 Prairie Provinces
7 British Columbia
8 not used
9 not used
(in the example,the SIN is a valid number which was issued in the Prairie Provinces)
计算机算法的题Develop an algorithm which takes as input a single 9-digit integer representing a Social Insurance Numbers (SIN) and determines whether or not it is valid.The algorithm should output if the SIN is valid or not.The rules determini
#include
#include
#include
#define N 9
int main()
{
\x05char *region[]={"not used",
\x05\x05"Atlantic Provinces",
\x05\x05"Quebec",
\x05\x05"not used",
\x05\x05"Ontario",
\x05\x05"not used",
\x05\x05"Prairie Provinces",
\x05\x05"British Columbia",
\x05\x05"not used",
\x05\x05"not used",
\x05};
\x05char SIN[N+1];
\x05printf("input %d digit SIN #:",N);
\x05fflush(stdin);
\x05fgets(SIN,N+1,stdin);
\x05
\x05int i=0;
\x05int step1_result[N/2];
\x05char step1[N]={'\0'};
\x05int step2_sum=0;
\x05//step 1 偶数位上的数乘以2,保存在数组step1_result中
\x05// 字符数组step1把上面的结果各位数字转换成对应数字字符保存起来
\x05while(i