Advertisement
Guest User

Untitled

a guest
May 4th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4.  
  5. #define MAX_DIGITS_INPUT 100001
  6.  
  7. extern int errno;
  8.  
  9. /*
  10. argc - counter of input arguments
  11. argv - pointer to input arguments
  12. argv[0] = revsum
  13. argv[1] = <input_file>
  14. */
  15. int weightCount(int M[MAX_DIGITS_INPUT],int cnt,int M_weights[MAX_DIGITS_INPUT],int forstart){
  16. if (cnt % 2 == 1 ) {
  17. if(M[cnt/2]%2==0) M_weights[cnt/2]=0;
  18. else M_weights[cnt/2]=1;}
  19. for(int i=forstart; i<cnt/2;i++){
  20. M_weights[cnt-i-1]=M_weights[i-2];
  21. if (M[i]-M[cnt-1-i]==1 || M[i]-M[cnt-1-i]==-9){
  22. 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;}
  23. else {return 0;}}
  24. else if (M[i]-M[cnt-1-i]==-1 || M[i]-M[cnt-1-i]==9){
  25. 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;}
  26. else {return 0;}}
  27. else if (M[i]==M[cnt-1-i]){
  28. // M_weights[cnt-1-i]=M_weights[i-2];
  29. M_weights[i]=M_weights[cnt-1-i];
  30. 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;}}
  31.  
  32. 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;}
  33. }
  34. else return 0;
  35. }
  36. return 1;
  37. }
  38.  
  39. int main (int argc, char **argv) {
  40. int M[MAX_DIGITS_INPUT];
  41. int M_weights[MAX_DIGITS_INPUT];
  42. int cnt = 0; //isWanted = 1;
  43. FILE *fp;
  44.  
  45. /*Opening file while checking for errors*/
  46. fp = fopen(argv[1], "r");
  47. if (fp == NULL) {
  48. fprintf(stderr, "Value of errno: %d\n", errno);
  49. exit(1);
  50. }
  51. /*Read one digit at a time and store it in int array M*/
  52. while(!feof(fp)){
  53. if (fscanf(fp, "%1d", &M[cnt]) == 1){
  54. cnt++;
  55. }
  56. }
  57. int answerbits=cnt;
  58. M_weights[cnt-1]=0;
  59. int flag=1;
  60. if (M[0]!=1) {
  61.  
  62. M_weights[cnt-2]=0;
  63. M_weights[cnt-1]=0;
  64. if (M[0]-M[cnt-1]==1)M_weights[0]=1;
  65. else if (M[0]==M[cnt-1])M_weights[0]=0;
  66. else flag=0;
  67. if (M[1]-M[cnt-2]==1 || M[1]-M[cnt-2]==-9)M_weights[1]=1;
  68. else if (M[1]==M[cnt-2]) M_weights[1]=0;
  69. else flag=0;
  70. if (flag==1){
  71. int forstart=2;
  72. flag = weightCount(M,cnt,M_weights,forstart);}
  73. // printf("%s\n","hi" );
  74. //printf("%d\n", flag);
  75. }
  76. else{
  77. // printf("%s\n", "hi" );
  78. M_weights[cnt-2]=0;
  79. M_weights[cnt-1]=0;
  80. if (M[0]-M[cnt-1]==1)M_weights[0]=1;
  81. else if (M[0]==M[cnt-1])M_weights[0]=0;
  82. else flag=0;
  83. if (M[1]-M[cnt-2]==1)M_weights[1]=1;
  84. else if (M[1]==M[cnt-2]) M_weights[1]=0;
  85. else flag=0;
  86. if (flag==1)
  87. {
  88. int forstart=2;
  89. flag=weightCount(M,cnt,M_weights,forstart);
  90. }
  91.  
  92. if (flag==0){
  93. printf("%s\n","hi" );
  94. flag=1;
  95. cnt++;
  96. M_weights[cnt-2]=0;
  97. if (M[1]!=0) M_weights[cnt-3]=1;
  98. else M_weights[cnt-3]=0;
  99. M_weights[0]=1;
  100. if (M[1]-M[cnt-2]==1 || M[1]-M[cnt-2]==-9) M_weights[1]=1;
  101. else if (M[1]==M[cnt-2]) M_weights[1]=0;
  102. else flag=0;
  103. if (M_weights[cnt-3]==1)
  104. {
  105. if (M[2]==M[cnt-3])M_weights[2]=1;
  106. else M_weights[2]=0;
  107. }
  108. else {
  109. if (M[2]==M[cnt-3])M_weights[2]=0;
  110. else M_weights[2]=1;
  111.  
  112. }
  113.  
  114. int forstart=2;
  115. flag=weightCount(M,cnt,M_weights,forstart);
  116. cnt--;
  117. answerbits--;
  118. }
  119. }
  120. int ANSWER[MAX_DIGITS_INPUT];
  121. if (answerbits%2==1) ANSWER[answerbits/2]= (10*M_weights[cnt/2-1]+M[cnt/2])/2;
  122. for (int j=0;j<answerbits/2;j++){
  123. ANSWER[answerbits-1-j]=((M[cnt-1-j]-M_weights[cnt-1-j])+10*M_weights[cnt-2-j])/2;
  124. ANSWER[j]=((M[cnt-1-j]-M_weights[cnt-1-j])+10*M_weights[cnt-2-j])-ANSWER[answerbits-1-j];
  125. }
  126. for(int i=0;i<cnt;i++)
  127. printf("%d",M_weights[i] );
  128. printf("\n");
  129.  
  130. if (flag==1) {
  131. int c=0;
  132. int plus=0;
  133. int result[MAX_DIGITS_INPUT];
  134. for(int j=0; j<answerbits;j++){
  135. int flag2=1;
  136. if ((ANSWER[j]+ANSWER[answerbits-j-1]+c)>=10) {result[j] = (ANSWER[j]+ANSWER[answerbits-j-1]+c)%10; flag2=0;}
  137. else result[j]=ANSWER[j]+ANSWER[answerbits-j-1]+c;
  138. if (flag2==0) c=1;
  139. else c=0; }
  140. if (c==1) result[answerbits]=1;
  141. else {answerbits--; plus=1;}
  142. for(int i=answerbits;i>=0;i--)
  143. printf("%d",result[i] );
  144.  
  145. // printf("%d",ANSWER[j]);}
  146. printf("\n%d\n",flag );
  147. for (int i=0; i<cnt; i++){
  148. printf("%d",M[i] );
  149. }
  150.  
  151. int flag3=1;
  152. int sum=0;
  153. for (int i=0;i<answerbits+plus;i++){
  154. if (M[i]!=result[answerbits-i]) {flag3=0; sum = i; break;}
  155. }
  156. printf("\n");
  157. for(int i=0;i<answerbits+plus;i++)
  158. printf("%d",ANSWER[i] );
  159.  
  160. if (flag3==1) printf("\n%s\n%d", "true",sum);
  161. else printf("\n%s\n%d", "false",sum);
  162. printf("\n");
  163.  
  164. }
  165. else printf("%d",0);
  166. printf("\n");
  167. /*Closing file */
  168. fclose(fp);
  169.  
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement