2016年2月8日 星期一

二信C++寒假練習-zerojudge之 四

// a015. 矩陣的翻轉
#include <iostream>
using namespace std;
int main( )
{
  int m,n,r,c;
  int a[100][100];
  while ((cin>>m>>n))
  {
          for(r=0;r<m;r++)
          {  for(c=0;c<n;c++)
             {
               cin>>a[r][c];
             }
          }
          for(c=0;c<n;c++)
          {  for(r=0;r<m;r++)
             {
               cout<<(r==0?"":" ")<<a[r][c];
             }
             cout <<endl;
          }
  }
  return 0 ;
}

// a020. 身分證檢驗 ,本題不需檢查輸入長度、及格式是否符合,純計算檢查碼是否正確
#include <iostream>
using namespace std;
int main( )
 {
    int seq[]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};
    string pid;
    while ( cin>>pid ) {
       int L=seq[int(pid[0]-'A')];  //字母轉二位數
       int sum=L/10+(L%10)*9;       // 第1位*1,第2位*9
       // 接著8個數字分別 *8 ~ * 1
       for(int i=1;i<=8;i++) sum+=( (pid[i]-'0')*(9-i) ) ;
       sum+=(pid[9]-'0');  //最後檢查碼 *1
       if(sum%10) cout<<"fake"<<endl; // ÷10的餘數非 0
       else cout<<"real"<<endl;
    }
    return 0 ;
}

// a022. 迴文
#include <iostream>
using namespace std;
int main( )
{
  string str;
  int i,n,s;
   while( cin>>str )
  {
     s=str.size();
     n=s/2;  //字串長度的一半
     s--;
     for(i=0;i<=n;i++)
          {
         if(str[i]!=str[s-i])   break;
     }
     if(i>n) cout<<"yes"<<endl;
     else cout<<"no"<<endl;
   }
   return 0;
}

// a024. 最大公因數(GCD)
#include <iostream>
using namespace std;
int main( )
{
  int a,b,q,r;
   while( cin>>a>>b )
  {
     r=a%b;
     while(r) {
       a=b;
       b=r;
       r=a%b;
     }
     cout<<b<<endl;
   }
   return 0;
}

// a034. 二進位制轉換
#include <iostream>
#include <vector>
using namespace std;
int main( )
{
    int k;
    while(cin >>k){
      vector <char> s; //動態陣列,以push_back加0或1
      while( k ) 
                {       
        s.push_back((k%2)?'1':'0');
        k/=2;
      }
      for(k=s.size()-1;k>=0;k--) cout <<s[k]; 
                cout<<endl;
    }
    return 0 ;
}

// a038. 數字翻轉
#include <iostream>
using namespace std;
int main( )
{
  int n,b;
  while( cin>>n )
  {
    b=0;
     while(n)
          {
       b=b*10+n%10;
       n/=10;       
     }
    cout<<b<<endl;    
  }
    return 0 ;
}

// a054. 電話客服中心
#include <iostream>
using namespace std;
int main( )
 {
    int seq[]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};
    string pid;
    int i;
    int ladd[26];  //字母要加的總和 先算出
    for(i=0;i<26;i++)
                ladd[i]=seq[i]/10 + seq[i]%10*9;
// 開始讀資料
    while ( cin>>pid )
         {
       int sum=0;
       for(int i=0;i<=7;i++) sum+=( (pid[i]-'0')*(8-i) ) ;
       sum+=(pid[8]-'0');
       for(i=0;i<26;i++)
                       if( (sum+ladd[i])%10==0 ) cout<<char('A'+i);   // 26個字母各試一次
       cout<<endl;
    }
    return 0 ;
}

// a065. 提款卡密碼
#include <iostream>
using namespace std;
int main( )
 {
    string pid;
    int i;
    while ( cin>>pid )
         {
      for(i=0;i<6;i++)
      {  if(pid[i]>pid[i+1]) cout<<int(pid[i]-pid[i+1]);
        else cout<<int(pid[i+1]-pid[i]);
      }
      cout<<endl;
    }
    return 0 ;
}

// a149. 乘乘樂
#include <iostream>
using namespace std;
int main()
{
  long long p; //有可能 > int ? 好像不會,使用int即可
  int i,n;
  string inp;
  cin >>n;
  while(n--)
  {
    cin>>inp;
    p=1;
    for(i=0; i<inp.size(); i++)
      p=p*(inp[i]-'0');
    cout<<p<<endl;
  }
  return 0;
}

// a215. 明明愛數數
#include <iostream>
using namespace std;
int main( )
 {
    int i,m,n;
    long long s;
    while ( cin>>n>>m )
         {     // m , n 有可能為負 ?
      i=1;s=n; //第1個數
      while(s<=m) // > m 才停
                {
                        ++n;
         s += n;
         ++i;
      }
      cout<<i<<endl;
    }
    return 0 ;
}

