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]);//释放内存,别忘了
		}                                                                                                                                            

你可能感兴趣的:(C++,c,C#)