ACM问题 我的代码哪里错了?? http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1406
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1406Letdbeadigitandnbeapos...
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1406
Let d be a digit and n be a positive number. A sequence of numbers t1, t2, . . . , tm is a d-sum of n if
• t1 + t2 + · · · + tm = n, and
• t1, t2, . . . , tm are numbers expressed using only the digit d. For example, if d = 2 then ti could be 2, 22, 222, . . .
Input
Each line of the input contains a digit d and a positive number n, in that order.
Output
For each line of input specifying a digit d and positive number n, output a line containing d, a line containing n and a d-sum of n having minimum number of terms m, and a line containing the number of terms m. If no d-sum of n exists, output a message to that effect.
Sample Input
3 402
5 64
Sample Output
d = 3
n = 402 = 333 + 33 + 33 + 3
...a shortest summation with m = 4 terms.
d = 5
n = 64
...has no such summation.
Source
我的代码:
#include<iostream>
#include<math.h>
using namespace std;
struct marks
{
int jj;
int count;
}mark[100];
int main()
{
__int64 n,dd[100];
int d,count,i,j,k,sum,sum1;
while(scanf("%d %I64d",&d,&n)!=EOF)
{
cout<<"d = "<<d<<endl;
printf("n = %I64d",n);
for(i=0;;i++)
{
if(n/(__int64)pow(double(10),double(i))<10)
break;
}//cout<<"i="<<i<<endl;
memset(dd,d,sizeof(dd));
dd[0]=d;
for(j=1;j<=i;j++)
{
dd[j]=d*pow(double(10),double(j))+dd[j-1];
}
j--;
// cout<<"j="<<j<<endl;
// printf("\ndd[j]=%I64d\n",dd[j]);
memset(mark,0,sizeof(mark));
count=0;sum=0;
while(n>d)
{
mark[count].count=n/dd[j];
mark[count].jj=j;
sum+=mark[count].count;
n=n-mark[count].count*dd[j];
count++;
j--;
}
mark[count].count=n/dd[j];
mark[count].jj=j;
sum+=mark[count].count;
n=n-mark[count].count*dd[j];
count++;
/* printf("\nn=%I64d\n",n);
for(j=0;j<count;j++)
{
cout<<mark[j].count<<" "<<mark[j].jj<<endl;
} */
sum1=sum;
if(n==0)
{
cout<<" = ";
for(j=0;j<count;j++)
{
for(k=0;k<mark[j].count;k++)
{
printf("%I64d",dd[mark[j].jj]);
if(sum1!=1)
{
cout<<" + ";
sum1--;
}
}
}
cout<<endl<<"...a shortest summation with m = "<<sum<<" terms."<<endl;
}
else if(n!=0)
{
cout<<endl<<"...has no such summation."<<endl;
}
}
return 0;
} 展开
Let d be a digit and n be a positive number. A sequence of numbers t1, t2, . . . , tm is a d-sum of n if
• t1 + t2 + · · · + tm = n, and
• t1, t2, . . . , tm are numbers expressed using only the digit d. For example, if d = 2 then ti could be 2, 22, 222, . . .
Input
Each line of the input contains a digit d and a positive number n, in that order.
Output
For each line of input specifying a digit d and positive number n, output a line containing d, a line containing n and a d-sum of n having minimum number of terms m, and a line containing the number of terms m. If no d-sum of n exists, output a message to that effect.
Sample Input
3 402
5 64
Sample Output
d = 3
n = 402 = 333 + 33 + 33 + 3
...a shortest summation with m = 4 terms.
d = 5
n = 64
...has no such summation.
Source
我的代码:
#include<iostream>
#include<math.h>
using namespace std;
struct marks
{
int jj;
int count;
}mark[100];
int main()
{
__int64 n,dd[100];
int d,count,i,j,k,sum,sum1;
while(scanf("%d %I64d",&d,&n)!=EOF)
{
cout<<"d = "<<d<<endl;
printf("n = %I64d",n);
for(i=0;;i++)
{
if(n/(__int64)pow(double(10),double(i))<10)
break;
}//cout<<"i="<<i<<endl;
memset(dd,d,sizeof(dd));
dd[0]=d;
for(j=1;j<=i;j++)
{
dd[j]=d*pow(double(10),double(j))+dd[j-1];
}
j--;
// cout<<"j="<<j<<endl;
// printf("\ndd[j]=%I64d\n",dd[j]);
memset(mark,0,sizeof(mark));
count=0;sum=0;
while(n>d)
{
mark[count].count=n/dd[j];
mark[count].jj=j;
sum+=mark[count].count;
n=n-mark[count].count*dd[j];
count++;
j--;
}
mark[count].count=n/dd[j];
mark[count].jj=j;
sum+=mark[count].count;
n=n-mark[count].count*dd[j];
count++;
/* printf("\nn=%I64d\n",n);
for(j=0;j<count;j++)
{
cout<<mark[j].count<<" "<<mark[j].jj<<endl;
} */
sum1=sum;
if(n==0)
{
cout<<" = ";
for(j=0;j<count;j++)
{
for(k=0;k<mark[j].count;k++)
{
printf("%I64d",dd[mark[j].jj]);
if(sum1!=1)
{
cout<<" + ";
sum1--;
}
}
}
cout<<endl<<"...a shortest summation with m = "<<sum<<" terms."<<endl;
}
else if(n!=0)
{
cout<<endl<<"...has no such summation."<<endl;
}
}
return 0;
} 展开
展开全部
这题的关键在于:
1. n能被d整除, 是有解的充要条件.
2. 注意输出顺序 从大到小. 不清楚从小到大是否会出错, 不过最好按着sample来.
我的代码贴一下. 很简单, 没有几行.
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int d, n, nn, x, cnt;
while (scanf("%d%d", &d, &n) == 2)
{
if (n%d != 0)
{
printf("d = %d\nn = %d\n...has no such summation.\n\n", d, n);
continue;
}
nn = n / d;
printf("d = %d\nn = %d = ", d, n);
cnt = 0;
x = 1111111111;
while (nn != 0)
{
while (x > nn) x /= 10;
nn -= x;
if (cnt != 0) printf(" + ");
printf("%d", x*d);
cnt++;
}
printf("\n...a shortest summation with m = %d terms.\n\n", cnt);
}
}
1. n能被d整除, 是有解的充要条件.
2. 注意输出顺序 从大到小. 不清楚从小到大是否会出错, 不过最好按着sample来.
我的代码贴一下. 很简单, 没有几行.
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int d, n, nn, x, cnt;
while (scanf("%d%d", &d, &n) == 2)
{
if (n%d != 0)
{
printf("d = %d\nn = %d\n...has no such summation.\n\n", d, n);
continue;
}
nn = n / d;
printf("d = %d\nn = %d = ", d, n);
cnt = 0;
x = 1111111111;
while (nn != 0)
{
while (x > nn) x /= 10;
nn -= x;
if (cnt != 0) printf(" + ");
printf("%d", x*d);
cnt++;
}
printf("\n...a shortest summation with m = %d terms.\n\n", cnt);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询