1、幾種輸入輸出方式:
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <iostream>
# include <cstdio>
# include <string>
# define MAXN 100010
using namespace std;
char s[MAXN];
int main(){
/* 1
while(fgets(s,MAXN,stdin) != NULL)
puts(s);
*/
/* 2
int ch;
while((ch = getchar()) != EOF)
putchar(ch);
*/
string s;
while(cin >> s)
cout << s << "\n";
return 0;
}
2、注意點
1、q = !q //常用作標記變量。
for循環中要想着剪紙,少幾次循環也是優化。
題目描述中明顯帶有循環和比較性質的題目,想着用取餘操作。
2、判斷兩數相加最多進了多少位:
for(int i=9;i>=0;i++){//int的上限可以保存9位整數
c = (a%10+b%10+c) > 9 ? 1 : 0;
ans+=c;
a/=10;
b/=10;
}
3、 因子和階乘:
n!=1*2*3*4*5*6*7*...*n
將n!分解成素因子相乘的形式,
素數表:2 3 5 7 11 13 17 ...
5! = 3 1 1 (指的是3個2,1個3,1個5)
53! = 49 23 12 8 4 4 3 2 2 1 1 1 1 1 1 1
怎麼來做?
首先要構造一個素數表,prime[]={2,3,5,7...}
然後將1-n中的每個數反覆除以prime[j],並記錄p[j]出現的次數,即指數。
3、階乘的精確計算
# include <stdio.h>
# include <string.h>
# define MAXN 3000
int f[MAXN];
int main(){
int n,j;
scanf("%d",&n);
memset(f,0,sizeof(f));
f[0]=1;
for(int i=2;i<=n;i++){
int c=0;
for(j=0;j<MAXN;j++){
int s=f[j] * i + c;
f[j] = s%10;
c=s/10;
}
}
for(j=MAXN-1;j>=0;j--)
if(f[j]) break;
for(int i=j;i>=0;i--){
printf("%d",f[i]);
}
printf("\n");
return 0;
}