2013年11月18日 星期一

分子量計算(2)

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
 map <char,int> pt;
 int n,m;
 char ch,pch;
 cin >>n;
 char e;
 int qe;
 for (int i=0;i<n;i++){
  cin >> e >> qe;
  pt[e]=qe;
 }
 
 //'x'
 cin >>m;
 for (int i=0;i<m;i++){
  int q=0;
  while(cin>>ch && ch!='x'){
   if (ch >='2' && ch<='9'){
    q+=pt[pch]*(ch-'0'-1);
   }
   else{
    q+= pt[ch];
    pch=ch;
   }   
  }
  cout << q;
 }
 system("pause");
 return 0;
}

平方取中法

輸入: 3 位數
利用平方取中法產生亂數
規則: 取萬位數到十位數
輸出: 產生到第幾個數的時候開始出現重複?

範例
輸入:123
輸出: 14
計算過程
1 512
2 214
3 579
4 524
5 457
6 884
7 145
8 102
9 40
10 160
11 560
12 360
13 960
14 160
15 560
16 360
17 960
18 160
19 560

平方取中法=>到第幾個數開始出現重複?

#include <iostream>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
int main(){
 int x;
 map<int,bool> m;
 cin >> x;
 while( (x=((x*x)/10)%1000) && !m[x]){  
  m[x]=true;
 } 
 cout << m.size()+1;
 system("pause");
}
/*
 123 => 14
 172 => 17


輸入: 3 位數
利用平方取中法產生亂數
規則: 取萬位數到十位數
輸出: 產生到第幾個數的時候開始出現重複?
範例
輸入:123
輸出: 14
計算過程
1512
2214
3579
4524
5457
6884
7145
8102
940
10160
11560
12360
13960
14160
15560
16360
17960
18160
19560
*/

分子量計算

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
  map <char,int> pt;
  char ch,pch;
  pt['H']=1;
  pt['O']=16;
  pt['C']=12;
  //'x'
  int q=0;
  while(cin>>ch && ch!='x'){
   if (ch >='2' && ch<='9'){
  q+=pt[pch]*(ch-'0'-1);
   }
   else{
  q+= pt[ch];
  pch=ch;
   }   
  }
  cout << q;
  system("pause");
return 0;
}
/*
以x標示分子式的結束
CH3COOHx  =>60
CH3CH2COOHx => 74

題目二
輸入資料: 
3 <= 構成原子數量
H 1
O 16
C 12
2 <= 分子式數量
CH3COOHx
CH3CH2COOHx
輸出
60
74
*/

平方取中

#include <iostream>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
int main(){
 int samplesToProduce,x;
 int newseed;
 vector<int> storage;
 cin >> x;
 cin >> samplesToProduce;
 x=x*x; 
 for (int i = 0; i < samplesToProduce; ++i)
 {
        //select the middle six digits for the next seed
  cout << setw(6);
  cout << x << "\t";
        newseed = (x / 10) % 1000;
  cout << setw(3);
  cout << newseed << endl;
  storage.push_back(newseed);        
        x = newseed * newseed;
 }
 system("pause");
}
/*
123 10
512
214
579
524
457
884
145
102
40
160
 
455 100
172 100
*/

5 的 100次方 mod 7 (更精簡版)

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
 map<int,bool> m; //記錄已經出現的餘數
 vector<int> v;  //依序記錄餘數
 int a,b,c,d;  //a被除數, b除數, c次方, d餘數
 cin >> a >> b >> c;
 d = a%b;  //處理 a<b 的情形
 while(!m[d]){  
  m[d]=true;
  v.push_back(d);
  d=(d*a)%b; 
 } 
 c = c%m.size(); 
 cout<<v[c-1];
 system("pause");
 return 0;
}
/*
 5 7 1 => 5 //(5^1)%7, 5%7
 5 7 100 => 2 //(5^100)%7
*/

2013年11月11日 星期一

5 的 100次方 mod 7

#include <iostream>
#include <map>
using namespace std;
int main(){
map<
int,bool> m; //記錄已經出現的餘數
int a,b,c,d;
cin >> a >> b>>c;
d = a;
while(!m[d]){
m[d]=
true;
d=(d*a)%b;
}
//cout << m.size();
c = c%m.size();
d = a;
for(int i=0;i<c-1;i++){
d=(d*a)%b;
}
cout<<d;

system(
"pause");
return 0;
}
/*
5 7 1 => 5
5 7 100 => 2
*/

循環節的長度

#include <iostream>
#include <map>
using namespace std;int main(){
map<
int,bool> m;
int a,b;
cin >> a >> b;
a=a%b;
while(!m[a]){
m[a]=
true;
a=(a*10)%b;
}
cout << m.size();
system(
"pause");
return 0;
}
/*
注意用不是用商判斷,用餘數判斷 14143 99999 => 5
1 7 => 7
1231234 9999999 => 7
Excel
=QUOTIENT(B2,99999)
=MOD(B2,99999)
*/