bool isPowerOfTwo(int n)
{unsigned int k = 1;if(n<=0)return false;if(n==1)return true;for(int i = 1;i<32;i++){k*=2;if(k==n)return true;}return false;
}
bool isPowerOfThree(int n){unsigned int k = 1;if(n<=0)return false;if(n==1)return true;for(int i = 0;i<=20;i++)//3的20次幂已经是32位整型的上限了。{k*=3;if(k==n)return true;}return false;}
int kthFactor(int n, int k)
{int count = 0;for(int i = 1;i<=n;i++){if(n%i==0)count++;if(count==k)return i;}return -1;
}
防止溢出,要用long long型
死循环枚举所有情况
bool isPerfectSquare(int num)
{int i = 0;long long sum = 0;while(1){i++;sum=(long long)i*i;if(sum==num)return true;if(sum>=num)return false;}
}
int search(int* nums, int numsSize, int target)
{int i;for(i =0; i < numsSize; i++ ){if(nums[i]== target){return i;}}return -1;
}
生成元:X+X的每一位数字之和等于Y,则X是Y的生成元。
例如:
输入216则他的最小生成元是198
因为198 + 1 +9 +8 = 216
#include
int main()
{int x, y, z, gen = 0;scanf("%d", &y);for (int i = 1; i < y; i++){x = i; z = i;while (i > 0){x = x + i % 10;i = i / 10;}i = z;if (x == y){gen = 1;printf("%d", i);break;}}if (gen != 1){printf("0");}return 0;
}
#include
int main()
{char c;char s[1000];double arr[200];arr['C'] = 12.01;arr['O'] = 16.00;arr['H'] = 1.008;arr['N'] = 14.01;//输入字符串scanf("%s", s);int i = 0;double w = 0;//循环遍历s字符串while (s[i] != '\0') {int sum = 0;c = s[i];i++;//判断下一位是否为数字while (s[i] >= '0' && s[i] <= '9') {//如果数字 求出数字大小sum = sum * 10 + s[i] - '0';//进行自加 便以判断下一位i++;}//如果有数字进行以下操作if (sum)w = w+ sum * arr[c];else w = w + arr[c];}printf("%.3f g/mol\n", w);return 0;
}
‘\n’表示忽略所有的空白字符,输入非空白字符结束输入 ,不加’\n’会使c接收换行符
#include
int main()
{int n,i;char c;double h,s;scanf("%d\n", &n); for(i=0;iscanf("\n%c%lf",&c,&h);s=0;if(c=='M'){s=h/1.09;}else{s=h*1.09;}printf("%.2lf\n",s);}return 0;
}
#include
int main()
{int a,b,i,sum=1;scanf("%d%d",&a,&b);for(i=1;i<=a+b;i++)sum*=i;printf("%d\n",sum);
}