如何在C中执行以下Python程序? [英] How do I do the following Python program in C ?

查看:94
本文介绍了如何在C中执行以下Python程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#我在C中有相同的结果...



#How do I have the same result in C...

t=int(input())
for j in range(t):
        s=input()
        def co(a,b):
                c=0
                for i in b:
                        if(i==a):
                                c+=1
                return c

        for i in s:
                if(co(i,s)==len(s)/2):
                        print("YES")
                        break
        else:
                print("NO")





我的尝试:





What I have tried:

#include<stdio.h>
#include<string.h>
int co(char,char);
void main(){
	char s[50];
	int i,c;
	scanf("%s",s);
	while(s[i]!='\0'){
		if(co(s[i],s)==len(s)/2)
			printf("YES");
		else
			printf("NO");
		i++;
	}
}
int co(char a,char b[50]){
	int i,c=0;
	while(b[i]!='\0'){
		if(b[i]==a)
			c++;
	}
	return c;
}

推荐答案

int i,c;
scanf("%s",s);
while(s[i]!='\0'){



您尚未初始化索引变量( i )为零,开始索引数组。如果你使用了合适的有意义的变量名而不是单个字母,你的代码可能会更容易调试。


You have not initialised the index variable (i) to zero, to start indexing the array. Your code would probably be easier to debug if you used proper meaningful variable names rather than single letters.


这篇关于如何在C中执行以下Python程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