Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.  
  6. int total,min,max,nilai,count,kasus;
  7.  
  8.  
  9. FILE *fp=fopen("random.txt","r");
  10.  
  11. fscanf(fp,"%d\n",&kasus);
  12. for(int x=0;x<kasus;x++)
  13. {
  14. fscanf(fp,"%d %d %d\n",&total,&min,&max);
  15. char string[total];
  16. count=0;
  17. nilai=1;
  18. fscanf(fp,"%s\n",string);
  19. for(int j=0;j<strlen(string);j++)
  20. {
  21.  
  22. if(string[j]=='1'&&string[j+1]=='1')
  23. {
  24. nilai++;
  25.  
  26. if(nilai>max)
  27. {
  28. nilai=1;
  29.  
  30. }
  31. else if(nilai>=min && nilai<=max)
  32. {
  33. if(string[j+1]=='1'&&string[j+2]=='0')
  34. {
  35. count++;
  36. nilai=1;
  37. }
  38. else if(string[j+1]=='1'&&string[j+2]=='\0')
  39. {
  40. count++;
  41. nilai=1;
  42. }
  43.  
  44. }
  45.  
  46. }
  47. else if(string[j]=='1'&&string[j+1]=='0')
  48. {
  49. if(nilai>max)
  50. {
  51. nilai=1;
  52.  
  53. }
  54. else if(nilai>=min && nilai<=max)
  55. {
  56. if(string[j+1]=='0' && string[j-1]=='\0')
  57. {
  58. count++;
  59. nilai=1;
  60. }
  61. else if(string[j+1]=='0' && string[j-1]=='0')
  62. {
  63. count++;
  64. nilai=1;
  65. }
  66.  
  67.  
  68. }
  69.  
  70. }
  71. else if(string[j]=='1' && string[j+1]=='\0')
  72. {
  73. if(nilai>max)
  74. {
  75. nilai=1;
  76.  
  77. }
  78. else if(nilai>=min && nilai<=max)
  79. {
  80.  
  81. count++;
  82. nilai=1;
  83.  
  84.  
  85. }
  86. }
  87. else
  88. nilai=1;
  89.  
  90.  
  91. }
  92. printf("Case #%d: %d\n",x+1,count);
  93.  
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement