首页 > 学院 > 开发设计 > 正文

Lucky Number

2019-11-06 06:28:37
字体:
来源:转载
供稿:网友
   To Chinese people, 8 is a lucky number. Now your task is to judge if a number is lucky.We say a number is lucky if it’s a multiple of 8, or the sum of digits that make up the number is a multiple of 8, or the sum of every digit’s square is a multiple of 8.InputThe first line contains an integer stands for the number of test cases. Each test case contains an integer n (n >= 0). OutputFor each case, output “Lucky number!” if the number is lucky, otherwise output “What a pity!”.Sample Input
208Sample Output
Lucky number!Lucky number!
-----------------------------------------------------------------
思路分析:
输入一个数,对这个数进行三种情况的判断,
1、该数是否是8的倍数;
2、该数每一位的和是否是8的倍数;
3、该数每一位的平方的和是否是8的倍数;
如果满足,则输出Lucky number!否则输出What a pity!
#include <iostream>#include<string>#include<stdio.h>#include<cmath>/* run this PRogram using the console pauser or add your own getch, system("pause") or input loop */using namespace std;//8的倍数,和是八得倍数,和的平方是八的倍数 int digsum(int a){//和是否是8的倍数	int sum=0;	while(a!=0)	{		sum+=a%10;		a=a/10;	}	return sum;}int sumf(int a)  //和的平方是否是8的倍数{	int sum=0;	while(a!=0)	{		sum=sum+pow(a%10,2);		a=a/10;	}	return sum;}int main(int argc, char** argv) {	int n,num[1000],sum;	cin>>n;	for(int i=0;i<n;i++)	{		cin>>num[i];		if(num[i]==0)		printf("Lucky number!/n");		else if(num[i]%8==0)		{			printf("Lucky number!/n");		}		else if((sum=digsum(num[i]))%8==0)		{			printf("Lucky number!/n");		}		else if((sum=sumf(num[i]))%8==0)		{			printf("Lucky number!/n");		}		else		{			printf("What a pity!/n");		}	 } 	 	return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表