有高手能幫幫忙嗎 拜託了!!
狀況:
與正確輸出不相符(line:7)
您的答案為: MMCCXVII
正確答案為: MCMXCVII
code:
#include<stdio.h>
有高手能幫幫忙嗎 拜託了!!
狀況:
與正確輸出不相符(line:7)
您的答案為: MMCCXVII
正確答案為: MCMXCVII
code:
#include
我不是什麼高手
但改好可以AC了
請參考
#include<stdio.h>
#include <iostream>
using namespace std;
char c1; char*c[100]={0}; //*c[] 指標陣列都指向字串
int counter=0,sum1=0,sum2=0,i,j=0,k=0,l=0; //sum1 sum2 為兩數個別和
int m,diff[1000],a[10],b[10],temp;
//diff為兩數差 a[]中裝字母代表的值 b[]把數字拆成四位(b[0]為千位)
//temp紀錄讀入兩數間空格的陣列index值
int main()
{
while(scanf("%c",&c1)!=EOF)
{
switch(c1)//把字母轉成值 存入陣列方便算和
{
case'I':a[counter]=1;counter++;break;
case'V':a[counter]=5;counter++;break;
case'X':a[counter]=10;counter++;break;
case'L':a[counter]=50;counter++;break;
case'C':a[counter]=100;counter++;break;
case'D':a[counter]=500;counter++;break;
case'M':a[counter]=1000;counter++;break;
case' ':a[counter]=0;temp=counter;counter++;break;
case'#':break;
}
if(c1 == '#') break;
if(c1=='\n')//讀到換行符號 準備讀下一組資料
{
for(i=0;i<temp;i++)
{
if(i + 1 < temp && a[i + 1] > a[i])
sum1 -= a[i];
else
sum1 += a[i];
}
for(k=temp+1;k<counter;k++)
{
if(k + 1 < counter && a[k + 1] > a[k])
sum2 -= a[k];
else
sum2 += a[k];
}
if(sum1>sum2) //因為要取絕對值 故先比較
diff[j]=sum1-sum2;
else
diff[j]=sum2-sum1;
j++;
temp = counter=0;
sum1=0,sum2=0;
}
}
//cout << diff[0] << " " << diff[1] << "\n";
for(m=0;m<j;m++)//把上面存入陣列的值 轉成個十百千4位
{
b[0]=diff[m]/1000; //千位
b[1]=(diff[m]/100)%10; //百位
b[2]=(diff[m]/10)%10; //十位
b[3]=diff[m]%10; //個位
switch(b[0]) //千位遇到的狀況 不超過4000
{
case 0:c[0]="";break; //千位數為零的意思
case 1:c[0]="M\0";break;
case 2:c[0]="MM\0";break;
case 3:c[0]="MMM\0";break;
}
switch(b[1])
{
case 0:c[1]="";break;
case 1:c[1]="C\0";break;
case 2:c[1]="CC\0";break;
case 3:c[1]="CCC\0";break;
case 4:c[1]="CD\0";break;
case 5:c[1]="D\0";break;
case 6:c[1]="DC\0";break;
case 7:c[1]="DCC\0";break;
case 8:c[1]="DCCC\0";break;
case 9:c[1]="CM\0";break;
}
switch(b[2])
{
case 0:c[2]="";break;
case 1:c[2]="X\0";break;
case 2:c[2]="XX\0";break;
case 3:c[2]="XXX\0";break;
case 4:c[2]="XL\0";break;
case 5:c[2]="L\0";break;
case 6:c[2]="LX\0";break;
case 7:c[2]="LXX\0";break;
case 8:c[2]="LXXX\0";break;
case 9:c[2]="XC\0";break;
}
switch(b[3])
{
case 0:c[3]="";break;
case 1:c[3]="I\0";break;
case 2:c[3]="II\0";break;
case 3:c[3]="III\0";break;
case 4:c[3]="IV\0";break;
case 5:c[3]="V\0";break;
case 6:c[3]="VI\0";break;
case 7:c[3]="VII\0";break;
case 8:c[3]="VIII\0";break;
case 9:c[3]="IX\0";break;
}
if(diff[m] == 0)//全為零
printf("ZERO\n");
else
printf("%s%s%s%s\n",c[0],c[1],c[2],c[3]);
}
return 0;
}
有高手能幫幫忙嗎 拜託了!!
狀況:
與正確輸出不相符(line:7)
您的答案為: MMCCXVII
正確答案為: MCMXCVII
code:
#include
我不是什麼高手
但改好可以AC了
請參考
#include
#include
using namespace std;
char c1; char*c[100]={0}; //*c[] 指標陣列都指向字串
int counter=0,sum1=0,sum2=0,i,j=0,k=0,l=0; //sum1 sum2 為兩數個別和
int m,diff[1000],a[10],b[10],temp;
//diff為兩數差 a[]中裝字母代表的值 b[]把數字拆成四位(b[0]為千位)
//temp紀錄讀入兩數間空格的陣列index值
int main()
{
while(scanf("%c",&c1)!=EOF)
{
switch(c1)//把字母轉成值 存入陣列方便算和
{
case'I':a[counter]=1;counter++;break;
case'V':a[counter]=5;counter++;break;
case'X':a[counter]=10;counter++;break;
case'L':a[counter]=50;counter++;break;
case'C':a[counter]=100;counter++;break;
case'D':a[counter]=500;counter++;break;
case'M':a[counter]=1000;counter++;break;
case' ':a[counter]=0;temp=counter;counter++;break;
case'#':break;
}
if(c1 == '#') break;
if(c1=='\n')//讀到換行符號 準備讀下一組資料
{
for(i=0;i
{
if(i + 1 < temp && a[i + 1] > a[i])
sum1 -= a[i];
else
sum1 += a[i];
}
for(k=temp+1;k
{
if(k + 1 < counter && a[k + 1] > a[k])
sum2 -= a[k];
else
sum2 += a[k];
}
if(sum1>sum2) //因為要取絕對值 故先比較
diff[j]=sum1-sum2;
else
diff[j]=sum2-sum1;
j++;
temp = counter=0;
sum1=0,sum2=0;
}
}
//cout << diff[0] << " " << diff[1] << "\n";
for(m=0;m
{
b[0]=diff[m]/1000; //千位
b[1]=(diff[m]/100)%10; //百位
b[2]=(diff[m]/10)%10; //十位
b[3]=diff[m]%10; //個位
switch(b[0]) //千位遇到的狀況 不超過4000
{
case 0:c[0]="";break; //千位數為零的意思
case 1:c[0]="M\0";break;
case 2:c[0]="MM\0";break;
case 3:c[0]="MMM\0";break;
}
switch(b[1])
{
case 0:c[1]="";break;
case 1:c[1]="C\0";break;
case 2:c[1]="CC\0";break;
case 3:c[1]="CCC\0";break;
case 4:c[1]="CD\0";break;
case 5:c[1]="D\0";break;
case 6:c[1]="DC\0";break;
case 7:c[1]="DCC\0";break;
case 8:c[1]="DCCC\0";break;
case 9:c[1]="CM\0";break;
}
switch(b[2])
{
case 0:c[2]="";break;
case 1:c[2]="X\0";break;
case 2:c[2]="XX\0";break;
case 3:c[2]="XXX\0";break;
case 4:c[2]="XL\0";break;
case 5:c[2]="L\0";break;
case 6:c[2]="LX\0";break;
case 7:c[2]="LXX\0";break;
case 8:c[2]="LXXX\0";break;
case 9:c[2]="XC\0";break;
}
switch(b[3])
{
case 0:c[3]="";break;
case 1:c[3]="I\0";break;
case 2:c[3]="II\0";break;
case 3:c[3]="III\0";break;
case 4:c[3]="IV\0";break;
case 5:c[3]="V\0";break;
case 6:c[3]="VI\0";break;
case 7:c[3]="VII\0";break;
case 8:c[3]="VIII\0";break;
case 9:c[3]="IX\0";break;
}
if(diff[m] == 0)//全為零
printf("ZERO\n");
else
printf("%s%s%s%s\n",c[0],c[1],c[2],c[3]);
}
return 0;
}
非常謝謝 超感動的^^