Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstring>
- using namespace std;
- ifstream cin("text.in");
- ofstream cout("text.out");
- char para[1010];
- char *pCuv[1001];
- int main()
- {
- int maxRand;
- cin >> maxRand;
- cin.get();
- int nrPara = 0;
- while(cin.getline(para,1005))
- {
- nrPara++;
- if(strcmp(para,"") == 0)
- {
- cout << '\n';
- continue;
- }
- int len = strlen(para);
- char *p = strtok(para," ");
- int nrCuv = 0;
- while(p != NULL)
- {
- pCuv[++nrCuv] = p;
- p = strtok(NULL," ");
- }
- if(nrCuv == 1)
- {
- cout << pCuv[nrCuv] << '\n';
- for(int i = 0;i<=len;i++)
- para[i] = 0;
- continue;
- }
- // Sa prelucram cuvintele si sa aflam nr de spatii lol
- int pozC = 1, cLen = 1, sum = strlen(pCuv[1]);
- while(pozC < nrCuv)
- {
- while(pozC < nrCuv && sum + strlen(pCuv[pozC+1]) + 1 <= maxRand)
- cLen++,sum+=strlen(pCuv[pozC+1]) + 1, pozC++;
- int gaps = cLen - 1;
- int totalSpaces = maxRand - sum + gaps;
- int normalSpaces = totalSpaces / gaps;
- int cntExtra = totalSpaces % gaps;
- int cnt = 1;
- if(pozC == nrCuv)
- {
- for(int i = pozC-cLen+1;i<=pozC;i++)
- {
- cout << pCuv[i];
- if(i != pozC) cout << ' ';
- }
- cout << '\n';
- break;
- }
- for(int it = pozC - cLen + 1;it <= pozC;it++)
- {
- cout << pCuv[it];
- if(it != pozC)
- {
- for(int j = 1;j<=normalSpaces;j++)
- cout << ' ';
- if(cnt <= cntExtra) cout << ' ' , cnt++;
- }
- else cout << ' ';
- }
- cout << '\n';
- pozC++;
- if(pozC!=nrCuv)
- cLen = 1, sum = strlen(pCuv[pozC]);
- else cout << pCuv[nrCuv] << '\n';
- }
- for(int i = 0;i<=len;i++)
- para[i] = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment