一些精简的小技巧

时间:2022-07-01 16:04:23

   一.标志变量的设置   

样例输入

"To be or not to be," quoth the Bard,"that is the question".

样例输出

``To be or not to be,"quoth the Bard,``that is the question".
 
int  main()
{
	int c, q = 1;
	while((c = getchar()) != EOF)
	{
		if( c == '"')
		{
			printf("%s",q ? "``" : "''");
			q = !q;
		}
		else
			printf("%c",c);

	}
	return 0;
}

  

   二.周期串   

for( int i = 1; i <= len; i++)
if( len % i ==0)
{
int ok = 1;
for(int j = i; j < len; j++)
  if( word[i] != word[ j % i ])
     {ok = 0; break;}
  if( ok == 1)
    printf("%s",word);
}
  

   三.计算有多少次进位   

int c = 0, ans = 0;
for(int i = 9; i >= 0; i--)
{
    c = ( a%10 + b %10 + c) > 9 ? 1 : 0;
    ans += c;
    a = a / 10;
    b = b / 10;
}

 

四.阶乘的精确值
#include"stdio.h"
#include"string.h"
const int maxn = 3000;
int f[maxn];
int main()
{
	int i, j, n;
	scanf("%d", &n);
	memset(f, 0, sizeof(f));
	f[0] = 1;
	for( 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(i = j; i >= 0; i--)
		printf("%d",f[i]);
	printf("\n");
	return 0 ;
}