2017年12月31日 星期日

[UVa]10071 - Back to High School Physics(C++)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=1012&mosmsg=Submission+received+with+ID+20553865


Q10071: Back to High School Physics

某一個粒子有一初速度和等加速度。假設在 t 秒後此粒子的速度為 v ,請問這個粒子在 2t 秒後所經過的距離是多少。
Input
每組測試資料1列,有2個整數,分別代表 v(-100 <= v <=100)和 t(0 <= t <= 200)。
Output
對每組測試資料請輸出這個粒子在 2t 秒後所經過的距離是多少。
Sample input
0 0
5 12
Sample Output
0
120

以上翻譯題目:
http://luckycat.kshs.kh.edu.tw/homework/q10071.htm


#include <iostream>
using namespace std;
 
int main() {
 // 10071 - Back to High School Physics
 int t, v;
 while(cin >> t >> v)
 {
  cout << 2*t*v << endl;
 }
 return 0;
}

[UVa]490 - Rotating Sentences(C++)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=6&problem=431&mosmsg=Submission+received+with+ID+20553703

Q490: Rotating Sentences

在這個問題中你必須將數列文字往順時針方向旋轉90度。也就是說將原本由左到右,由上到下的句子輸出成由上到下,由右到左。
Input and Output
輸入最多不會超過100列,每列最多不會超過100個字元。
合法的字元包括:換行,空白,所有的標點符號,數字,以及大小寫字母。(注意:Tabs並不算是合法字元。)
最後一列輸入必須垂直輸出在最左邊一行,輸入的第一列必須垂直輸出在最右邊一行。
請參考sample intput/output。
Sample Input
Rene Decartes once said,
"I think, therefore I am."
Sample Output
"R
Ie
 n
te
h 
iD
ne
kc
,a
 r
tt
he
es
r 
eo
fn
oc
re
e 
 s
Ia
 i
ad
m,
. 
"



以上題目翻譯:
http://luckycat.kshs.kh.edu.tw/homework/q490.htm

#include <iostream>
using namespace std;

int main() {
// 490 - Rotating Sentences
string text;
string t[100][100];//使用者輸入
string c[100][100];//轉置矩陣
string r[100][100];//結果
int count[100];
int col = 0;//計算有幾行的文字
int max = -1;//紀錄最長的句子有多少字
//矩陣歸零
for(int i = 0;i < 100;i++)
{
for(int j = 0;j < 100;j++)
{
t[i][j] = " ";
c[i][j] = " ";
r[i][j] = " ";
}
}
//每次先處理一行句子
while(getline(cin, text))
{
count[col] = text.size();
if(count[col] > max)
{
max = count[col];
}
for(int i = 0;i < text.size();i++)
{
t[col][i] = text[i];
}
col++;
}
//轉置矩陣
for(int i = 0;i < col;i++)
{
for(int j = 0;j < max;j++)
{
c[j][i] = t[i][j];
}
}
//把陣列c移位成結果--陣列r
for(int j = 0;j < max;j++)
{
int cc = col-1;
for(int i = 0;i < col;i++)
{
r[j][cc] = c[j][i];
cc--;
}
}

//輸出陣列r
for(int j = 0;j < max;j++)
{
for(int i = 0;i < col;i++)
{
if(i != 0)
{
cout << r[j][i];
}
else
{
cout << r[j][i];
}
}
cout << endl;
}
return 0;
}

[UVa]10035 - Primary Arithmetic(C++)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=976

Q10035: Primary Arithmetic

在小學時我們都做過加法的運算,就是把2個整數靠右對齊然後,由右至左一位一位相加。如果相加的結果大於等於10就有進位(carry)的情況出現。你的任務就是要判斷2個整數相加時產生了幾次進位的情況。這將幫助小學老師分析加法題目的難度。
Input
每一列測試資料有2個正整數,長度均小於10位。最後一列有2個0代表輸入結束。
Output
每列測試資料輸出該2數相加時產生多少次進位,請參考Sample Output。注意進位超過1次時operation有加s
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.


以上題目翻譯:
http://luckycat.kshs.kh.edu.tw/homework/q10035.htm





#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <cmath>
using namespace std;
 
int main() {
 // 10035 - Primary Arithmetic
 string n1, n2;//first num, second num
 while(cin >> n1 >> n2)
 {
  if(n1 == "0" && n2 == "0")
  {
   break;
  }
  else
  {
   int n1_len = n1.size(); //n1的長度
   int n2_len = n2.size(); //n2的長度
   int c = 0;//紀錄進位
   int result = 0; //進位數量
   while(n1_len != 0 && n2_len != 0)
   {
    int x; //相加結果
    int i_n1 = n1[n1_len-1]-'0';
    int i_n2 = n2[n2_len-1]-'0';
    x = i_n1 + i_n2 + c;
    c = 0;
    if(x >= 10)
    {
     c = floor(x/10);
     result++;
    }
    n1_len--;
    n2_len--;
   }
   //如果兩數字長度不同,也要繼續相加計算進位
   if(n1_len > n2_len)
   {
    while(n1_len != 0)
    {
     int x;
     x = n1[n1_len-n2_len-1]-'0' + c;
     c = 0;
     if( x >= 10)
     {
      c = floor(x/10);
      result++;
     }
     n1_len--;
    }
   }
   else if(n2_len > n1_len)
   {
    while(n2_len != 0)
    {
     int x;
     x = n2[n2_len-n1_len-1]-'0' + c;
     c = 0;
     if(x >= 10)
     {
      c = floor(x/10);
      result++;
     }
     n2_len--;
    }
   }
   //輸出
   if(result == 0)
   {
    cout << "No carry operation." << endl;
   }
   else if(result == 1)
   {
 
    cout << result << " carry operation." << endl;
   }
   else
   {
    cout << result << " carry operations." << endl;
   }
  }
 }
 return 0;
}

