如何删除 C 程序中输出的第一个 *?
How to remove the first * of my output in a C program?
我正在尝试编写一个程序来打印给定数字的质因数,但我需要将它们以 *
分隔打印,例如:对于输入 100,输出将是:2 *2*5*5,有什么建议吗?到目前为止我这样做了:
# include <stdio.h>
# include <math.h>
void decomposicao(int n)
{
printf("%d = ", n);
while (n%2 == 0)
{
n = n/2;
printf("* %d ", 2);
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
printf("* %d ", i);
}
}
if (n > 2)
printf ("* %d ", n);
}
int main()
{
int n;
scanf("%d", &n);
if (n<=0||n==1)
{
printf("Error", n);
}
else
{
decomposicao(n);
}
return 0;
}
对于此代码,输出为:
100 = *2*2*5*5
添加一个布尔变量,告诉您是否打印第一个因素。
#include <stdbool.h>
void decomposicao(int n)
{
bool first = true;
printf("%d = ", n);
while (n%2 == 0)
{
n = n/2;
if (!first)
printf("* ");
printf("%d ", 2);
first = false;
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
if (!first)
printf("* ");
printf("%d ", i);
first = false;
}
}
if (n > 2) {
if (!first)
printf("* ");
printf ("%d ", n);
}
}
首先,您可以更改 * 的打印方式。而不是在你的字符串的乞求,只是把它放在最后。
现在,您可以写入内部缓冲区,而不是直接将结果写入输出流,完成后,只需忽略其中的最后两个字符即可。
void decomposicao(int n)
{
const size_t len = 512;
char buffer[len];
size_t written = 0;
written = snprintf(buffer, len, "%d = ", n);
while (n%2 == 0)
{
n = n/2;
written += snprintf(buffer + written, len - written, "%d * ", 2);
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
written += snprintf(buffer + written, len - written, "%d * ", i);
}
}
if (n > 2)
written += snprintf(buffer + written, len - written, "%d * ", n);
buffer[len - 1] = '[=10=]';
buffer[written - 2] = '[=10=]';
printf("%s\n", buffer);
}
我正在尝试编写一个程序来打印给定数字的质因数,但我需要将它们以 *
分隔打印,例如:对于输入 100,输出将是:2 *2*5*5,有什么建议吗?到目前为止我这样做了:
# include <stdio.h>
# include <math.h>
void decomposicao(int n)
{
printf("%d = ", n);
while (n%2 == 0)
{
n = n/2;
printf("* %d ", 2);
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
printf("* %d ", i);
}
}
if (n > 2)
printf ("* %d ", n);
}
int main()
{
int n;
scanf("%d", &n);
if (n<=0||n==1)
{
printf("Error", n);
}
else
{
decomposicao(n);
}
return 0;
}
对于此代码,输出为:
100 = *2*2*5*5
添加一个布尔变量,告诉您是否打印第一个因素。
#include <stdbool.h>
void decomposicao(int n)
{
bool first = true;
printf("%d = ", n);
while (n%2 == 0)
{
n = n/2;
if (!first)
printf("* ");
printf("%d ", 2);
first = false;
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
if (!first)
printf("* ");
printf("%d ", i);
first = false;
}
}
if (n > 2) {
if (!first)
printf("* ");
printf ("%d ", n);
}
}
首先,您可以更改 * 的打印方式。而不是在你的字符串的乞求,只是把它放在最后。
现在,您可以写入内部缓冲区,而不是直接将结果写入输出流,完成后,只需忽略其中的最后两个字符即可。
void decomposicao(int n)
{
const size_t len = 512;
char buffer[len];
size_t written = 0;
written = snprintf(buffer, len, "%d = ", n);
while (n%2 == 0)
{
n = n/2;
written += snprintf(buffer + written, len - written, "%d * ", 2);
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
written += snprintf(buffer + written, len - written, "%d * ", i);
}
}
if (n > 2)
written += snprintf(buffer + written, len - written, "%d * ", n);
buffer[len - 1] = '[=10=]';
buffer[written - 2] = '[=10=]';
printf("%s\n", buffer);
}