Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 18, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/b53f38ad8540de6c9fe46026fe4db225 to your computer and use it in GitHub Desktop.
Save maehrm/b53f38ad8540de6c9fe46026fe4db225 to your computer and use it in GitHub Desktop.
平成20年度秋期試験_基本情報午後問6
#include <stdio.h>
void convert(long, char []);
void convert(long num, char str[]) {
int minus = 0, i = 0, j = 0;
char table[] = "0123456789";
char tmp;
if (num < 0) {
minus = 1;
num = -num;
}
do {
str[j++] = table[num % 10]; /* 数値の下位から順に文字に変換 */
num /= 10;
i++;
if (i % 3 == 0 && num != 0) {
str[j++] = ',';
}
} while (num != 0);
if (minus != 0) {
str[j++] = '-';
}
str[j--] = '\0';
for (i = 0; i < j; i++, j--) { /* 順序を逆にする。 */
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
int main(void) {
long nums[] = {1234567, -57482, 63, -999999};
char str[BUFSIZ];
int i, size = sizeof(nums) / sizeof(nums[0]);
for (i = 0; i < size; i++) {
convert(nums[i], str);
printf("%s\n", str);
}
return 0;
}
@maehrm
Copy link
Author

maehrm commented Mar 18, 2019

平成20年度秋季基本情報午後問6 - Mae向きなブログ https://maehrm.hatenablog.com/entry/2019/03/19/054007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment