编成求和:s=a+aa+aaa+aaaa+…+aaa..a(n个a),其中a为1-9的数字。

c++
2024-12-12 09:54:33
推荐回答(2个)
回答1:

崴姐 正好我们也有这个题#include
void main(){
int a,n,s=0,temp=0,i=1;
printf("Please Input The Value Of a(a<10且a>=0):\n");
scanf("%d",&a);
printf("Please Input The Value Of n:\n");
scanf("%d",&n);for(i;i<=n;i++)
{
temp=temp*10+a;
s=s+temp;
}
printf("The Sum=%d\n\n",s);
}

回答2:

#include using namespace std; int nbr[100]; void check(){ for (int i = 0; i < 99; ++i) { if (nbr[i] > 9) { nbr[i + 1] += nbr[i] / 10; nbr[i] %= 10; } }} void calculate(int a, int n){ for (int i = 0; i < n; ++i) { nbr[i] = a * (n - i); } check();} int main(){ int n, a; int i = 100; cout << "Enter a: "; cin >> a; cout << "Enter n: "; cin >> n; calculate(a, n); while (i > 0 && !nbr[--i]); while (i >= 0) { cout << nbr[i--]; } return 0;}