1. 程式人生 > >題解 【Cpdeforces755A】 PolandBall and Hypothesis

題解 【Cpdeforces755A】 PolandBall and Hypothesis

ace urn () def names name lan std char

我們可以發現,當n>2時,n·(n-2)+1=(n-1)·(n-1),因此,輸出n-2即可。

如果n<=2,我們可以發現:

  當n=2時,2·4+1=9不是質數,輸出4即可;

  當n=1時,1·3+1=4不是質數,輸出3即可。

至此,此題就被我們解決了!

AC代碼:

 1 #include <bits/stdc++.h>//萬能頭文件
 2 
 3 using namespace std;//使用標準名字空間
 4 
 5 inline int read() { //快速讀入
 6     int
f=1,x=0; 7 char c=getchar(); 8 9 while(c<0 || c>9) { 10 if(c==-)f=-1; 11 c=getchar(); 12 } 13 14 while(c>=0 && c<=9) { 15 x=x*10+c-0; 16 c=getchar(); 17 } 18 19 return f*x; 20 } 21 22 int n,m; 23
24 int main() { 25 n=read();//輸入n 26 27 if(n>2) { //如果n>2 28 printf("%d",n-2);//就輸出n-2 29 30 return 0; 31 } 32 33 if(n==2) { //如果n=2 34 printf("4");//就輸出4 35 36 return 0; 37 } 38 39 if(n==1) { //如果n=1 40 printf("3");//就輸出3 41
42 return 0; 43 } 44 45 return 0;//結束 46 }

題解 【Cpdeforces755A】 PolandBall and Hypothesis