如图一所示的C语言编程题
起初我没有用函数来做,编译运行时,有几组数的结果没有输出(空白)。(貌似是有第4和6组)
当我把计算骰子点数的内容做成函数内容后,就成功了。我想知道我那个没用函数的方法是哪里出了问题。还是有漏掉了什么细节。没用函数和用了函数的代码如下。谢谢解答!
#include<stdio.h>
int main(void)
{
unsigned int a[200],b[200];
int sum1=0;
int sum2=0;
int n;
int i=1;
int j=1;
int m1,m2;
int m;
int num1,num2;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%u%u",&a[i],&b[i]);
for(j=1;j<=n;j++)
{ sum1=sum2=0;
while(a[j]!=0){
num1=a[j]%10;
a[j]/=10;
sum1+=num1;}
while(b[j]!=0){
num2=b[j]%10;
b[j]/=10;
sum2+=num2;}
m1=sum1%6+1;
m2=sum2%6+1;
m=m1+m2;
if((m==7)||(m==11))
printf("success!\n");
else if((m==2)||(m==3)||(m==12))
printf("fail!\n");
else
{
a[j]++;
b[j]++;
while(a[j]!=0)
{ sum1=sum2=0;
while(a[j]!=0){
num1=a[j]%10;
a[j]/=10;
sum1+=num1;}
while(b[j]!=0){
num2=b[j]%10;
b[j]/=10;
sum2+=num2;}
m1=sum1%6+1;
m2=sum2%6+1;
if((m1+m2)==m)
{
printf("success!\n");
a[j]=0;
}
else if ((m1+m2)==7)
{
printf("fail!\n");
a[j]=0;
}
else
{
a[j]++;
b[j]++;
}
}
}
}
return 0;
}
#include<stdio.h>
int sum(int a,int b);
int main(void)
{
unsigned int a[200],b[200];
int n;
int i=1;
int j=1;
int m;
int S;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%u%u",&a[i],&b[i]);
for(j=1;j<=n;j++)
{
m=sum(a[j],b[j]);
if((m==7)||(m==11))
printf("success!\n");
else if((m==2)||(m==3)||(m==12))
printf("fail!\n");
else
{
a[j]++;
b[j]++;
while(a[j]!=0)
{
S=sum(a[j],b[j]);
if (S==m)
{
printf("success!\n");
a[j]=0;
}
else if (S==7)
{
printf("fail!\n");
a[j]=0;
}
else
{
a[j]++;
b[j]++;
}
}
}
}
return 0;
}
int sum(int a,int b)
{ int m1,m2;
int m;
int num1,num2;
int sum1,sum2;
sum1=sum2=0;
while(a!=0){
num1=a%10;
a/=10;
sum1+=num1;}
while(b!=0){
num2=b%10;
b/=10;
sum2+=num2;}
m1=sum1%6+1;
m2=sum2%6+1;
m=m1+m2;
return m;
}