// a216. 數數愛明明
#include <iostream>
using namespace std;
int main( )
 {
    int i,n;
    long long f[30000],g[30000];
    f[0]=g[0]=1;
    int m=1;
    while ( cin>>n )
         {
      if(n>m)
         for(i=m;i<n;i++)
         {
          f[i]=(i+1)+f[i-1];
          g[i]=f[i]+g[i-1];
         }
       cout<<f[n-1]<<" "<<g[n-1]<<endl;
    }
    return 0 ;
}

// a224. 明明愛明明  ,只有26字母,只要成對即可
#include <iostream>
using namespace std;
int main( )
 {
    int i,k,n;
    string s;
    bool m[26];
    while ( cin>>s )
         {
      for(i=0;i<26;i++) m[i]=false; // 26個字母做記號用
      n=s.size();
      for(i=0;i<n;i++)
      {
         if(s[i]>='A' && s[i]<='Z')   m[s[i]-'A'] = !m[s[i]-'A'];
         if(s[i]>='a' && s[i]<='z')   m[s[i]-'a'] = !m[s[i]-'a'];      
      }                     
      for(i=0,k=0;i<26;i++) if(m[i]) k++;
      if(k>1) cout <<"no..."<<endl;
      else  cout <<"yes !"<<endl;
    }
    return 0 ;
}


2016年2月5日 星期五

二信C++寒假練習-zerojudge之 三

// a006. 一元二次方程式
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
   int a, b, c ;
   while( cin >> a >> b >> c )
   {
           int d = b*b - 4*a*c;
      if( d < 0 )
          cout << "No real root\n";
      else if( d == 0 )
          cout << "Two same roots x=" << -b/(2*a) << endl;
      else
          cout << "Two different roots x1=" << (-b + sqrt(d)) / (2*a)
                             << " , x2=" << (-b - sqrt(d)) / (2*a) << endl;
    }
    return 0;
}

//   a009. 解碼器
#include <iostream>
#include <cstring>
using namespace std;
int main( )
{
        string s;
        while( getline(cin,s) )
        {
      int i,n=s.size();
     for(i=0;i<n;i++)
                        cout<<(char (s[i]-7));
                cout<<endl;
        }
   return 0 ;
}

// d126: 一、牧場圍欄   NOI冬令營    計算矩形周長
#include <iostream>
using namespace std;
int main( )
{
   int a,b;
   while( cin >> a >> b)
     cout << a+a+b+b << endl;
   return 0;
}

// d127 牧場面積 -- NOI冬令營    給周長(偶數)計算矩形最大面積
#include <iostream>
using namespace std;
int main( )
{
   long long  l,a,b;
   while( cin >> l)
   {
               l/=2; a=l/2; b=l-a;
           cout <<a*b  << endl;
        }
   return 0;

// d881. 作業苦多
#include <iostream>
using namespace std;
int main( )
{
     int i,d ;
   
    while (cin >>d ) //      cout <<1275+19600*d <<endl ;  //因固定 50 項,可事先求出公式
    {
            int s=1 , a=1 , d2=1; // 第1項 1,第2項與第1項差為1,第3項與第2項顯差為1+d
            for(i=1; i<50; ++i)
            {
                    a = a+d2; 
                    s = s + a;
                    d2 += d;   // 下一公差 d2
            }
            cout << s << endl;
        }
   
  return 0 ;
}

// d579: 兩條線
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
  double n,b;
  while( cin>>n )
  {
     cout << fixed << setprecision(4) << "|"   <<n   << "|=" ;//printf("|%1.4f|=",n);
      if(n<0)n=-n;
     cout << fixed << setprecision(4)  << n << endl ; // printf("%1.4f\n",n);
  }
    return 0 ;
}

//   d581. 三條線    輸入 t , 印出 t 列的 「 =_=||| 」
#include <iostream>
using namespace std;
int main( )
{
   int t ;
   while(cin >> t)
     while(t--)
             cout << "=_=|||" << endl;
   return 0;
}

//   d532. 文文的求婚 (三) -- 板橋高中教學題
#include<iostream>
using namespace std;

bool isleap(int y)
{
  if( (y%4==0 && y%100 ) || (y%400==0) ) return true;
  else return false;
}

int main()
{
 int a,b,y,cnt;
 while(cin>>a>>b)
 {
    cnt=0;
    while(!isleap(a)) a++;
    for(y=a;y<=b;y+=4)
    {
      if(isleap(y)) cnt++;
    }
    cout<<cnt<<endl;
 }
  return 0;
}

//   d559. 班號
#include <iostream>
using namespace std;
string s="'C' can use printf(\"%d\",n); to show integer like ";
int main()
{
   int n;
    while ( cin>>n)
    {
       cout<<s<<n<<endl;
    }
       
    return 0;
}




