2016年2月10日 星期三

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

Zerojudge 首頁的題目中除了 a006, a015, 還有 b681, b682 可以試試哦

// a248. 新手訓練 ~ 陣列應用  , 但以下參考解沒用陣列
#include <iostream>
using namespace std;
int main( )
{
unsigned int a,b,n,i;
 while(cin>>a>>b>>n)
 {
    cout<<a/b<<"."; //整數部份加小數點
    a=(a%b)*10; //模擬直式除法,餘數補0 一直除n
    for(i=0;i<n;i++)
    {
      cout<<a/b;
      a=(a%b)*10;
    }
    cout<<"\n";
  }
    return 0 ;
}

// a263. 日期差幾天
#include <iostream>
#include <cstdlib>     /* abs */
using namespace std;
bool isleap(int y) //是否閏年
{
    return (y % 4 == 0 && y % 100 || y % 400 == 0);
}
// 提示 年份在[0,9999]範圍內  {應是 15821015日之後}
//                        1582104日之前, 須另考慮

int from0(int y, int m, int d) // 0/0/0 起至 y/m/d 的總日數
{
    int md[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int cnt = y * 365;
    cnt += (y - 1) / 4 + 1;
    cnt -= (y - 1) / 100 + 1;
    cnt += (y - 1) / 400 + 1;
    for(int i = 1; i < m; ++i) cnt += md[i];
    if(m > 2 && isleap(y)) ++cnt;
    cnt += d;
    return cnt;
}

int main()
{
    int y, m, d, diff;
    while(cin>>y>>m>>d)
         {
        diff=from0(y,m,d);   //第一個y/m/d 0/0/0 總日期
        cin>>y>>m>>d;
        diff-=from0(y,m,d); // 第二個y/m/d 0/0/0 總日期
        cout<<abs(diff)<<endl;  //印絕對值
    }
    return 0;
}

// a410. 解方程
#include <iostream>
#include <cstdio>  // printf 需用
#include <iomanip> // setprecision , fixed ??
using namespace std;
int main( )
{
int a,b,c,d,e,f;
double g,h,i,x,y;
while(cin >>a>>b >>c >>d >>e >>f)
 {
    g=a*e-b*d;  h=c*e-b*f;  i=a*f-c*d;
    if(g!=0.0)
         {
      x=h/g; // printf("x=%1.2f\n",x);
         cout <<"x=" << fixed << setprecision(2) << x << endl;
      y=i/g; // printf("y=%1.2f\n",y);
         cout <<"y=" << fixed << setprecision(2) << y << endl;
    }
         else if( h==0.0 && i==0.0 ) cout <<"Too many"<<endl;
    else cout <<"No answer"<<endl;  
  }
    return 0 ;
}

// a414. 位元運算之進位篇
#include <stdio.h>  // printf 需用
#include <iostream>
using namespace std;
int main( )
{
unsigned int n;
int cnt;
  while(1)    //while(cin >>n , n) 使用 cin+cout TLE
  {
    scanf("%d",&n);  if(!n) break;
    cnt=0;
    while( n%2 )
         {
      cnt++;   n/=2;
    }
    printf("%d\n",cnt);  // cout << cnt << endl;
  }
  return 0 ;
}

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

// b681: 高中組第一題-山洞探險
#include <iostream>
using namespace std;
int main(void)
{
   int n;
        while(cin >> n)
        {
                if(n<0) cout <<(-n)*2 << endl;
                else cout << 2*n-1 << endl;
        }
        return 0;
}

// b682. 高中組第二題-同學早安
#include <iostream>
using namespace std;
const int day=24*60;
int main(void)
{
   int h1,h2 , m1,m2;
   int t;
        while(cin >> h1 >> m1)
        {
                cin >> h2 >> m2;
                t = ( h2*60 + m2 + day - h1*60 - m1 ) % day;
                cout << t/60 << " " << t%60 << endl;
        }
        return 0;
}

// d010. 盈數、虧數和完全數
#include <cstdio> // scanf+printf
#include <iostream>
using namespace std;
int main()
{
    int i,j,k,n,s;
    while( cin>>n  )//while( scanf("%d",&n)!=EOF )
    {   s=n;
       for(i=1;i<=n/2;i++)
       {
                    if(!(n%i)) s-=i;
          if(s<0) break;
       }
       if(!s) cout << "完全數\n";  //printf("完全數\n");
       else if(s<0) cout << "盈數\n";  //printf("盈數\n");
       else  cout << "虧數\n";  //printf("虧數\n");
    }
    return 0;
}

// d051. 糟糕,我發燒了! -- 板橋高中教學題
#include <iostream> // cout , cin
#include <iomanip>  // setw  , setprecision
using namespace std;  // , fixed
int main( )
{
        int f; 
   double c;
      while(cin >>f)
      {
                // 若宣告 double c =  (f-32)*5/9.0; f太大 c結果會錯{ int * int 溢位 }
//     4 測資點(0%): WA (line:1)  您的答案為: 238609275.889 正確答案為: 1193046452.778
                c  = (f-32)*5.0/9;
                cout << fixed << setprecision(3) << c << endl;
            // cout << int((f-32)*5/9.0*1000+0.5)/1000.0 << endl;
            //cout << fixed << setprecision(3) << (f-32)*5/9.0 << endl;
        }
   return 0 ;
}

// d074. 電腦教室 -- 板橋高中教學題
#include <cstdio>   // scanf , printf
#include <iostream> // cout , cin
using namespace std;
int main()
{
   int n,s;
    while ( cin >> n ) // while (scanf("%d",&n)!=EOF)
    {
                 int m = -1;
       while(n--)
                 {
         cin >> s;  //scanf("%d",&s);
         if(s>m) m=s;
       }
       cout << m << endl;  //printf("%d\n",m);
    }
    return 0;
}

/*   d277. 矩形對角線
若有偶數個,例4,交叉的對角線已排了 k 個,剩下還沒排放的一樣多
1001   若有奇數個,例3,交叉的對角線已 k 個,剩下還沒排放的,比k1
0110       101
0110       010
1001       101
*/

#include<iostream>
using namespace std;

int main()
{
   int k;
   while(cin>>k)
   {
      cout<< k-(k%2) << endl;
   }
   return 0;
}

//   d299. 程式設計師的面試問題
// 要跑程式不屬於簡易題, 但因只有一組解答
// 用手算推導可能較快,先從 TEN EN 一定是 50 開始
// FO 加上進位的數字後 會成為 SI,所以F+1=S,又O一定是9I1
// …快導出了,自己想想
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
        int d[]={0,1,2,3,4,5,6,7,8,9};    //char c[11]="0123456789"; 
        // d[ 0 1 2 3 4 5 6 7 8 9 ]
        //    F O R T Y E N S I X
//   cout<<"29786 + 850 + 850 = 31486"<<endl;
  
//   int cnt=0;
   do
        {
     // ++ cnt;
     //cout << c << endl;  印出所有排列
   //       if(cnt%1000==0) cout <<cnt <<":"<< c << endl;
      if( d[6]!=0 || d[5]!=5 || d[1]!=9 || d[8]!=1 ) continue;
      if( d[0]+1 != d[7] ) continue;
      if( d[2]+d[3]+d[3] == 19+d[9] ) break;
   } while( next_permutation(d,d+10) );  // (c,c+10)
 //  cout <<cnt <<":"<< c << endl;
        cout << d[0] << d[1] << d[2] << d[3] << d[4] ;
       cout << " + " << d[3] << d[5] << d[6] ;
       cout << " + " << d[3] << d[5] << d[6] ;
       cout << " = " << d[7] << d[8] << d[9] << d[3] << d[4] << endl;
  return 0;
}


0 意見:

張貼留言