您的答案為: ZERO 正確答案為: MCMXCVIII
我使用visual c++測試可以通過
為什麼丟上來之後卻會出現上面的錯誤勒?
有人可以告訴我問題出在哪裡嗎?
以下是我的程式碼
#include <stdio.h> #include <string.h> int decode(char *code){ int value = 0, i, iplus = 0, xplus = 0, cplus = 0; for(i = strlen(code) - 1; i >= 0; i--){ switch(code[i]){ case 'I': if(iplus == 0) value += 1; else value -= 1; break; case 'V': iplus++; value += 5; break; case 'X': iplus++; if(xplus == 0) value += 10; else value -= 10; break; case 'L': xplus++; value += 50; break; case 'C': xplus++; if(cplus == 0) value += 100; else value -= 100; break; case 'D': cplus++; value += 500; break; case 'M': cplus++; value += 1000; break; default: printf("Input value fail"); break; } } return value; } char *encode(int value){ char code[16]; int i; for(i = 0; i < 15 && value > 0; i++){ while(value >= 1000){ code[i++] = 'M'; value -= 1000; } while(value > 900){ code[i++] = 'C'; code[i++] = 'M'; value -= 900; } while(value >= 500){ code[i++] = 'D'; value -= 500; } while(value >= 100){ code[i++] = 'C'; value -= 100; } while(value > 90){ code[i++] = 'X'; code[i++] = 'C'; value -= 90; } while(value >= 50){ code[i++] = 'L'; value -= 50; } while(value >= 10){ code[i++] = 'X'; value -= 10; } while(value > 9){ code[i++] = 'I'; code[i++] = 'X'; value -= 9; } while(value >= 5){ code[i++] = 'V'; value -= 5; } while(value >= 1){ code[i++] = 'I'; value -= 1; } } code[i - 1] = '\0'; return code; } int main(){ char buff1[16], buff2[16], a[16]; char *pch; int x[2]; while(scanf("%s", buff1)){ if(buff1[0] == '#') break; x[0] = decode(buff1); scanf("%s", buff2); x[1] = decode(buff2); if(x[0] == x[1]) strcpy(a, "ZERO"); else if(x[0] > x[1]) strcpy(a, encode(x[0] - x[1])); else if(x[0] < x[1]) strcpy(a, encode(x[1] - x[0])); printf("%s\n", a); } return 0; }
您的答案為: ZERO 正確答案為: MCMXCVIII
我使用visual c++測試可以通過
為什麼丟上來之後卻會出現上面的錯誤勒?
有人可以告訴我問題出在哪裡嗎?
以下是我的程式碼
#include #include int decode(char *code){ int value = 0, i, iplus = 0, xplus = 0, cplus = 0; for(i = strlen(code) - 1; i >= 0; i--){ switch(code[i]){ case 'I': if(iplus == 0) value += 1; else value -= 1; break; case 'V': iplus++; value += 5; break; case 'X': iplus++; if(xplus == 0) value += 10; else value -= 10; break; case 'L': xplus++; value += 50; break; case 'C': xplus++; if(cplus == 0) value += 100; else value -= 100; break; case 'D': cplus++; value += 500; break; case 'M': cplus++; value += 1000; break; default: printf("Input value fail"); break; } } return value; } char *encode(int value){ char code[16]; int i; for(i = 0; i < 15 && value > 0; i++){ while(value >= 1000){ code[i++] = 'M'; value -= 1000; } while(value > 900){ code[i++] = 'C'; code[i++] = 'M'; value -= 900; } while(value >= 500){ code[i++] = 'D'; value -= 500; } while(value >= 100){ code[i++] = 'C'; value -= 100; } while(value > 90){ code[i++] = 'X'; code[i++] = 'C'; value -= 90; } while(value >= 50){ code[i++] = 'L'; value -= 50; } while(value >= 10){ code[i++] = 'X'; value -= 10; } while(value > 9){ code[i++] = 'I'; code[i++] = 'X'; value -= 9; } while(value >= 5){ code[i++] = 'V'; value -= 5; } while(value >= 1){ code[i++] = 'I'; value -= 1; } } code[i - 1] = '\0'; return code; } int main(){ char buff1[16], buff2[16], a[16]; char *pch; int x[2]; while(scanf("%s", buff1)){ if(buff1[0] == '#') break; x[0] = decode(buff1); scanf("%s", buff2); x[1] = decode(buff2); if(x[0] == x[1]) strcpy(a, "ZERO"); else if(x[0] > x[1]) strcpy(a, encode(x[0] - x[1])); else if(x[0] < x[1]) strcpy(a, encode(x[1] - x[0])); printf("%s\n", a); } return 0; }
不好意思 我事新手沒有注意到格試跑掉了 我重新又發了一篇
麻煩請到另外那篇回應 感謝