|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 bin554385863 于 2019-7-26 17:10 编辑
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- /*根据罗马数字转换阿拉伯数字 */
- // -I+ 73 1
- // V 86 5
- // -X+ 88 10
- // L 92 50
- // -C+ 67 100
- // D 68 500
- // -M+ 77 1000
- int romanToInt(char *s)
- {
- int slen = strlen(s);
- int sum = 0;
- /*--------------定义一个整数数组--------------- */
- int *arr = (int *)malloc(sizeof(int) * (slen));
- /*--------------转换成整数数组----------------- */
- for (size_t i = 0; i < slen; i++)
- {
- if (s[i] == 'I')
- {
- arr[i] = 1;
- }
- else if (s[i] == 'V')
- {
- arr[i] = 5;
- }
- else if (s[i] == 'X')
- {
- arr[i] = 10;
- }
- else if (s[i] == 'L')
- {
- arr[i] = 50;
- }
- else if (s[i] == 'C')
- {
- arr[i] = 100;
- }
- else if (s[i] == 'D')
- {
- arr[i] = 500;
- }
- else if (s[i] == 'M')
- {
- arr[i] = 1000;
- }
- }
- /*-----------调整数组元素的正负号--------------------------------- */
- for (size_t i = 0, j = 1; i <slen - 1, j < slen; i++, j = i + 1)
- {
- if (arr[i] == 1)
- {
- if (arr[i] < arr[j])
- {
- arr[i] = -1;
- }
- }
- if (arr[i] == 10)
- {
- if (arr[i] < arr[j])
- {
- arr[i] = -10;
- }
- }
- if (arr[i] == 100)
- {
- if (arr[i] < arr[j])
- {
- arr[i] = -100;
- }
- }
- }
- /*----------------求和---------------------------- */
- for (size_t i = 0; i < slen; i++)
- {
- sum = sum + arr[i];
- }
-
-
- return sum;
- free(arr);
- }
- int main(int argc, char const *argv[])
- {
- char str[50];
- gets(str);
- int a = romanToInt(str);
- printf("%d", a);
- }
复制代码
---------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\C_Cpp_Language Study>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-tdhf0z3y.spf --stdout=Microsoft-MIEngine-Out-paepwm2z.rgr --stderr=Microsoft-MIEngine-Error-e5zryizh.evl --pid=Microsoft-MIEngine-Pid-vipggfv1.r1h --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
I
1
E:\Administrator\Documents\C_Cpp_Language Study>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-z5vqnfv0.1lu --stdout=Microsoft-MIEngine-Out-2trhilsp.xug --stderr=Microsoft-MIEngine-Error-5wndy4gq.nl5 --pid=Microsoft-MIEngine-Pid-k4l4qid4.5h4 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
II
2
E:\Administrator\Documents\C_Cpp_Language Study>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-n1n2ebsa.xdv --stdout=Microsoft-MIEngine-Out-2jvaqbnc.uot --stderr=Microsoft-MIEngine-Error-z40nmd3p.qiq --pid=Microsoft-MIEngine-Pid-tm0rgura.vph --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
VI
6
E:\Administrator\Documents\C_Cpp_Language Study>
------------------------------------------------------------------------------------
看起来代码很短吧?我尼玛花了三天的时间才写出来(╯‵□′)╯︵┻━┻
这是人干的???
当然代码还不够严谨,算了也懒得改了,反正线上通过了 |
|