一本通1083:计算星期几

一本通1083:计算星期几_第1张图片

一:常见错误

int main()
{
 //long long范围最大是9*10^18
 //而a<100且b < 1000,所以a^b的最大值是100^1000,long long存不下
	long long a = 0, b = 0;
	scanf("%lld %lld", &a, &b);
	int i = 0;
	long long  r = pow(a, b);//3的2000次方太大了,无法储存
	r = r % 7;//范围是0~6
	switch (r)
	{
	case 1:
		printf("Monday\n");
		break; 
	case 2:
			printf("Tuseday\n");
			break;
	case 3:
		printf("Wednesday\n");
		break;
	case 4:
		printf("Thursday\n");
		break;
	case 5:
		printf("Friday\n");
		break;
	case 6:
		printf("Saturday\n");
		break;
	case 0:
		printf("Sunday\n");
		break;
	}
	return 0;
}

二:思路

一本通1083:计算星期几_第2张图片

三:代码

一本通1083:计算星期几_第3张图片

#include 
int main()
{
	int a = 0, b = 0;
	int s = 1;
	scanf("%d %d", &a, &b);
	int i = 0;
	for (i = 0; i < b; i++)
	{
		s = (s * a) % 7;//%7结果范围在0~6之间
	}
	switch (s)
	{
	case 1:
		printf("Monday\n");
		break;
	case 2:
		printf("Tuesday\n");
		break;
	case 3:
		printf("Wednesday\n");
		break;
	case 4:
		printf("Thursday\n");
		break;
	case 5:
		printf("Friday\n");
		break;
	case 6:
		printf("Saturday\n");
		break;
	case 0:
		printf("Sunday\n");
		break;
	}
	return 0;
}

你可能感兴趣的:(题目讲解,算法,c语言,学习方法,开发语言,visual,studio)