使我的CS50解决方案更优雅,更高效

你好IAM初学者,一年级CS学生IAM,参加CS50,虽然完成了我的解决方案工作,但我完成了实施Luhn Check算法的第一周问题,但是我觉得它很草率且效率不高,请问有人可以建议我如何改进我的解决方案(第一周的问题,您不打算使用数组或函数)

这是代码

#include<stdlib.h>
#include<cs50.h>
int main()
{
    long long x,j=0;
    int len,i,len_flag,flag=0,flag_val=0,sp_len=0,check_flag=0; //basic integers related  to loop etc
    int res,sum,fin_sum=0,h=0,d2_sum=0,d1_sum=0,ff_num=0; // variables  related to extracting numbers 
    int sum_len=0,in_sum=0,in_fsum=0,len_sum=0,m=0; // extraing numbers from more than single  sum result 
    int sum_f=0,sum_final=0;


    do{
        x = get_long("enter a valid card number \n");
        j=x;
        len_flag = 0;
        len = 0;
        while(len_flag!=1)
        {
          x = x / 10;
          if(x==0)
          {
            len_flag = 1;   //finding the lenght of the number 
          }
          len++;
        }
        if(len==15||len==16||len==13)
        {
          flag = 1;
          sp_len =1;
        }
        else
        {
            flag=1;
        }

    }while(flag!=1);


    for(i=0;i<len;i++)
    {
       res = j % 10;
       j = j / 10;
       if(i%2!=0)
       {

        sum = res * 2;
        //printf("multi_res : %d \n",sum);
        len_flag = 0;
        sum_len = 0;
        len_sum = sum;
        while(len_flag!=1)
        {
            len_sum = len_sum / 10;
            if(len_sum==0)
            {
               len_flag=1;       // checking if the sum is more than single  digit 
            }   
            sum_len++;
            //printf("trig\n");
        }
        if(sum_len>1)
        {   x=0;
            while(x<sum_len)
            {
             in_sum = sum % 10;
             sum = sum/10; 
             in_fsum = in_fsum + in_sum; 
             //printf("double sum : %d \n",in_fsum);
             x++;

            }
        }

        fin_sum = fin_sum + sum;

       }
       if(i==len-1)
       {

         for(h=0;h < 1;h++)
         {
            fin_sum = fin_sum + in_fsum; 
         }

         d1_sum = res;
       }

       else if(i%2==0)
       {
        sum_f = sum_f + res; // adding up the number that where not  x2
       }

       if(i==len-2)
       {
         d2_sum = res;     //5555555555554444
       }

    }

    sum_final = sum_f + fin_sum;

    //printf("sum_final : %d\n",sum_final);
    //printf("sum_f_final : %d\n",sum_f);
    //printf("sum_in_final : %d\n",fin_sum);

    if(sum_final%10==0)
    {
        flag_val=1; // checking if the number is valid  
    }
    if(ff_num == 0)
    {                                   
        ff_num = (d1_sum*10) + d2_sum;     //flip
    }

    do
    {
       if(sp_len==0)
       {
        printf("INVALID\n");
        check_flag=1;
        break;
       }
       if((flag==1&&flag_val==1&&ff_num==34)||ff_num==37)
       {
        printf("AMEX\n");
        check_flag=1;
        break;
       }
       else if(flag==1&&flag_val==1&&ff_num>50&&ff_num<56)
       {
        printf("MASTERCARD\n");
        check_flag=1;
        break;
       }
       else if(flag==1&&flag_val==1&&d1_sum==4)
       {
        printf("VISA\n");
        check_flag=1;
        break;
       }
       else
       {
        printf("INVALID\n");
        check_flag=1;
        break;
       }
    }while(check_flag!=1);

    return 0;

}

我觉得我在编写代码时正在修复泄漏,我将尝试纠正一件事,而另一件事会出错,这就是最终结果

谢谢您的帮助(如果未正确回答问题,请原谅我第一次)

评论