208Sample OutputLucky 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;}
新闻热点
疑难解答