Home > Back-end >  C language subject
C language subject

Time:10-05

The first input two integers m, n, representing the number of rows and length
Request output, short length of a string into n long, more than enough to fill 0, newline continue output
Such as input:
2, 8
1234567812345678123
Asd

O
12345678
12345678
12300000


A little code can't write the
Int main ()
{
Int m;//store the input string of the total number of rows
Int n=8;//storage of fixed length
int len;
Int a rest;
Int row;
Int count;
Char STR [100].//store the input string
Char \ [100] [100].



For (int I=0; i <100; I++)
For (int j=0; J & lt; 100; J++)
{
\ [I] [j]='\ 0';
//STR [I] [j]='\ 0';
//puts (temp [I]);
}


Scanf_s (" % d ", & amp; M);
Printf_s (" please input string \ n ");
For (int I=0; i {
Char c;
int j=0;
Scanf_s (" % s ", STR);
/* while ((c=getchar ())! )
='\ n'{
STR [I] [j++]=c;
}
getchar(); */
}
//int index=0;
//for (int j=0; J & lt; m; J++)
//{
[j]//len=strlen (STR);
//row=len/n;
//if (row)//if the length more than fixed length
//{
////count=n - 1;
//rest=len % n;
//for (int k=0; K & lt; The row. K++)
//{
//for (int I=0; i //{
//strncpy_s (temp [index], 1, & amp; STR [k] [I], n);
//index++;
//I=I + n.
//}
//
//
//}
//for (int I=0; i //{
//puts (temp [I]);
//}
//
//}
//
//



//}

}

CodePudding user response:

Use first row=len/n; Calculate how many full line, then output the full line in turn, finally use the nLenLeft len=% n calculate how much is left characters, if not zero, the remaining characters copied to the output buffer, the length of the remaining with 0

CodePudding user response:

reference 1st floor dataxdata response:
use first row=len/n. Calculate how many full line, then output the full line in turn, finally use the nLenLeft len=% n calculate how much is left a character, if not zero, the remaining characters copied to the output buffer, the length of the remaining with zero line

I think so too but program writing is wrong

CodePudding user response:

This way is huawei machine questions this year? Try the original poster in preparation for huawei machine?

I compiled, posted everybody help search search Bug

/*
Problem description
First input two integers m, n, represent the number of rows and the length, the required output, short length of a string into n long, more than enough to fill 0, newline continue output
Such as input:
2, 8
1234567812345678123
Asd

O
12345678
12345678
12300000
Asd00000


Input (the set of input is Bug killer)
3, 3
1234567891
Abcdefgh
012034

O
123
456
789
100
ABC
Def
Gh0
012
034
*/

# include "stdio.h"
# include "string. H"
# include "malloc. H"
# include "stdlib. H"
# define M/100/default line length is not more than 100

Void main ()
{
Int m, n;
int i,j,k;
The scanf (" % d ", & amp; M);
The scanf (" % d ", & amp; N);
Char * p=(char *) malloc (sizeof (char) * M * M);//open m a storage unit of m lines are
for(i=0; iThe scanf (" % s ", p + I * M);//input m line by pointer string

//truncation output
for(i=0; i{
for(j=0; j{
If ((j + 1) % n!=0& & (* (p + I * M + j))!='\ 0')
Printf (" % c ", * (p + I * M + j));
The else
If ((j + 1) % n==0 & amp; & (* (p + I * M + j))!='\ 0')
{
Printf (" % c ", * (p + I * M + j));
printf("\n");
}
The else//met '\ 0'
If ((j % n))//j at this time is equal to the number of characters
{
For (k=j % n; Kprintf("0");
printf("\n");
break;//don't forget to break, because M may be greater than the length of the string
}
The else
break;//the string just enough truncated
}
}

Free (p);
P=NULL;
}

CodePudding user response:

 # include & lt; Stdio. H> 
#include
Int main ()
{
Char a []="012034";
Int m, n, I=0, j;
The scanf (" % d, % d ", & amp; M, & amp; N);
Char * p;
P=a;
While (* p!='\ 0')
{

If (I & lt; N)
{
Printf (" % c ", * p).
P++;
i++;
}
The else
{
Printf (" % c ", '\ n');
I=0;
}

}
If (I & lt; N)
{
for (j=0; j{
Putchar (' 0 ');
}
}
}

CodePudding user response:

The
reference 3 floor hnfeng2014 response:
this is huawei machine exam this year? Try the original poster in preparation for huawei machine?

I compiled, posted everybody help search search Bug

/*
Problem description
First input two integers m, n, represent the number of rows and the length, the required output, short length of a string into n long, more than enough to fill 0, newline continue output
Such as input:
2, 8
1234567812345678123
Asd

O
12345678
12345678
12300000
Asd00000


Input (the set of input is Bug killer)
3, 3
1234567891
Abcdefgh
012034

O
123
456
789
100
ABC
Def
Gh0
012
034
*/

# include "stdio.h"
# include "string. H"
# include "malloc. H"
# include "stdlib. H"
# define M/100/default line length is not more than 100

Void main ()
{
Int m, n;
int i,j,k;
The scanf (" % d ", & amp; M);
The scanf (" % d ", & amp; N);
Char * p=(char *) malloc (sizeof (char) * M * M);//open m a storage unit of m lines are
for(i=0; iThe scanf (" % s ", p + I * M);//input m line by pointer string

//truncation output
for(i=0; i{
for(j=0; j{
If ((j + 1) % n!=0& & (* (p + I * M + j))!='\ 0')
Printf (" % c ", * (p + I * M + j)); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related