在C中,我需要以正确的对齐方式以下面的方式打印数字模式,即:当双位数字出现时,上面的单个数字应该自己调整到右边等等。
14:27:10.488 [qtp878991463-70] ERROR WireMock - Request was not matched:
{
"url" : "/Login?username=admin&password=test&_spring_security_remember_me=true&submit=Login%20Name",
"absoluteUrl" : "http://localhost:8089/Login?username=admin&password=test&_spring_security_remember_me=true&submit=Login%20Name",
"method" : "POST",
"clientIp" : "127.0.0.1",
"headers" : {
"User-Agent" : "Java/1.8.0_101",
"Connection" : "keep-alive",
"Host" : "localhost:8089",
"Accept" : "text/plain, application/json, application/*+json, */*",
"Content-Length" : "0",
"Content-Type" : "application/x-www-form-urlencoded"
},
"cookies" : { },
"browserProxyRequest" : false,
"loggedDate" : 1516544830458,
"bodyAsBase64" : "",
"body" : "",
"loggedDateString" : "2018-01-21T14:27:10Z"
}
Closest match:
{
"url" : "http://localhost:8089/Login?username=admin&password=test&_spring_security_remember_me=true&submit=Login%20Name",
"method" : "POST",
"cookies" : {
"COOKIE" : {
"contains" :
"FNROTlvZz09Onp0RmwwRnFqbHd2RXNFOFZiTWNaa"
}
}
}
必需的输出:
int main() {
long int k = 1;
int i, j, n;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%ld ", k);
k++;
}
printf("\n");
}
return 0;
}
输出我得到:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55 `
需要输出:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
输出我得到:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
答案 0 :(得分:1)
您应该计算最后输出的数字的长度,并将该值用作printf调用中的字段宽度。
你在这里。
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
unsigned int upper = n * ( n + 1 ) / 2;
int size = 0;
do { ++size; } while ( upper /= 10 );
putchar( '\n' );
for ( unsigned int i = 0, value = 0; i < n; i++ )
{
do
{
printf( "%*u ", size, ++value );
} while ( value != ( i + 1 ) * ( i + 2 ) / 2 );
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
程序输出
Enter a non-negative number (0 - exit): 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
Enter a non-negative number (0 - exit): 20
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
Enter a non-negative number (0 - exit): 0
答案 1 :(得分:0)
我不会帮你编写代码,因为显示了零努力,但我可以通过伪代码给出提示。
cin >> n; //desired number of rows
for (int i = 0; i < n; i++){
//generate triangle numbers
}//store the first n triangle numbers, given by n(n-1)/2
for (int i = 0; i < n; i++){
cout << i;
if (i==any triangle number){
cout << "\n"; //note that an endline is printed only at triangle numbers
}
}
答案 2 :(得分:-1)
这是一个简单的解决方案,也可以正确处理行尾:
#include <stdio.h>
int main(void) {
int i, j, k, n, len;
if (scanf("%d", &n) == 1 && n > 0) {
/* compute the width of the last number */
//len = snprintf(NULL, 0, "%d", n * (n + 1) / 2);
//char buf[48]; /* large enough for 128-bit integers */
//len = sprintf(buf, "%d", n * (n + 1) / 2);
for (len = 1, k = n * (n + 1) / 2; k > 9; k /= 10)
len++;
for (i = k = 1; i <= n; i++) {
for (j = 1; j < i; j++) {
printf("%*d ", len, k++);
}
/* print the last number on the line without a trailing space */
printf("%*d\n", len, k++);
}
}
return 0;
}