2014年6月4日 星期三

//利用 setw 對齊例
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << setw(3) << j << 'x' << i << '=' << setw(2) << j*i;
}

cout << endl;
}
}
return 0;
}


  1. //位數計算
  2. #include <iostream>  
  3. #include <cmath>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.     double a,b;  
  8.     while (cin >> a>>b)  
  9.     {  
  10.         cout << (int)(b*log10(a))+1 << endl;  
  11.     }  
  12.     return 0;  
  13. }  

  1. //  小數點處理例
  2. #include <iostream>  
  3. #include <cmath>  
  4. #include <iomanip>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     int x1,y1,x2, y2;  
  9.     while (cin >> x1 >> y1 >> x2 >> y2)  
  10.     {  
  11.         double ans = sqrt((double)(x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));  
  12.         cout.setf(ios::fixed, ios::floatfield);  
  13.         cout.precision(3);  
  14.         cout << ans << endl;  
  15.     }  
  16.     return 0;  
  17. }  

2014年5月30日 星期五

//故障計算機
#include <iostream>
using namespace std;
int main()
{
     int n;  
     while (cin >> n)
     {
         if (n<=9)
            cout <<'|' <<"_______" << n <<'|' << endl;
         else if (n<=99)
            cout <<'|' << "______" << n <<'|' << endl;
         else if (n<=999)
             cout <<'|' << "_____" << n<<'|' << endl;
         else if (n<=9999)
             cout <<'|' << "____" << n<<'|' << endl;
         else
         {
             n =  n%10000;
             if (n<=9)
                cout <<'|' <<"____000" << n <<'|' << endl;
             else if (n<=99)
                cout <<'|' << "____00" << n <<'|' << endl;
             else if (n<=999)
                 cout <<'|' << "____0" << n<<'|' << endl;
             else
                 cout <<"|____" <<  n<<'|' << endl;
          }
       }  
     system("PAUSE");
     return 0;
}

// 大數相加、比大小
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b;
while (cin >> a >> b)
{
int ar[20], br[20], cr[20],dr[20];
memset(ar, 0, 20 * sizeof(ar[0]));
memset(br, 0, 20 * sizeof(br[0]));
memset(cr, 0, 20 * sizeof(cr[0]));
memset(dr, 0, 20 * sizeof(dr[0]));
int j = 0;
for (int i = a.length() - 1; i >= 0; i--)
{
ar[j] = a[i] - 48;
j++;
}
j = 0;
for (int i = b.length() - 1; i >= 0; i--)
{
br[j] = b[i] - 48;
j++;
}

//subtract
for (int i = 19; i >= 0; i--)
dr[i] = ar[i] - br[i];
for (int i = 0; i <= 18; i++)
if (dr[i] < 0)
{
dr[i + 1] = dr[i + 1] -1;
dr[i] = dr[i] + 10;
}
int i = 19;
while (dr[i] == 0) i--;
if (dr[i] > 0)
cout << "A ";
else if (dr[i] < 0)
cout << "B ";
else
cout << "  ";

//plus
for (int i = 19; i >= 0; i--)
cr[i] = br[i] + ar[i];
for (int i = 0; i <= 18; i++)
if (cr[i] >= 10)
{
cr[i + 1] = cr[i + 1] + cr[i] / 10;
cr[i] = cr[i] % 10;
}

i = 19;
while (cr[i] == 0) i--;
while (i >= 0)
{
cout << cr[i];
i--;
}
cout << endl;
}
return 0;
}