C++求助!急!
题目描述:判断一个正整数 n 是否能被一个“幸运数”整除。幸运数是指一个只包含 4 或 7 的正整数,如 7、47、477 等都是幸运数,17、42 则不是幸运数。
输入输出格式:
输入格式:
一行一个正整数 n,1<=n<=1000。
输出格式:
一行一个字符串,如果能被幸运数整除输出“YES”(注意大写);否则,输出“NO”(注意大写)。
输入输出样例:
输入样例#1:
47
输出样例#1:
YES
输入样例#2:
23
输出样例#2:
NO
为什么运行下面代码会出错:
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
bool isLuckyNumber(int n)
{
char notlucky[]={'0','1','2','3','5','6','8','9'};
char num;
itoa(n,num,4);
for(int i=0;i<4;i++)
for(int j=0;j<8;j++)
if(num==notlucky)
return 0;
return 1;
}
int main()
{
int n;
scanf("%s",n);
for(int i=0;i<1000;i++)
{
if(isLuckyNumber(i))
{
if(n%i==0)
{
printf("YES");
return 0;
}
}
}
printf("NO");
return 0;
}
本帖最后由 zltzlt 于 2020-8-1 17:32 编辑
scanf() 那里有点小错误(应该是 %d,而且应该加上取址符 &),而且不建议转成字符串处理。
这样即可:
#include <stdio.h>
#include <cstring>
#include <cstdlib>
using namespace std;
bool isLuckyNumber(int n)
{
int i, j;
for (i = 0; i < 3; i++)
{
if ((n % 10) != 4 && (n % 10) != 7)
return 0;
n /= 10;
}
return 1;
}
int main()
{
int n, i;
scanf("%d", &n);
for (i = 0; i < 1000; i++)
{
if (isLuckyNumber(i))
{
if (n % i == 0)
{
printf("YES");
return 0;
}
}
}
printf("NO");
return 0;
} 个人认为判断幸运数就写错了
本帖最后由 永恒的蓝色梦想 于 2020-8-1 17:27 编辑
你的代码不够快,要用这个{:10_256:}:#include<iostream>
#include<set>
using namespace std;
const set<unsigned short> targets{ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268, 272, 276, 280, 284, 288, 292, 296, 300, 304, 308, 312, 316, 320, 324, 328, 332, 336, 340, 344, 348, 352, 356, 360, 364, 368, 372, 376, 380, 384, 388, 392, 396, 400, 404, 408, 412, 416, 420, 424, 428, 432, 436, 440, 444, 448, 452, 456, 460, 464, 468, 472, 476, 480, 484, 488, 492, 496, 500, 504, 508, 512, 516, 520, 524, 528, 532, 536, 540, 544, 548, 552, 556, 560, 564, 568, 572, 576, 580, 584, 588, 592, 596, 600, 604, 608, 612, 616, 620, 624, 628, 632, 636, 640, 644, 648, 652, 656, 660, 664, 668, 672, 676, 680, 684, 688, 692, 696, 700, 704, 708, 712, 716, 720, 724, 728, 732, 736, 740, 744, 748, 752, 756, 760, 764, 768, 772, 776, 780, 784, 788, 792, 796, 800, 804, 808, 812, 816, 820, 824, 828, 832, 836, 840, 844, 848, 852, 856, 860, 864, 868, 872, 876, 880, 884, 888, 892, 896, 900, 904, 908, 912, 916, 920, 924, 928, 932, 936, 940, 944, 948, 952, 956, 960, 964, 968, 972, 976, 980, 984, 988, 992, 996, 1000, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196, 203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322, 329, 336, 343, 350, 357, 364, 371, 378, 385, 392, 399, 406, 413, 420, 427, 434, 441, 448, 455, 462, 469, 476, 483, 490, 497, 504, 511, 518, 525, 532, 539, 546, 553, 560, 567, 574, 581, 588, 595, 602, 609, 616, 623, 630, 637, 644, 651, 658, 665, 672, 679, 686, 693, 700, 707, 714, 721, 728, 735, 742, 749, 756, 763, 770, 777, 784, 791, 798, 805, 812, 819, 826, 833, 840, 847, 854, 861, 868, 875, 882, 889, 896, 903, 910, 917, 924, 931, 938, 945, 952, 959, 966, 973, 980, 987, 994, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400, 440, 480, 520, 560, 600, 640, 680, 720, 760, 800, 840, 880, 920, 960, 1000, 44, 88, 132, 176, 220, 264, 308, 352, 396, 440, 484, 528, 572, 616, 660, 704, 748, 792, 836, 880, 924, 968, 47, 94, 141, 188, 235, 282, 329, 376, 423, 470, 517, 564, 611, 658, 705, 752, 799, 846, 893, 940, 987, 70, 140, 210, 280, 350, 420, 490, 560, 630, 700, 770, 840, 910, 980, 74, 148, 222, 296, 370, 444, 518, 592, 666, 740, 814, 888, 962, 77, 154, 231, 308, 385, 462, 539, 616, 693, 770, 847, 924, 400, 800, 404, 808, 407, 814, 440, 880, 444, 888, 447, 894, 470, 940, 474, 948, 477, 954, 700, 704, 707, 740, 744, 747, 770, 774, 777 };
int main() {
unsigned short i;
cin >> i;
cout << (targets.count(i) ? "YES" : "NO") << endl;
return 0;
} #include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
bool isLuckyNumber(int n)
{
if(n==0)return false;
while(n){
int temp = n%10;
if(temp!=4&&temp!=7)return false;
n = n/10;
}
return true;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=4;i<1000;i++)
{
if(isLuckyNumber(i))
{
if(n%i==0)
{
printf("YES");
return 0;
}
}
}
printf("NO");
return 0;
} zltzlt 发表于 2020-8-1 16:52
scanf() 那里有点小错误(应该是 %d,而且应该加上取址符 &),而且不建议转成字符串处理。
这样即可:
...
对于幸运数的判断写错了,这样只会判断个位 baige 发表于 2020-8-1 17:31
对于幸运数的判断写错了,这样只会判断个位
感谢指出,漏了句 n /= 10
页:
[1]