作业帮 > 英语 > 作业

计算机算法的题Develop an algorithm which takes as input a single 9-

来源:学生作业帮 编辑:拍题作业网作业帮 分类:英语作业 时间:2024/04/28 19:37: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 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)
#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
再问: 不是要编程 是算法 最好是英文的 eg: read sin set digit1 = sin / 100000000
再答: 算法就是解决问题的步骤或曰过程,其实在题干中就已经写得很清楚了。下面我根据代码简单写下: .begin 0. read in SIN(9 digits, with rightmost digit the LSB digit, index it with 1) 1. double digits at SIN[2],SIN[4], SIN[6] and SIN[8] to get a 4-element int array step1_result 2. sum individual digits of each step1_result element, then sum the 4 result together to get step2_sum; 3. sum digits of SIN at SIN[9], SIN[7], SIN[5],SIN[3] together to get step3_sum 4. plus step3_sum and step2_sum, to get step4_sum 5. if the unit digit of step4_sum is 0, then we set step5_result with 0; or step5_result is set with 10 minus the unit digit of step4_sum. 6. if step5_result == SIN[1], then SIN is a valid number, and according to SIN[9], we can get which region it belongs to by looking up the region table. 7. if step5_result SIN[1], then SIN is not a valid number. .end.