我必须使用一些现有的(很旧的)C库的几乎1:1的Python翻译,然后发现了问题。
原始代码为:
int i, n;
//...
for (i = 0; i < n; i++)
if (someCondition(i))
doSomething();
break;
if (i == n)
doSomeOtherStuff();
Run Code Online (Sandbox Code Playgroud)
它被翻译成
for i in range(n):
if someCondition(i):
doSomething()
break
if i == n:
doSomeOtherStuff()
Run Code Online (Sandbox Code Playgroud)
问题是如果永不为真,i则等于n - 1循环后someCondtion(i)。
我的解决方案是
found = False
for i in range(n):
if someCondition(i):
doSomething()
found = True
break
if not found:
doSomeOtherStuff()
Run Code Online (Sandbox Code Playgroud)
有更好的解决方案吗?我希望代码更改最少的解决方案仍然能够比较C和Python实现。Python代码仅用于测试,C实现是生产代码。因此,没有性能要求,只有可读性。
学习编码,并尝试一个简单的while循环数字猜谜游戏.这个想法是,如果错误猜测的数量达到5,就会导致循环变为"假"并结束.相反,它将循环,直到它被测试站点作为无限循环结束."破解"版本的工作正常.我的问题是为什么if"break"工作,但是|| 设置值= false不起作用?
没有工作的代码片段
while (secret != guess || wrong < 5)
{
if (guess < secret)
{
printf("You guessed too low\n");
wrong++;
}
else
{
printf("you guessed too high\n");
wrong++;
}
printf("Input another guess\n");
scanf ("%d", &guess);
}
Run Code Online (Sandbox Code Playgroud)
工作代码片段
while (secret != guess)
{
if (guess < secret)
{
printf("You guessed too low\n");
}
else
{
printf("you guessed too high\n");
}
printf("Input another guess\n");
scanf("%d", &guess);
wrong = wrong + 1;
if (wrong >= 5)
{
break;
}
}
Run Code Online (Sandbox Code Playgroud) 所以我正在编写一个程序,它采用三种颜色的电阻带的字符串输入并计算它的电阻.
我已经接受了输入,这个函数使用strcmp(或strcasecmp)来比较字符串和颜色列表.但是,我收到编译器错误,说"语句不可分配",直接引用if/elseif语句.
我有一个数组中的列表,但出于此目的,我只使用了一个字符串.这是我收到错误的函数.我是否可能需要添加for或while循环?
char calculate_first_resistance(char color1[8])
{
g = strlen(color1)-1;
if( color1[ g ] == '\n')
color1[g] = '\0';
if (strcmp(color1, "black")=NULL)
{
band1=0;
}
else if (strcmp(color1, "brown")=NULL)
{
band1=10;
}
else if (strcmp(color1, "red")=NULL)
{
band1=20;
}
else if (strcmp(color1, "orange")=NULL)
{
band1=30;
}
else if (strcmp(color1, "yellow")=NULL)
{
band1=40;
}
else
{
}
printf("%d", band1);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud) 我学习C和尝试,我写了一个小程序.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int *x = malloc(sizeof(int)*3);
int i;
for(i=0;i<3; i++){
x[i] = i*i;
printf("x[i] = %d\n", x[i]);
}
free(x);
}
Run Code Online (Sandbox Code Playgroud)
现在输出是(ofc it)
x [i] = 0
x [i] = 1
x [i] = 4
我的问题是,我如何更改代码以获得输出?
x [0] = 0
x [1] = 1
x [2] = 4
#include<iostream>
using namespace std;
int main()
{
long long n, k;
cin >> n >> k;
long long limit[n];
long count;
for (long long i = 0; i < n; i++)
{
cin >> limit[i];
}
for (long long i = 0; i < n - 1; i++)
{
for (long long j = i + 1; j < n; j++)
{
if (k > (limit[i] + limit[j]))
count++;
}
}
cout << count;
}
Run Code Online (Sandbox Code Playgroud)
https://www.codechef.com/ZCOPRAC/problems/ZCO13003 好吧,我一直在从codechef网站解决问题,并遇到了一些难题.如您所见,我为该问题编写了上述代码,并能够使用所有示例输入验证我的代码.但是,只要我将其插入提交列,我就会遇到"发现错误"错误.谁能告诉我我做错了什么?
#include<stdio.h>
int calsum(int x,int y,int z);
int main()
{
while(1)
{
int a, b, c, sum;
printf("Enter any3 numbers");
scanf("%d%d%d", &a, &b, &c);
sum = calsum(a, b, c);
printf("sum=%d\n", sum);
}
}
int calsum (int x, int y, int z)
{
int d;
d = x + y + z;
if(d > 2)
return d;
else
d = 1;
return;
}
Run Code Online (Sandbox Code Playgroud)
当我提供输入时,-1 1 0我的输出应该是,1但它给出了0
原因?这是关于添加三个数字
我试图通过使用2个for循环在c ++中绘制一个矩形.一切都很好,除了矩形没有正确绘制.有人可以给我一个提示吗?非常感谢!
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
// Variabili
int base1, altezza1;
cout << "Inserisci base del rettangolo:";
cin >> base1;
cout << "Inserisci l'altezza del rettangolo:";
cin >> altezza1;
cout << "I dati che hai inserito sono -> " << "Base:" << base1 << " e Altezza:" << altezza1 << endl;
// Controllo base e altezza maggiori di zero + scrivo in output il rettangolo con i dati in input
if(base1 …Run Code Online (Sandbox Code Playgroud) 我需要使用堆栈计算 n 的阶乘,并且我编写的代码不返回任何结果。我也不知道 pop stack 真正做了什么(它的第二个参数是什么)所以我只是在那里使用了一个随机值。我使用int **x;是因为我不知道该放什么pop(&mystack,*x);。
#include <iostream>
using namespace std;
int n;
int aux;
int aux1;
int aux2;
int **x;
typedef struct {
int content[100];
int top;
} stack;
stack mystack;
int push(stack *somestack,int somevalue)
{
if (somestack->top+1>=100)
return 1;
(*somestack).top++;
(*somestack).content[(*somestack).top]=somevalue;
return 0;
}
int pop(stack *somestack, int *oldvalue)
{
if((*somestack).top==0)
{
return 1;
}
*oldvalue=(*somestack).content[(*somestack).top];
return 0;
}
int main()
{
cout<<"n=";
cin>>n;
push(&mystack,n);
int direction=1;
while(mystack.top>=1)
{
if((direction==1)&&(mystack.content[mystack.top]>1))
{
aux=mystack.content[mystack.top];
push(&mystack,aux-1); …Run Code Online (Sandbox Code Playgroud) 当我运行以下C程序时,编译器显示分段错误(核心转储).我不明白为什么会这样,我怎么能检索这段代码.
#include <stdio.h>
int power(int x, int n)
{
if (n = 0)
return 1;
else
return x * power (x, n - 1);
}
int main(void)
{
int x=3,n=4;
printf("the answer is:%d\n",power(3,4));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用向量找出二维矩阵的转置。以下代码对我不起作用。我试图将矩阵传递到名为 transpose() 的函数中。我还可以采取哪些其他方法?如果您指出我的错误,我将不胜感激。
#include <iostream>
#include <vector>
using namespace std;
void transpose(vector<vector<int> > &b)
{
vector<vector<int> > trans_vec;
for(int i=0;i<b.size();i++)
{
for(int j=0;j<b[i].size();j++)
{
trans_vec[j][i]=b[i][j];
}
}
}
int main()
{
vector<vector<int> > v1;
for(int i=0;i<2;i++)
{
vector<int> temp;
for(int j=0;j<3;j++)
{
temp.push_back(i);
}
v1.push_back(temp);
}
//Display output
for(int i=0;i<v1.size();i++)
{
for(int j=0;j<v1[i].size();j++)
{
cout<<v1[i][j];
}
cout<<endl;
}
transpose(v1);
//display transposed matrix
for(int i=0;i<v1.size();i++)
{
for(int j=0;j<v1[i].size();j++)
{
cout<<v1[i][j];
}
cout<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)