gpt4 book ai didi

c++ - 编写一个程序,输出 5 位数字的总数,其中有 5,但没有 8

转载 作者:行者123 更新时间:2023-11-30 02:36:05 25 4
gpt4 key购买 nike

我的挑战是输出有数字 5 但没有数字 8 的五位数的总数。到目前为止我只有两个答案是 0 或 90000。有人能帮我吗?

#include <iostream>
using namespace std;

int main() {
int number;
int counter=10000;
int ncounter=0;
while (counter >= 10000 && counter <= 99999) {
int n1,n2,n3,n4,n5;
counter = counter + 1;
n1 = number%10;
number /= 10;
n2 = number%10;
number /= 10;
n3 = number%10;
number /= 10;
n4 = number%10;
number /=10;
n5 = number%10;
number /= 10;
if (n1 == 5||n2 == 5||n3 == 5||n4 == 5||n5 == 5)
if (n1!=8)
if (n2!=8)
if (n3!=8)
if(n4!=8)
if (n5!=8)
ncounter=ncounter+1;
}
cout<<ncounter<<endl;
return 0;
}

最佳答案

(num with 5 but not 8) = (num without 8) - (num with neither 8 nor 5) = 8*9*9*9*9 - 7*8*8*8*8= 23816

关于c++ - 编写一个程序,输出 5 位数字的总数,其中有 5,但没有 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com