NYOJ位数n-1问题

来源:互联网 发布:电大好还是网络教育好 编辑:程序博客网 时间:2024/05/10 18:19

新看了一种计算位数的好方法,利用sprintf,其实和printf差不多,区别在于,printf直接输出,而sprintf是包含三个内容(数组名,“%d/c”,所输入到数组中的数)。

举例,计算123的位数:

sprintf(str,"%d",123);

<span style="font-size:18px;">#include<stdio.h>#include<string.h>int main(){int t;scanf("%d",&t);while(t--){int n,i,len;scanf("%d",&n);     char str[100]={0};        sprintf(str,"%d",n);    len=strlen(str);switch(len){case 2:n=n%10;break;case 3:n=n%100;break;case 4:n=n%1000;break;case 5:n=n%10000;break;case 6:n=n%100000;break;case 7:n=n%1000000;break;}printf("%d\n",n);}return 0;}</span>
最优解:
<span style="font-size:18px;">#include<cstdio>int main(){int n,m;scanf("%d",&n);while(n--){scanf("\n%*c%d",&m);printf("%d\n",m);}}</span>


0 0
原创粉丝点击