JOJ:1012

水题

为了复习下c的最基本

//joj提示runtime error???
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//定义个宏,求指定的字符在字符串中的位置,c中居然没有这个函数???
#define strPosition(str,c) strchr(str,c)-str
int gcd(int a,int b)
{
	if(a < b)
	{
		int tmp = a;
		a = b;
		b = tmp;
	}	
	while(a%b !=0)
	{
		int tmp = a%b;
		a = b;
		b = tmp;
	}
	return b;
}
int getNum(char* str,int begin,int end)
{
	int num = 0;
	for(int i=begin;i<=end;i++)
		num = num * 10 + str[i]-48;.//居然没有int.parse函数,java把我弄傻了
	return num;
}
int operation(char* equation)
{
	int start = strPosition(equation,'x')-1;
	int a = getNum(equation,0,start);
	int b = getNum(equation,strPosition(equation,'+')+1,strPosition(equation,'y')-1);
	int c = getNum(equation,strPosition(equation,'=')+1,strlen(equation)-1);
	return c%gcd(a,b) == 0;
}
int main()
{
	int time;
	char *equation[20];
       //读入一个,处理一个
	while(scanf("%d",&time) != EOF) 
	{
		for(int i=0;i<time;i++)
		{		
			scanf("%s",equation);
			if(operation(equation) ==0)
				printf("%s has a solution.\n",equation);
			else
				printf("%s has no solution.\n",equation);			
		}
	}	
	return 1;
}

 一个简单版

int main()
{
int t;
char str[1000];
scanf("%d", &t);
while (t--)
{
int a,b,c;
scanf("%s", str);
sscanf(str, "%dx+%dy=%d", &a, &b, &c);//重点是这个函数
if (c % mcd(a, b) == 0)
{
printf("%s has a solution.\n", str);
}
else
{
printf("%s has no solution.\n", str);
}
}
}
 

 

 在加一个申请内存版的

//忘了,居然没有string
        char *equation[20];
	while(scanf("%d",&time) != EOF) 
	{
		for(int i=0;i<time;i++)
		{	
			equation[i] = (char*)malloc(30);
			scanf("%s",equation[i]);
			if(operation(equation[i]) ==0)
				printf("%s has a solution.\n",equation[i]);
			else
				printf("%s has no solution.\n",equation[i]);
			free(equation[i]);//释放内存,别忘了
		}                                                                                                                                            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值