二信C++寒假練習-zerojudge之二

// b761: 搶30 兩人輪流 疊積木,可放1~3個,疊第 N 個的 fat 、否則 egg
#include <iostream>
using namespace std;
int main()
{      // 程式敘述  n為 4倍則 egg
  int n;
  while(cin>>n)
    cout << (n%4==0?"egg":"fat") << endl;
  return 0;
}

//d049. 中華民國萬歲! -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int y; 
  while(cin >>y)
  {
    cout << y-1911<< endl;
  }
   return 0 ;
}

//d050. 妳那裡現在幾點了? -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
    int h; 
  while(cin>>h)
  {
    cout << (h+9)%24 << endl;
  }
  return 0 ;
}

// d058. BASIC 的 SGN 函數 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int n;
  while( cin>>n )
  {
    cout << (n>0)-(n<0) << endl;
  }
   return 0 ;
}


// d060. 還要等多久啊? -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int m;
  while( cin >> m )
  {
    cout << (60-(m+35)%60)%60 << endl;
  }
  return 0 ;
}

// d063. 0 與 1 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int k; 
  while(cin>>k)
  {
    cout<<1-k<<endl;
  }
  return 0 ;
}

// d064. ㄑㄧˊ 數? -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int i;
   while (cin >>i)
   {
       cout << (i%2?"Odd":"Even" )<< endl;
   }
  return 0 ;
}

//    d065. 三人行必有我師 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
   int a,b,c;
    while (cin >>a>>b>>c)
    {
       cout << (a>=b ? ((a>=c)?a:c) : ((b>=c)?b:c) ) << endl;
    }
  return 0 ;
}

// d066. 上學去吧! -- 板橋高中教學題  7:30含至17:00不含 為 At School
#include <iostream>
using namespace std;
int main( )
{
  int h,m;
    while ( cin >> h >> m )
    {
       cout << ( (h==7&&m>=30 || h>7&&h<17)?"At":"Off") << " School\n" ;
    }
  return 0 ;
}

//    d067. 文文的求婚--續集 (1 行版) -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int y;
    cin >>y;
// string s =   (!(y%400)||(y%100)&&(!(y%4))) ? "leap" : "normal"  ;
// 或 if(y%400==0 || (y%100 && y%4==0) ) s="leap"; else s="normal";
// cout << "a " << s << " year\n" ;
    cout << "a " << ( (!(y%400)||(y%100)&&(!(y%4))) ? "leap" : "normal" ) << " year\n" ;
  return 0 ;
}

// d068. 該減肥了! -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int w;
    while (cin >>w)
    {
       cout << (w>50?w-1:w) << endl;
    }
  return 0 ;
}

// d069. 文文的求婚--續集 (n 行版) -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int n,y;
    cin >> n;
       while (n--)
    {
             cin >> y;
       cout << "a " << ( (!(y%400)||(y%100)&&(!(y%4))) ? "leap" : "normal" ) << " year\n" ;
    }
  return 0 ;
}

// d070. 文文的求婚--續集 (0 尾版) -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int y;
       while ( cin >> y , y )
    {    // 或在迴圈內 if(y==0) break;
       cout << "a " << ( (!(y%400)||(y%100)&&(!(y%4))) ? "leap" : "normal" ) << " year\n" ;
    }
  return 0 ;
}

// d071. 文文的求婚--續集 (EOF版) -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int y;
       while ( cin >> y )
    {
       cout << "a " << ( (!(y%400)||(y%100)&&(!(y%4))) ? "leap" : "normal" ) << " year\n" ;
    }
  return 0 ;
}

// d072. 文文的求婚--續集 (CASE版) -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int y,n,i;
        cin >> n;
       for(i=1;i<=n;i++)
   {
          cin >> y;
     cout << "Case " << i << ": a " << ( (!(y%400)||(y%100)&&(!(y%4))) ? "leap" : "normal" ) << " year\n" ;
   }
  return 0 ;
}

// d073. 分組報告 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int n;
        while (cin >> n)
    {
        cout << (n+2)/3 << endl;
    }
  return 0 ;
}

// d461. 班際籃球賽 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
    int n;
    while(cin>>n) // while 內只有一行可不加大括號
     cout<<n-1<<endl;
  return 0 ;
}

// d483. hello, world -- Wikipedia
#include <iostream>
using namespace std;
int main()
{
    cout <<"hello, world" <<endl ;
    return 0;
}

// d498. 我不說髒話 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
  int n;
    while(cin>>n)
    {
       while(n--)
         cout<<"I don't say swear words!"<<endl;
    }   
  return 0 ;
}

// d827. 買鉛筆 -- 板橋高中教學題
#include <iostream>
using namespace std;
int main( )
{
 int n;
  while( cin>>n )
    cout<<( (n/12)*50+(n%12)*5 )<<endl;    
  return 0 ;
}