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