C语言没法连续输入
C语言无法连续输入
题目:
Input a number N, and do the prime factorization(质因数分解).
The input contains several test cases. Each test case contains exact one integer N.
For each test case, you should output at least one line for the prime factorization of N. For example, if N = 144 = 24 * 32, then you should output two lines for this test case:
2 4
3 2
There should be an empty line between test cases.
Data Promise
For all test cases, 2 <= N <= 105
Sample Input
2
10
144
Sample Output
2 1
2 1
5 1
2 4
3 2
我的代码:
我试了一下 输入一个数字答案是对的,但是输入一个,回车,再输入一个,回车,程序就报错退出了,这是为什么呢?
------解决思路----------------------
------解决思路----------------------
test第二次的时候变成了0
你可以设个断点看。
题目:
Input a number N, and do the prime factorization(质因数分解).
The input contains several test cases. Each test case contains exact one integer N.
For each test case, you should output at least one line for the prime factorization of N. For example, if N = 144 = 24 * 32, then you should output two lines for this test case:
2 4
3 2
There should be an empty line between test cases.
Data Promise
For all test cases, 2 <= N <= 105
Sample Input
2
10
144
Sample Output
2 1
2 1
5 1
2 4
3 2
我的代码:
#include<stdio.h>
int main() {
int num, i, j, k = 0, t = 1, test, a[10000] = {0}, b[10000] = {0}, \
m, n, p;
while (scanf("%d", &num) != EOF) {
for (i = 2; i < num; i++) {
t = 1;
for (j = 2; j < i; j++) {
if (i % j == 0) {
t = 0;
break;
}
}
if (t == 1 && num % i == 0) {
a[k] = i;
k++;
}
}
for (m = 0; m < k; m++) {
n = 1;
test = a[m];
while (num % test == 0) {
test *= a[m];
n++;
}
b[m] = n-1;
}
a[k] = b[k] = 0;
k++;
}
for (j = 0; j < k-1; j++) {
if (a[j] == 0) printf("\n");
else printf("%d %d\n", a[j], b[j]);
}
return 0;
}
我试了一下 输入一个数字答案是对的,但是输入一个,回车,再输入一个,回车,程序就报错退出了,这是为什么呢?
------解决思路----------------------
while (num % test == 0)//有bug,除数为0就出错了
------解决思路----------------------
test第二次的时候变成了0
你可以设个断点看。