被输入中连续空格给坑到了。。。
大家注意了,什么叫连续空格, 叫做 空格空格asduhasdjnsa空格空格回车, 惊了把!
标准操作送答案?那我也送个C的答案
#include <stdio.h> #include <string.h> typedef struct {char word[40]; int length;} Unit; int main() { int T, I; scanf("%d",&T); for(I=0; I<T; I++) { int m, l, i, cnt=0, j=0, check=0; char sor[2018]; Unit unit[1000]; scanf("%d",&m); getchar(); gets(sor); l=strlen(sor); for(i=0; i<l; i++) { if(sor[i]==' ') { if(sor[i+1]==' ') continue; else if(cnt==0&&check==0) {check=1;continue;} unit[cnt].word[j]='\0'; unit[cnt].length=strlen(unit[cnt].word); cnt++; j=0; } else { unit[cnt].word[j++]=sor[i]; check=1; } } unit[cnt].word[j]='\0'; unit[cnt].length=strlen(unit[cnt].word); cnt++; int max=cnt, k, x, y, z; printf("case #%d:\n",I); for(i=0, j=0, l=0, cnt=0, z=0; i<max; i++) { l+=(unit[i].length+1); cnt++; if(l>m+1) { cnt-=2; l-=(unit[i--].length+2); l-=cnt; j=(m-l)/cnt; k=(m-l)%cnt; for(x=cnt; x>=0; x--) { printf("%s",unit[z++].word); if(x<=k&&x) for(y=0; y<j+1; y++) printf(" "); else for(y=0; y<j; y++) printf(" "); } printf("\n"); cnt=0; l=0; } } for(x=max-z; x>0; x--) printf("%s ",unit[z++].word); printf("\n"); } return 0; }
woshihaoaren,wolaisongdaanle
int main() { int t,i,j,str_len,line_len,m,words_len,words_count,space,start,l,p; char s[2001]; scanf(“%d”,&t); for(m=0;m<t;m++) { memset(s,0,sizeof(s)); scanf(“%d”,&line_len); getchar(); gets(s); printf(“case #%d:\n”,m); str_len=strlen(s); i=0; for(i=0;s[i]==’ ‘;i++); while(i<str_len) { start=i; words_len=0; words_count=0; for(;words_len+words_count<=line_len&&i<str_len;) { for(l=0,p=i;s[p]!=’ ‘&&p<str_len;p++,l++); if(words_len+words_count+l<=line_len) { words_len+=l; words_count++; for(i=p;s[i]==’ ‘;i++); } else break; } space=line_len-words_len; j=start; words_count–; while(j<i) { while(s[j]!=’ ‘&&j<str_len) putchar(s[j++]); while(s[j]==’ ‘&&j<str_len) j++; if(i==str_len&&words_count) putchar(‘ ‘); else { for(p=0;words_count&&p<space/words_count;p++) putchar(‘ ‘); space-=p; } words_count–; } putchar(‘\n’); } } return 0; }
真丶码农题(我来送答案了)
using namespace std; int T; string line,tmp; vector V; int cal_space(int tot,int now,int len) { int base=len/(tot-1); len%(tot-1); if(tot-now+1<=(len%(tot-1))) return base+1; else return base; } void solve() { int M;cin>>M; getline(cin,line); getline(cin,line);
V.clear(); tmp=”“; int ll=line.length(); for(int i=0;i<ll;i++) if(line[i]==’ ‘) { if(tmp!=”“) {V.push_back(tmp);tmp=”“;} } else tmp+=line[i]; if(tmp!=”“) V.push_back(tmp);
int pos=0,printed=0; while(printed<=V.size()-1) { int first=printed; int len=V[printed].length(); int wordlen=len; printed++; while(printed<=V.size()-1 && len+1+V[printed].length()<=M) { len+=1+V[printed].length(); wordlen+=V[printed].length(); printed++; } if(printed==V.size()) { int ff=1; for(int i=first;i<printed;i++) { if(ff) ff=0; else cout<<” “; cout<<V[i]; } } else { cout<<V[first]; int now=2; for(int i=first+1;i<printed;i++) { for(int j=1;j<=cal_space(printed-first,now,M-wordlen);j++) cout<<” “; now++; cout<<V[i]; } }
cout<<endl;
} return; }
int main() { scanf(“%d”,&T); getline(cin,line); for(int step=0;step<T;step++) { printf(“case #%d:\n”,step); solve(); } return 0; }
using namespace std;
int cut(const string& s,string *word){ int cnt=0; int i=s.find_first_not_of(” “); int j=s.find(” “,i); for(;;){ if(j==string::npos)break; // cout<<”i=”<<i<<” j=”<<j<<endl; word[cnt++].assign(s,i,j-i); i=s.find_first_not_of(” “,j); if(i==string::npos){ return cnt; } j=s.find(” “,i); } word[cnt++].assign(s,i,s.length()-i); // cout<<”word=”<<word[cnt-1]<<endl; return cnt; }
void Insert(string &line,int m,string blank){ int i=line.find_last_of(‘ ‘,line.length()-1);
int npos=string::npos; while(i!=npos&&line.length()<m){ line.insert(i,blank); i=line.find_last_not_of(' ',i); i=line.find_last_of(' ',i); }
}
void InsertBlank(string &line,int m,int blank_cnt){ int len=line.length(); int left=m-len; if(blank_cnt==0){ for(;;){ line+=” “; if(line.length()==m){ return ; } } } int n=left/blank_cnt; string blank(n,’ ‘); Insert(line,m,blank); if(line.length()<m){ blank=” “; Insert(line,m,blank); } } void printAll(string *line,int line_cnt){ for(int i=0;i<line_cnt;i++){ cout<<line[i]<<endl; } }
void put(string word,int m,int cnt){ int blank_cnt=0; int left,line_cnt; string line; line=new string[cnt]; line_cnt=0; left=m; for(int i=0;i<cnt;i++){ if(left<word[i].length()+1){ InsertBlank(line[line_cnt],m,blank_cnt); blank_cnt=0; left=m; line_cnt++; } int ret=0; if(left!=m) { line[line_cnt] += ” “; blank_cnt++; ret = 1; } line[line_cnt]+=word[i]; left-=word[i].length()+ret; } line_cnt++; printAll(line,line_cnt); }
void solve(){ int m; cin>>m; // cout<<”m=”<<m<>t; for(int i=0;i<t;i++){ printf(“case #%d:\n”,i); solve(); }
return 0;
被输入中连续空格给坑到了。。。
大家注意了,什么叫连续空格,
叫做
空格空格asduhasdjnsa空格空格回车,
惊了把!
标准操作送答案?那我也送个C的答案
woshihaoaren,wolaisongdaanle
include
include
include
int main()
{
int t,i,j,str_len,line_len,m,words_len,words_count,space,start,l,p;
char s[2001];
scanf(“%d”,&t);
for(m=0;m<t;m++)
{
memset(s,0,sizeof(s));
scanf(“%d”,&line_len);
getchar();
gets(s);
printf(“case #%d:\n”,m);
str_len=strlen(s);
i=0;
for(i=0;s[i]==’ ‘;i++);
while(i<str_len)
{
start=i;
words_len=0;
words_count=0;
for(;words_len+words_count<=line_len&&i<str_len;)
{
for(l=0,p=i;s[p]!=’ ‘&&p<str_len;p++,l++);
if(words_len+words_count+l<=line_len)
{
words_len+=l;
words_count++;
for(i=p;s[i]==’ ‘;i++);
}
else break;
}
space=line_len-words_len;
j=start;
words_count–;
while(j<i)
{
while(s[j]!=’ ‘&&j<str_len)
putchar(s[j++]);
while(s[j]==’ ‘&&j<str_len)
j++;
if(i==str_len&&words_count)
putchar(‘ ‘);
else
{
for(p=0;words_count&&p<space/words_count;p++)
putchar(‘ ‘);
space-=p;
}
words_count–;
}
putchar(‘\n’);
}
}
return 0;
}
真丶码农题(我来送答案了)
include
using namespace std;
int T;
string line,tmp;
vector V;
int cal_space(int tot,int now,int len)
{
int base=len/(tot-1);
len%(tot-1);
if(tot-now+1<=(len%(tot-1))) return base+1;
else return base;
}
void solve()
{
int M;cin>>M;
getline(cin,line); getline(cin,line);
V.clear();
tmp=”“;
int ll=line.length();
for(int i=0;i<ll;i++)
if(line[i]==’ ‘)
{
if(tmp!=”“) {V.push_back(tmp);tmp=”“;}
}
else tmp+=line[i];
if(tmp!=”“) V.push_back(tmp);
int pos=0,printed=0;
while(printed<=V.size()-1)
{
int first=printed;
int len=V[printed].length();
int wordlen=len;
printed++;
while(printed<=V.size()-1 && len+1+V[printed].length()<=M)
{
len+=1+V[printed].length();
wordlen+=V[printed].length();
printed++;
}
if(printed==V.size())
{
int ff=1;
for(int i=first;i<printed;i++)
{
if(ff) ff=0; else cout<<” “;
cout<<V[i];
}
}
else
{
cout<<V[first];
int now=2;
for(int i=first+1;i<printed;i++)
{
for(int j=1;j<=cal_space(printed-first,now,M-wordlen);j++) cout<<” “;
now++;
cout<<V[i];
}
}
}
return;
}
int main()
{
scanf(“%d”,&T);
getline(cin,line);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}
include
include
include
include
include
include
using namespace std;
int cut(const string& s,string *word){
int cnt=0;
int i=s.find_first_not_of(” “);
int j=s.find(” “,i);
for(;;){
if(j==string::npos)break;
// cout<<”i=”<<i<<” j=”<<j<<endl;
word[cnt++].assign(s,i,j-i);
i=s.find_first_not_of(” “,j);
if(i==string::npos){
return cnt;
}
j=s.find(” “,i);
}
word[cnt++].assign(s,i,s.length()-i);
// cout<<”word=”<<word[cnt-1]<<endl;
return cnt;
}
void Insert(string &line,int m,string blank){
int i=line.find_last_of(‘ ‘,line.length()-1);
}
void InsertBlank(string &line,int m,int blank_cnt){
int len=line.length();
int left=m-len;
if(blank_cnt==0){
for(;;){
line+=” “;
if(line.length()==m){
return ;
}
}
}
int n=left/blank_cnt;
string blank(n,’ ‘);
Insert(line,m,blank);
if(line.length()<m){
blank=” “;
Insert(line,m,blank);
}
}
void printAll(string *line,int line_cnt){
for(int i=0;i<line_cnt;i++){
cout<<line[i]<<endl;
}
}
void put(string word,int m,int cnt){
int blank_cnt=0;
int left,line_cnt;
string line;
line=new string[cnt];
line_cnt=0;
left=m;
for(int i=0;i<cnt;i++){
if(left<word[i].length()+1){
InsertBlank(line[line_cnt],m,blank_cnt);
blank_cnt=0;
left=m;
line_cnt++;
}
int ret=0;
if(left!=m) {
line[line_cnt] += ” “;
blank_cnt++;
ret = 1;
}
line[line_cnt]+=word[i];
left-=word[i].length()+ret;
}
line_cnt++;
printAll(line,line_cnt);
}
void solve(){
int m;
cin>>m;
// cout<<”m=”<<m<>t;
for(int i=0;i<t;i++){
printf(“case #%d:\n”,i);
solve();
}
}