if(M0>4000&&r==0) { r=1; if(r==1) { flag=!flag; } }
时间: 2024-04-20 21:25:46 浏览: 22
这段代码是一个简单的条件判断语句。首先,它判断变量 M0 是否大于 4000 并且变量 r 的值为 0。如果条件成立,那么将变量 r 的值设为 1。接着,在内部的条件语句中再次判断变量 r 的值是否为 1,如果是的话,将变量 flag 的值取反。
请注意,这段代码可能存在逻辑错误或者语法错误,因为它没有给出完整的上下文。如果需要更准确的帮助,请提供更多相关代码。
相关问题
#include <stdio.h> #include <stdlib.h> int findHead(FILE * ,int); int findLast(FILE * ,int); void deleteState(FILE **,int * ,int *); void deleteLine(FILE **,int ,int ); int main(){ FILE * fp = fopen("dict.dic","r"); int start,end; if(fp != NULL){ int a,b,c,d,e; fscanf(fp,"%d",&a); fscanf(fp,"%d",&b); fscanf(fp,"%d",&c); fscanf(fp,"%d",&d); fscanf(fp,"%d",&e); int mode; scanf("%d",&mode); switch (mode) { case 1: rewind(fp); start = findHead(fp,5); rewind(fp); end = findLast(fp,a); break; case 2: rewind(fp); start = findHead(fp,a); rewind(fp); end = findLast(fp,b); break; case 3: rewind(fp); start = findHead(fp,b); rewind(fp); end = findLast(fp,c); break; case 4: rewind(fp); start = findHead(fp,c); rewind(fp); end = findLast(fp,d); break; case 5: rewind(fp); start = findHead(fp,d); rewind(fp); end = findLast(fp,e); break; default: printf("??"); break; } rewind(fp); while(ftell(fp)<start) fgetc(fp); while(ftell(fp) != end){ int currentch = fgetc(fp); int flag = 1; if(currentch == '/' ){ int nextch = fgetc(fp); if(nextch == ''){ while(flag && ftell(fp) != end){ currentch = fgetc(fp); while(currentch == '' && ftell(fp) != end){ currentch = fgetc(fp); if(currentch == '/' && ftell(fp) != end) flag = 0; } } } else if(nextch == '/'){ while(currentch != '\n'){ currentch = fgetc(fp); } } else { printf("%c",currentch); currentch = nextch; } } if(ftell(fp) != end && flag) printf("%c",currentch); } } fclose(fp); return 0; } int findHead(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); } int findLast(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); }将子函数并到主函数里
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fp = fopen("dict.dic","r");
int start,end;
if(fp != NULL){
int a,b,c,d,e;
fscanf(fp,"%d",&a);
fscanf(fp,"%d",&b);
fscanf(fp,"%d",&c);
fscanf(fp,"%d",&d);
fscanf(fp,"%d",&e);
int mode;
scanf("%d",&mode);
switch (mode) {
case 1:
rewind(fp);
start = findHead(fp,5);
rewind(fp);
end = findLast(fp,a);
break;
case 2:
rewind(fp);
start = findHead(fp,a);
rewind(fp);
end = findLast(fp,b);
break;
case 3:
rewind(fp);
start = findHead(fp,b);
rewind(fp);
end = findLast(fp,c);
break;
case 4:
rewind(fp);
start = findHead(fp,c);
rewind(fp);
end = findLast(fp,d);
break;
case 5:
rewind(fp);
start = findHead(fp,d);
rewind(fp);
end = findLast(fp,e);
break;
default:
printf("??");
break;
}
rewind(fp);
while(ftell(fp)<start)
fgetc(fp);
while(ftell(fp) != end){
int currentch = fgetc(fp);
int flag = 1;
if(currentch == '/' ){
int nextch = fgetc(fp);
if(nextch == '\n'){
while(flag && ftell(fp) != end){
currentch = fgetc(fp);
while(currentch == ' ' && ftell(fp) != end){
currentch = fgetc(fp);
if(currentch == '/' && ftell(fp) != end)
flag = 0;
}
}
}
else if(nextch == '/'){
while(currentch != '\n'){
currentch = fgetc(fp);
}
}
else {
printf("%c",currentch);
currentch = nextch;
}
}
if(ftell(fp) != end && flag)
printf("%c",currentch);
}
}
fclose(fp);
return 0;
}
int findHead(FILE * fp,int line){
int ch = 0;
while(line-- && ch != EOF){
ch = fgetc(fp);
while(ch != '\n' && ch != EOF){
ch = fgetc(fp);
}
}
return ftell(fp);
}
int findLast(FILE * fp,int line){
int ch = 0;
while(line-- && ch != EOF){
ch = fgetc(fp);
while(ch != '\n' && ch != EOF){
ch = fgetc(fp);
}
}
return ftell(fp);
}
将如下代码转成C#语言代码import numpy as np def get_random(i, j=None): if j == None: # 返回0-i的随机整数 return np.random.randint(i + 1) if i > j: i, j = j, i # 获取i-j的随机整数 return np.random.randint(i, j + 1) def fast_power(base, power, n): result = 1 tmp = base while power > 0: if power & 1 == 1: result = (result * tmp) % n tmp = (tmp * tmp) % n power = power >> 1 return result def Miller_Rabin(n, s): # 2是素数 if n == 2: return True # n是偶数或小于2 if n & 1 == 0 or n < 2: return False # n-1 = (2^s) m m, p = n - 1, 0 while m & 1 == 0: m = m >> 1 p += 1 for _ in range(s): b = fast_power(get_random(2, n - 1), m, n) if b == 1 or b == n - 1: continue for __ in range(p - 1): b = fast_power(b, 2, n) if b == n - 1: break else: return False return True if name == 'main': num = 50000 s = 3 prime = [x for x in range(2, num) if not [y for y in range(2, int(np.sqrt(x) + 1)) if x % y == 0]] result = [] for i in range(num): flag = Miller_Rabin(i, s) if flag and i not in prime: print('error1: %d' % i) elif not flag and i in prime: print('error2: %d' % i)
以下是将代码转成C#语言代码的结果:
using System;
namespace MillerRabin
{
class Program
{
static void Main(string[] args)
{
int num = 50000;
int s = 3;
int[] prime = new int[num];
int k = 0;
for (int i = 2; i < num; i++)
{
bool isPrime = true;
for (int j = 2; j <= Math.Sqrt(i); j++)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
prime[k++] = i;
}
}
int[] result = new int[num];
for (int i = 0; i < num; i++)
{
bool flag = Miller_Rabin(i, s);
if (flag && !prime.Contains(i))
{
Console.WriteLine("error1: " + i);
}
else if (!flag && prime.Contains(i))
{
Console.WriteLine("error2: " + i);
}
}
}
static int get_random(int i, int? j = null)
{
if (j == null)
{
// 返回0-i的随机整数
Random random = new Random();
return random.Next(i + 1);
}
if (i > j)
{
int temp = i;
i = (int)j;
j = temp;
}
// 获取i-j的随机整数
Random rand = new Random();
return rand.Next(i, (int)j + 1);
}
static int fast_power(int baseNum, int power, int n)
{
int result = 1;
int tmp = baseNum;
while (power > 0)
{
if ((power & 1) == 1)
{
result = (result * tmp) % n;
}
tmp = (tmp * tmp) % n;
power = power >> 1;
}
return result;
}
static bool Miller_Rabin(int n, int s)
{
// 2是素数
if (n == 2) return true;
// n是偶数或小于2
if ((n & 1) == 0 || n < 2) return false;
// n-1 = (2^s) m
int m = n - 1, p = 0;
while ((m & 1) == 0)
{
m = m >> 1;
p++;
}
for (int i = 0; i < s; i++)
{
int b = fast_power(get_random(2, n - 1), m, n);
if (b == 1 || b == n - 1)
{
continue;
}
for (int j = 0; j < p - 1; j++)
{
b = fast_power(b, 2, n);
if (b == n - 1)
{
break;
}
}
if (b != n - 1)
{
return false;
}
}
return true;
}
}
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)