2017年12月30日 星期六

[UVa]注意事項

1. Java上傳要用Main

[C_AR74-中] 學生資料搜尋程式(C++)

[C_AR74-中] 學生資料搜尋程式

問題描述 :
請撰寫一支二維陣列學生資料搜尋程式,學生資料內容如下:
陣列內容
學號姓名系別
123TomDTGD
456CatCSIE
789NanaASIE
321LimDBA
654WonFDD
提示 :
(1). 可以選擇搜尋欄位 (1) 學號 (2) 姓名 (3) 系別。
(2). 輸入關鍵字。
輸入說明 :
輸入一個數字 N ,代表有 N 個測資,每個測資一行,每一行有兩個參數,第一個參數代表第幾個欄位,第二個參數代表那個欄位的 value
輸出說明 :
把找到的那個學生依序印出他的學號、姓名、系別,兩兩欄位間有一個空白
範例 :

輸入範例輸出範例
1
1 123
123 Tom DTGD

  1. #include <iostream>  
  2. using namespace std;  
  3.    
  4. int main() {  
  5.     // [C_AR74-中] 學生資料搜尋程式  
  6.     string data[5][3] =   
  7.     {  
  8.     {"123""Tom""DTGD"},  
  9.     {"456""Cat""CSIE"},  
  10.     {"789""Nana""ASIE"},  
  11.     {"321""Lim""DBA"},  
  12.     {"654""Won""FDD"}  
  13.     };  
  14.     int N;  
  15.     cin >> N;  
  16.     cin.ignore();   
  17.     for(int i = 0;i < N;i++)  
  18.     {  
  19.         int F;//欄位  
  20.         //(1) 學號 (2) 姓名 (3) 系別  
  21.         string ID;  
  22.         cin >> F >> ID;  
  23.         for(int j = 0; j < 5;j++)  
  24.         {  
  25.             if(ID == data[j][F-1])  
  26.             {  
  27.                 cout << data[j][0] << " " << data[j][1] << " " << data[j][2] << endl;   
  28.             }  
  29.         }  
  30.     }  
  31.     return 0;  
  32. }  

[ITSA Basic]題目1. 矩陣數字顯示(C++)

題目1. 矩陣數字顯示

問題描述
現有一可顯示四位數字的 LED 燈一組,可根據輸入的四位數字顯示數字。比如說,
由鍵盤輸入 1234 ,則可在螢幕上顯示
         C_ST53-1.JPG
若輸入 0789 ,則顯示
C_ST53-2.JPG
輸入格式 
輸入格式為 四個整數。
輸出格式 
輸出值為輸入值的矩陣數字。
Example
Sample Input:
Sample Output:
0789

C_ST53-2.JPG
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. using namespace std;  
  5.   
  6. int main() {  
  7.     // 題目1. 矩陣數字顯示  
  8.     string num[10][5];  
  9.     num[0][0] = "*****";        
  10.      num[0][1] = "*   *";        
  11.      num[0][2] = "*   *";        
  12.      num[0][3] = "*   *";        
  13.      num[0][4] = "*****";        
  14.      num[1][0] = "    *";        
  15.      num[1][1] = "    *";        
  16.      num[1][2] = "    *";        
  17.      num[1][3] = "    *";        
  18.      num[1][4] = "    *";        
  19.      num[2][0] = "*****";        
  20.      num[2][1] = "    *";        
  21.      num[2][2] = "*****";        
  22.      num[2][3] = "*    ";        
  23.      num[2][4] = "*****";        
  24.      num[3][0] = "*****";        
  25.      num[3][1] = "    *";        
  26.      num[3][2] = "*****";        
  27.      num[3][3] = "    *";        
  28.      num[3][4] = "*****";        
  29.      num[4][0] = "*   *";        
  30.      num[4][1] = "*   *";        
  31.      num[4][2] = "*****";        
  32.      num[4][3] = "    *";        
  33.      num[4][4] = "    *";        
  34.      num[5][0] = "*****";        
  35.      num[5][1] = "*    ";        
  36.      num[5][2] = "*****";        
  37.      num[5][3] = "    *";        
  38.      num[5][4] = "*****";        
  39.      num[6][0] = "*****";        
  40.      num[6][1] = "*    ";        
  41.      num[6][2] = "*****";        
  42.      num[6][3] = "*   *";        
  43.      num[6][4] = "*****";        
  44.      num[7][0] = "*****";        
  45.      num[7][1] = "    *";        
  46.      num[7][2] = "    *";        
  47.      num[7][3] = "    *";        
  48.      num[7][4] = "    *";        
  49.      num[8][0] = "*****";        
  50.      num[8][1] = "*   *";        
  51.      num[8][2] = "*****";        
  52.      num[8][3] = "*   *";        
  53.      num[8][4] = "*****";        
  54.      num[9][0] = "*****";        
  55.      num[9][1] = "*   *";        
  56.      num[9][2] = "*****";        
  57.      num[9][3] = "    *";        
  58.      num[9][4] = "    *";    
  59.      string N;  
  60.        
  61.      while(cin >> N)  
  62.      {  
  63.         char c[4];  
  64.         strcpy(c, N.c_str());  
  65.         for(int i = 0;i < 5;i++)  
  66.         {  
  67.             for(int j = 0;j < 4;j++)  
  68.             {  
  69.                 if(j != 0)  
  70.                 {  
  71.                     cout  << " " << num[c[j]-'0'][i];  
  72.                 }  
  73.                 else  
  74.                 {  
  75.                     cout  << num[c[j]-'0'][i];  
  76.                 }  
  77.             }  
  78.             cout << endl;  
  79.         }  
  80.      }  
  81.     return 0;  
  82. }