我希望这段代码能够打印一个数字因子,如果它不是素数,并且如果它是素数则识别数字.
#include <stdio.h>
main() {
int possible_prime, n, possible_divisor;
printf( "\tThis program lists all primes <= n\n\n" );
printf( "Input n: " );
scanf( "%d", &n );
printf( "\n\n\tPrimes <= %d: \n\n", n );
for ( possible_prime = 1; possible_prime <= n; possible_prime++ ) {
/* try to find a divisor of possible_prime */
for ( possible_divisor = 1; possible_divisor < possible_prime; possible_divisor++ ) {
if ( possible_prime % possible_divisor == 0 )
printf("\n\t%d", possible_prime);
}
/* found a divisor …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个列出给定数字因子的程序.它写的时候有用:
int main () {
int num, i;
printf("\nEnter a number: ");
scanf("%d", &num);
for(i == 0; i <= num; ++i) {
if (num % i == 0) {
printf("\n\t%d", i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当for循环是
for(i = 0; i <= num; ++i)
Run Code Online (Sandbox Code Playgroud)
我很困惑因为我认为这是for循环的格式.有人可以指出我的错误吗?
我只是希望它给我1到6之间的值,但它给了我这个:
P1d1 = 1445768086
P1d2 = -2
P2d1 = 1982468450
P2d2 = 198281572
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (){
srand(time(NULL));
/*Player 1*/
int P1d1 = 1 + rand() % 6; //Rolls Player 1's first die; random # between 1-6
int P1d2 = 1+ rand() % 6; //Rolls Player 1's second die; random # between 1-6
int P1total = P1d1 + P1d2; //Takes total of both rolls
/*Player 2*/
int P2d1 = 1 + rand() % 6; …Run Code Online (Sandbox Code Playgroud) 为什么我不能比较随机函数的结果?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main (){
srand(time(NULL));
/*Player 1*/
int P1d1, P1d2, P1total;
P1d1 = 1 + rand() % 6; //Rolls Player 1's first die; random # between 1-6
P1d2 = 1 + rand() % 6; //Rolls Player 1's second die; random # between 1-6
P1total = P1d1 + P1d2; //Takes total of both rolls
/*Player 2*/
int P2d1, P2d2, P2total;
P2d1 = 1 + rand() % 6; //Rolls Player 2's first die; random # between 1-6
P2d2 = …Run Code Online (Sandbox Code Playgroud)