|
2010年8月19日
f(n) 是正整數n 的各位數字之和。如果 f(n) 是1位數那么n的數根就是f(n),否則f(n) 的數根就是n 的數根。舉例說明:987的數根是6 (9+8+7=24 2+4=6)。你的任務是找出這樣表達式的數根: A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A2 + A1。
輸入包含K個測試樣例。輸入第一行會給出 K (1<=K<=5)。每個測試樣例占一行。這一行的第一個數是一個正整數N (N<=1000)。 接著是N個非負整數 (序列 A)。 均不超過109。
數根(百度知道): http://zhidao.baidu.com/question/29789035.html對于求余數的問題是我一開始就忽略了的地方,后來看了HPF的博客,才知道有這樣的技巧: (A+B)mod C = (A mod C) + (B mod C) (AB) mod C = (A mod C) × (B mod C) C編譯器 , 0MS 0KB , 如果你把前兩句定義變量的語句對調,將會耗費 20MS 的時間
#include <stdio.h> int main() { int i=0,j,K,N,temp,m; long int A; scanf("%d",&K); for (; i<K; i++) { scanf("%d",&N); m=1; A=0; for (j=0; j<N; j++) { scanf("%d",&temp); temp%=9; m=(m*temp)%9; A=(A+m)%9; } printf("%d\n",(A+8)%9+1); } return 0; }
下面的代碼沒有AC,沒有AC的原因是 PE on test 8, 注意,是第八組。我難以理解, 希望大牛們指教!
#include <iostream> #include <stdio.h> using namespace std;
int main() { __int64 x,left,right,mid; scanf("%I64d",&x); left=0; right=x+1; while ((left+1)<right) // 二分查找 { mid=(left+right)/2; if(mid*mid<=x) left=mid; else right=mid; } printf("%I64d",left); return 0; }
2010年8月18日
唉,半年沒有切題的后果就是這道水題寫了整整3個小時。。。 題目的要求是輸入若干組顏色數據,前16組是目標組,后面剩下的都是用來嘗試與其映射的,用后面與前面的一次匹配,各循環16次,各自最小的D的相應映射組就是我們要的結果。 C++ 編譯器 ,220K 0MS
#include<iostream> #include<climits> // 為第一組數據運算做的約束 using namespace std; int RGB[16][3],in[3],out[3]; // in 用來接收數據, out 用來暫存映射正確的數據
int main(){ for (int i=0; i<16; i++) // 輸入的數據中前16組是 target set cin>>RGB[i][0]>>RGB[i][1]>>RGB[i][2]; while (1) { cin>>in[0]>>in[1]>>in[2]; // 開始接收映射組 if (in[0] == -1) break; int MIN=INT_MAX; // 2147483647 for (int i=0; i<16; i++) { int sum=(RGB[i][0]-in[0])*(RGB[i][0]-in[0])+ (RGB[i][1]-in[1])*(RGB[i][1]-in[1])+ (RGB[i][2]-in[2])*(RGB[i][2]-in[2]); // 不需要開方,開方易產生誤差 if (sum < MIN) { out[0]=RGB[i][0]; out[1]=RGB[i][1]; out[2]=RGB[i][2]; MIN=sum; // 最小的即映射成功 } } cout<<"("<<in[0]<<","<<in[1]<<","<<in[2]<<") maps to (" <<out[0]<<","<<out[1]<<","<<out[2]<<")"<<endl; } return 0; }
有一點要說明的是<limits.h>頭文件,建議MSDN一下,你會發現很多有用的常量,拿來就能用。
2010年8月17日
我覺得有必要把它翻譯一下,然后就會發現考得其實是數學,你在google 翻譯上是得不到如下翻譯的: Description 考慮下面的交流電路。我們將假定電路在穩態。因此,節點1的電壓和節點2的電壓分別是v1 = VS coswt 和 v2 = VRcos (wt + q),其中Vs是電源電壓,w是頻率(弧度每秒),t是時間。VR是電阻R兩端電壓下降的幅度,q是它的相位。
 你需要寫一個程序,以確定不同的w對應的VR值。您將需要兩個電學定律來解決這個問題。第一 個是是歐姆定律,表述為V2 = iR,其中i是在電路順時針流向的電流大小。 第二個是i = C d/dt (v1-v2),i與電容器兩板上的電壓有關。"d/dt" 意為求關于t的求導。 Input 輸入包括一行或多行。第一行包括三個實數和一個非負整數。實數按順序是VS,R,C。 整數n是測試用例的個數。接下來的n行就是輸入,要求一行一個實數,代表w的值。
Output 輸出n行的VR值,注意,結果精確到小數點后三位。 下面需要推導一下求VR的公式: V2=iR=CR d/dt (VS*cos(wt)-VR*cos(wt+q))=VRcos(wt+q) = CR w (sin(wt+q)-sin(wt))=VRcos(wt+q) 下面用到高中數學當中的計算方法,分別令 t=0 和 wt+q=0 ,得到 CRw tan b = 1 和 VR=CRw VS sin b , 然后利用三角函數中的萬能公式,求得 :VR = CRw VS / sqrt (1+ (CRw) ^ 2 ))
// C 編譯器:
#include <stdio.h>
#include <math.h>
int main()
  {
int i=0,n;
double VR,VS,R,C,w;
scanf("%lf%lf%lf%d",&VS,&R,&C,&n);
for (; i<n; i++)
 {
scanf("%lf",&w);
VR=C*R*w*VS / sqrt(1+C*C*R*R*w*w);
printf("%.3lf\n",VR);
}
return 0;
}
注意 , 用 double
#include <iostream> #include <string> using namespace std;
int main() { int a[200],b[200],c[400]={0},i,j,ls1,ls2; string s; for (cin>>s,ls1=s.length(),i=ls1-1,j=0; i>=0; i--) a[j++]=s[i]-'0'; //將第一個數逆序放入a數組 for (cin>>s,ls2=s.length(),i=ls2-1,j=0; i>=0; i--) b[j++]=s[i]-'0'; //將第二個數逆序放入b數組
for (i=0; i<ls1; i++) for (j=0; j<ls2; j++) { c[i+j] += a[i]*b[j]; if(c[i+j] >= 10) { c[i+j+1] += c[i+j]/10; c[i+j] %= 10; } } i=399; while (i--) if (c[i]) break; //跳過所有前導0 for (; i>=0; i--) printf("%d",c[i]); //輸出主體部分 return 0; }
#include <iostream> #include <string> using namespace std; int main() { int a[201]={0},b[200]={0},i,j,len,ls1,ls2,f=0; // 相加后結果放在a內 string s; for (cin>>s,ls1=s.length(),i=ls1-1,j=0; i>=0; i--) a[j++]=s[i]-'0'; //將第一個數逆序放入a數組 for (cin>>s,ls2=s.length(),i=ls2-1,j=0; i>=0; i--) b[j++]=s[i]-'0'; //將第二個數逆序放入b數組
for (i=0,len=ls1>ls2?ls1:ls2; i<len; i++) // 注意len取二者較大的值 { a[i] += b[i]; //相加結果放入a數組 if (a[i] >= 10) { a[i] %= 10; //進位處理 a[i+1]++; } } if (a[len]) printf("%d",a[len]); //所謂的前導0 for (i=len-1; i>=0; i--) printf("%d",a[i]); //輸出主體部分 return 0; }
- 題目描述
- 某校大門外長度為L的馬路上有一排樹,每兩棵相鄰的樹之間的間隔都是1米。我們可以把馬路看成一個數軸,馬路的一端在數軸0的位置,另一端在L的位置;數軸上的每個整數點,即0,1,2,……,L,都種有一棵樹。
馬路上有一些區域要用來建地鐵,這些區域用它們在數軸上的起始點和終止點表示。已知任一區域的起始點和終止點的坐標都是整數,區域之間可能有重合的部分。現在要把這些區域中的樹(包括區域端點處的兩棵樹)移走。你的任務是計算將這些樹都移走后,馬路上還有多少棵樹。
- 輸入
- 輸入的第一行有兩個整數L(1 <= L <= 10000)和 M(1 <= M <= 100),L代表馬路的長度,M代表區域的數目,L和M之間用一個空格隔開。接下來的M行每行包含兩個不同的整數,用一個空格隔開,表示一個區域的起始點和終止點的坐標。
- 輸出
- 輸出包括一行,這一行只包含一個整數,表示馬路上剩余的樹的數目。
- 樣例輸入
-
500 3
150 300
100 200
470 471
- 樣例輸出
-
298
基本思路:將所有的樹做標記,移走則標記為0,存在標記為1.由于不好判斷給定數目是多少,故用向量。效率雖然不高,但是可以AC。
#include <iostream> #include <vector> using namespace std;
int main() { int L,M,i=0,j,start,end,count=0; scanf("%d%d",&L,&M); vector<int> Mark(L+1,1); for (; i<M; i++) { scanf("%d%d",&start,&end); for (j=start; j<=end; j++) Mark[j]=0; } for (i=0; i<=L; i++) if (Mark[i]==1) count++; printf("%d\n",count); return 0; }
Description
輸入一個2進制的數,要求輸出該2進制數的16進制表示。 在16進制的表示中,A-F表示10-15
Input
第1行是測試數據的組數n,后面跟著n行輸入。每組測試數據占1行,包括一個以0和1組成的字符串,字符串長度至少是1,至多是10000
Output
n行,每行輸出對應一個輸入。
Sample Input
2
100000
111
Sample Output
20
7
09小孩問我的一道題,原來寫的代碼足足有90多行,今天重寫:
#include <iostream> #include <string> using namespace std;
int main() { int n,pos,sec,i,j,w[4] = {1,2,4,8}; //sec是分段處理,pos是對應權值位置 char x[17] = "0123456789ABCDEF"; //打表 string bin; //輸入的二進制字符串 cin>>n; while (n--) { cin>>bin; sec=bin.length()%4; pos=0; for (i=sec; i>0; i--) if (bin[sec-i]=='1') pos += w[i-1]; if (sec) printf("%c",x[pos]); for (i=sec; i<bin.length(); i+=4) { pos=0; for (j=0; j<4; j++) if (bin[i+j]=='1') pos += w[3-j]; printf("%c",x[pos]); } printf("\n"); } return 0; }
Description
你的一個朋友買了一臺電腦。他以前只用過計算器,因為電腦的顯示器上顯示的數字的樣子和計算器是不一樣,所以當他使用電腦的時候會比較郁悶。為了幫助他,你決定寫一個程序把在電腦上的數字顯示得像計算器上一樣。
Input
輸入包括若干行,每行表示一個要顯示的數。每行有兩個整數s和n (1 <= s <= 10, 0 <= n <= 99999999),這里n是要顯示的數,s是要顯示的數的尺寸。
如果某行輸入包括兩個0,表示輸入結束。這行不需要處理。
Output
顯示的方式是:用s個'-'表示一個水平線段,用s個'|'表示一個垂直線段。這種情況下,每一個數字需要占用s+2列和2s+3行。另外,在兩個數字之間要輸出一個空白的列。在輸出完每一個數之后,輸出一個空白的行。注意:輸出中空白的地方都要用空格來填充。
Sample Input
2 12345
3 67890
0 0
Sample Output
-- -- --
| | | | | |
| | | | | |
-- -- -- --
| | | | |
| | | | |
-- -- --
--- --- --- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- --- ---
Hint
數字(digit)指的是0,或者1,或者2……或者9。 數(number)由一個或者多個數字組成。
這題橫跨我一個學期之久,后來無奈還是百度了一下:
#include <iostream> using namespace std;
char n1[] = "- -- -----"; char n2[] = "| ||| ||"; char n3[] = "||||| |||"; char n4[] = " ----- --"; char n5[] = "| | | | "; char n6[] = "|| |||||||"; char n7[] = "- -- -- --";
int main() { int m; char n[10]; cin>>m>>n; while ((n[0]-'0')||m) { int len = strlen(n); for (int i=0;i<len;i++) { cout<<" "; int num = n[i]-'0'; for (int j=0;j<m;j++) { cout<<n1[num]; } cout<<" "; } cout<<endl; int temp = m; while (temp--) { for (int i=0;i<len;i++) { int num = n[i]-'0'; cout<<n2[num]; for (int j=0;j<m;j++) { cout<<" "; } cout<<n3[num]; cout<<" "; } cout<<endl; } for (int i=0;i<len;i++) { cout<<" "; int num = n[i]-'0'; for (int j=0;j<m;j++) { cout<<n4[num]; } cout<<" "; } cout<<endl; temp = m; while (temp--) { for (int i=0;i<len;i++) { int num = n[i]-'0'; cout<<n5[num]; for (int j=0;j<m;j++) { cout<<" "; } cout<<n6[num]; cout<<" "; } cout<<endl; } for (int i=0;i<len;i++) { cout<<" "; int num = n[i]-'0'; for (int j=0;j<m;j++) { cout<<n7[num]; } cout<<" "; } cout<<endl<<endl;
cin>>m>>n; } return 0; }
我想,第一次做出來這題的人真是了不起!
|