Lisas Workbook Hackerrank
Lisa just got a new math workbook. A workbook contains exercise problems, grouped into chapters.
There are chapters in Lisa’s workbook, numbered from 1 to n.
The i-th chapter has ti problems, numbered from 1 to ti.
Each page can hold up to k problems. There are no empty pages or unnecessary spaces, so only the last page of a chapter may contain fewer than k problems.
Each new chapter starts on a new page, so a page will never contain problems from more than one chapter.
The page number indexing starts at 1.
Lisa believes a problem to be special if its index (within a chapter) is the same as the page number where it’s located. Given the details for Lisa’s workbook, can you count its number of special problems?
Note: See the diagram in the Explanation section for more details.
Input Format
The first line contains two integers n and k— the number of chapters and the maximum number of problems per page respectively.
The second line contains n integers t1, t2,…, tn, where ti denotes the number of problems in the i-th chapter.
Constraints
1<=n, k, ti<=100
Output Format
Print the number of special problems in Lisa's workbook.
Sample Input
5 3
4 2 6 1 10
Sample Output
4
Explanation
The diagram below depicts Lisa's workbook with 5 chapters and a maximum of 3 problems per page. Special problems are outlined in red, and page numbers are in yellow squares.
There are 4 special problems and thus we print the number 4 on a new line.
Solution
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N,K,t; cin>>N>>K; int p[N]; for(int i=0;i<N;i++){ cin>>t; p[i]=t; } int count=0,pi=0,pf=0,page=0,temp; //pi,pf are intial problem ,final problem for a page for(int i=0;i<N;i++){ temp=0; while(p[i]>0){ page++; if(p[i]/K){ pf=K*(temp+1); }else{ pf=p[i]+K*(temp); } pi=temp*K+1; if(page>=pi && page<=pf){ count++; } p[i]=p[i]-K; temp++; } } cout<<count; /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
II C++
#include<bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d%d", &n, &k); int answer = 0; int page = 1; for(int chapter = 1; chapter <= n; ++chapter) { int problems; scanf("%d", &problems); for(int index = 1; index <= problems; ++index) { if(index == page) answer++; if(index == problems || index % k == 0) ++page; } } printf("%d\n", answer); return 0; }
III C++
#include <bits/stdc++.h> using namespace std; int n, k; int tab[107]; int res; int a = 1, b; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &tab[i]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= tab[i]; j++) { if (j == a) { res++; } b++; if (j == tab[i] || b == k) { b = 0; a++; } } } printf("%d\n", res); return 0; }
# Enter your code here. Read input from STDIN. Print output to STDOUT
n, k = map(int, raw_input().split())
t = map(int, raw_input().split())
page, result = 0, 0
for i in xrange(n):
ques = 0
page += 1
for j in xrange(t[i]):
if ques == k:
ques = 0
page += 1
ques += 1
if j+1 == page:
result += 1
print result