Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #define MAX_DIGITS_INPUT 100001
- extern int errno;
- /*
- argc - counter of input arguments
- argv - pointer to input arguments
- argv[0] = revsum
- argv[1] = <input_file>
- */
- int weightCount(int M[MAX_DIGITS_INPUT],int cnt,int M_weights[MAX_DIGITS_INPUT],int forstart){
- if (cnt % 2 == 1 ) {
- if(M[cnt/2]%2==0) M_weights[cnt/2]=0;
- else M_weights[cnt/2]=1;}
- for(int i=forstart; i<cnt/2;i++){
- M_weights[cnt-i-1]=M_weights[i-2];
- if (M[i]-M[cnt-1-i]==1 || M[i]-M[cnt-1-i]==-9){
- if (M_weights[cnt-1-i]==0 || (M[i-1]==0 && M[cnt-i]==9)) { M_weights[i]=1; M_weights[cnt-1-i]=0;}
- else {return 0;}}
- else if (M[i]-M[cnt-1-i]==-1 || M[i]-M[cnt-1-i]==9){
- if (M_weights[cnt-1-i]==1 || (M[cnt-i]==0 && M[i-1]==9)){ M_weights[i]=0; M_weights[cnt-1-i]=1;}
- else {return 0;}}
- else if (M[i]==M[cnt-1-i]){
- // M_weights[cnt-1-i]=M_weights[i-2];
- M_weights[i]=M_weights[cnt-1-i];
- if (M_weights[cnt-1-i]==1 || (M[cnt-i]==0 && M[i-1]==9)) {{M_weights[i]=1;M_weights[cnt-1-i]=1;}}
- if (M_weights[cnt-1-i]==0 || (M[i-1]==0 && M[cnt-i]==9)) {M_weights[i]=0;M_weights[cnt-1-i]=0;}
- }
- else return 0;
- }
- return 1;
- }
- int main (int argc, char **argv) {
- int M[MAX_DIGITS_INPUT];
- int M_weights[MAX_DIGITS_INPUT];
- int cnt = 0; //isWanted = 1;
- FILE *fp;
- /*Opening file while checking for errors*/
- fp = fopen(argv[1], "r");
- if (fp == NULL) {
- fprintf(stderr, "Value of errno: %d\n", errno);
- exit(1);
- }
- /*Read one digit at a time and store it in int array M*/
- while(!feof(fp)){
- if (fscanf(fp, "%1d", &M[cnt]) == 1){
- cnt++;
- }
- }
- int answerbits=cnt;
- M_weights[cnt-1]=0;
- int flag=1;
- if (M[0]!=1) {
- M_weights[cnt-2]=0;
- M_weights[cnt-1]=0;
- if (M[0]-M[cnt-1]==1)M_weights[0]=1;
- else if (M[0]==M[cnt-1])M_weights[0]=0;
- else flag=0;
- if (M[1]-M[cnt-2]==1 || M[1]-M[cnt-2]==-9)M_weights[1]=1;
- else if (M[1]==M[cnt-2]) M_weights[1]=0;
- else flag=0;
- if (flag==1){
- int forstart=2;
- flag = weightCount(M,cnt,M_weights,forstart);}
- // printf("%s\n","hi" );
- //printf("%d\n", flag);
- }
- else{
- // printf("%s\n", "hi" );
- M_weights[cnt-2]=0;
- M_weights[cnt-1]=0;
- if (M[0]-M[cnt-1]==1)M_weights[0]=1;
- else if (M[0]==M[cnt-1])M_weights[0]=0;
- else flag=0;
- if (M[1]-M[cnt-2]==1)M_weights[1]=1;
- else if (M[1]==M[cnt-2]) M_weights[1]=0;
- else flag=0;
- if (flag==1)
- {
- int forstart=2;
- flag=weightCount(M,cnt,M_weights,forstart);
- }
- if (flag==0){
- printf("%s\n","hi" );
- flag=1;
- cnt++;
- M_weights[cnt-2]=0;
- if (M[1]!=0) M_weights[cnt-3]=1;
- else M_weights[cnt-3]=0;
- M_weights[0]=1;
- if (M[1]-M[cnt-2]==1 || M[1]-M[cnt-2]==-9) M_weights[1]=1;
- else if (M[1]==M[cnt-2]) M_weights[1]=0;
- else flag=0;
- if (M_weights[cnt-3]==1)
- {
- if (M[2]==M[cnt-3])M_weights[2]=1;
- else M_weights[2]=0;
- }
- else {
- if (M[2]==M[cnt-3])M_weights[2]=0;
- else M_weights[2]=1;
- }
- int forstart=2;
- flag=weightCount(M,cnt,M_weights,forstart);
- cnt--;
- answerbits--;
- }
- }
- int ANSWER[MAX_DIGITS_INPUT];
- if (answerbits%2==1) ANSWER[answerbits/2]= (10*M_weights[cnt/2-1]+M[cnt/2])/2;
- for (int j=0;j<answerbits/2;j++){
- ANSWER[answerbits-1-j]=((M[cnt-1-j]-M_weights[cnt-1-j])+10*M_weights[cnt-2-j])/2;
- ANSWER[j]=((M[cnt-1-j]-M_weights[cnt-1-j])+10*M_weights[cnt-2-j])-ANSWER[answerbits-1-j];
- }
- for(int i=0;i<cnt;i++)
- printf("%d",M_weights[i] );
- printf("\n");
- if (flag==1) {
- int c=0;
- int plus=0;
- int result[MAX_DIGITS_INPUT];
- for(int j=0; j<answerbits;j++){
- int flag2=1;
- if ((ANSWER[j]+ANSWER[answerbits-j-1]+c)>=10) {result[j] = (ANSWER[j]+ANSWER[answerbits-j-1]+c)%10; flag2=0;}
- else result[j]=ANSWER[j]+ANSWER[answerbits-j-1]+c;
- if (flag2==0) c=1;
- else c=0; }
- if (c==1) result[answerbits]=1;
- else {answerbits--; plus=1;}
- for(int i=answerbits;i>=0;i--)
- printf("%d",result[i] );
- // printf("%d",ANSWER[j]);}
- printf("\n%d\n",flag );
- for (int i=0; i<cnt; i++){
- printf("%d",M[i] );
- }
- int flag3=1;
- int sum=0;
- for (int i=0;i<answerbits+plus;i++){
- if (M[i]!=result[answerbits-i]) {flag3=0; sum = i; break;}
- }
- printf("\n");
- for(int i=0;i<answerbits+plus;i++)
- printf("%d",ANSWER[i] );
- if (flag3==1) printf("\n%s\n%d", "true",sum);
- else printf("\n%s\n%d", "false",sum);
- printf("\n");
- }
- else printf("%d",0);
- printf("\n");
- /*Closing file */
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement