前提・実現したいこと
PGM形式の画像ファイルをリダイレクションを用いて入力し、Run Length法によって圧縮したデータを出力する(encode.exe)とencode.exeを入力してPGM形式の画像ファイルを出力する(decode.exe)のプログラムはできたのですがもっとスマートなやり方が分かりません
該当のソースコード
Cソースコード
1 2encode.exeのプログラム 3#include <stdio.h> 4 5int main(void){ 6 7 int a,b,max,dot,gaso,tmp,cnt=0,j=1,i; 8 9 getchar(); 10 getchar(); 11 12 scanf("%d",&a); 13 scanf("%d",&b); 14 scanf("%d",&max); 15 16 printf("%d\n",a); 17 printf("%d\n",b); 18 printf("%d\n",max); 19 20 gaso = a * b; 21 22 for(i=0; gaso>i;i++){ 23 24 scanf("%d",&dot); 25 roop = 1; 26 27 while(j==1){ 28 29 if(cnt == 0){ 30 printf("%d\n",dot); 31 tmp = dot; 32 } 33 34 if(tmp == dot){ 35 cnt++; 36 break; 37 38 if(gaso==i+1){ 39 printf("%d\n",cnt); 40 } 41 42 }else{ 43 printf("%d\n",cnt); 44 cnt = 0; 45 } 46 } 47 } 48 49 return 0; 50} 51 52decode.exeのプログラム 53#include <stdio.h> 54 55int main(void){ 56 57 int a,b,max,dot,i,a_cnt=1,b_cnt=0,cnt=0; 58 scanf("%d",&a); 59 scanf("%d",&b); 60 scanf("%d",&max); 61 62 printf("P2\n"); 63 printf("%d",a); 64 printf("%d\n",b); 65 66 printf("%d\n",max); 67 68 while(b_cnt<b){ 69 70 scanf("%d",&dot); 71 scanf("%d",&cnt); 72 73 for(i = 0;cnt>i;i++){ 74 75 if(a_cnt<a){ 76 printf("%d\n",dot); 77 a_cnt++; 78 }else{ 79 printf("%d\n",dot); 80 a_cnt=1; 81 b_cnt++; 82 } 83 } 84 } 85 return 0; 86} 87
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー