1027: Smith Numbers

 


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout30s8192K2312565Standard

While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University, noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith’s telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:

The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42?, and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7= 42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.

As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.

Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!

 

Input

The input consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.

 

 

Output

For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n, and print it on a line by itself. You can assume that such a number exists.

 

Sample Input

4937774
0

Sample Output

4937775
这道题写的太差了,看来真的不在状态。
整个过程没有考虑清楚,首先这个SMITH数必须是合数,题意没读清楚,一位只要是质数都是。
第二,这个过程的N1,N2,SUM,SUM2,T的初始化没弄好
第三,如果T是大于等于10的,那么就需要把它的各位数字相加,这也没弄清楚
#include<iostream>
int is_prime(int n)
{
 for(int i=2;i*i<=n;i++)
     if(n%i==0) return i;
 return 1;
}
int main()
{
 int n,n2;
 freopen("in.txt","r",stdin);
 freopen("out.txt","w",stdout);
 while((scanf("%d",&n),n)!=0)
 {
  int sum=0,sum2=0;
  n2=n;
  while(1)
  {
   n=n2;
   n++;
   n2=n;
   sum=0;
   sum2=0;
   int t=n;
      while(t>0)
      {
         sum+=t%10;
         t=t/10;
      }
   t=is_prime(n);
   if(t==1) continue;
   while(t>1)
   {
      int t2=t;
      if(t2>=10)
      {
     while(t2>0)
              {
               sum2+=t2%10;
               t2=t2/10;
              }
         }
         else
           sum2+=t2;
      n=n/t;
      t=is_prime(n);
   }
   while(n>0)
      {
      sum2+=n%10;
      n=n/10;
      }
      if(sum2==sum) break;
   } 
   printf("%d/n",n2);
 }
 return 0;
}
<think>我们正在使用natbib宏包,并且希望参考文献按照引用顺序(即引用出现的顺序)排序,而不是按作者字母顺序排序。 根据引用[1]和引用[2]中的信息,我们知道在LaTeX中,参考文献样式(.bst文件)决定了排序方式。其中,`unsrt`样式就是按照引用顺序排序的,而`plain`样式则是按字母顺序排序。 但是,用户使用的是Elsevier模板,而Elsevier模板通常使用`elsarticle`文档类,并且默认使用`natbib`宏包。在Elsevier模板中,默认的参考文献样式可能是按字母顺序排序的(类似于`plain`样式)。 因此,我们需要做的是: 1. 在文档类选项中指定参考文献样式为`unsrt`(按引用顺序排序)或者使用`natbib`包选项来设置。 2. 或者,在加载natbib宏包时(如果模板允许)设置相应的选项。 然而,Elsevier模板(elsarticle)通常使用自己的参考文献样式文件(如`elsarticle-num.bst`用于数字引用并按引用顺序排序,而`elsarticle-harv.bst`用于作者年份格式并按字母排序)。因此,我们需要选择正确的参考文献样式。 具体步骤: - 在文档类选项中指定`num`格式(数字引用)即可,因为`num`格式默认按引用顺序排序。 - 或者,在编译时选择正确的.bst文件。 在elsarticle文档中,通常使用以下方式指定文档类选项: \documentclass[preprint,12pt]{elsarticle} % 默认可能是作者年份格式(按字母排序) 为了得到按引用顺序排序的数字引用,我们应该使用: \documentclass[preprint,12pt, sort&compress]{elsarticle} % 注意:这里sort&compress选项可能会影响排序 但是,根据引用[2]中的说明,`unsrt`样式是按照引用顺序排序的。而在natbib中,我们可以通过设置`biboptions`来调整。 实际上,在elsarticle文档类中,有一个`biboptions`命令可以设置natbib的选项。我们可以这样设置: \biboptions{numbers, sort&compress} % 这里sort选项会导致按字母排序,而我们需要按引用顺序,所以应该禁用sort 根据natbib文档,如果我们使用`numbers`选项,并且不启用`sort`,那么参考文献将按照引用顺序排序。因此,我们应该设置: \biboptions{numbers, compress} % 注意:这里没有sort,所以按引用顺序,并且启用压缩(如[1-3]) 因此,解决方案如下: 步骤: 1. 在导言区(\documentclass之后)添加: \biboptions{numbers,compress} % 按引用顺序,数字引用,并压缩连续引用 2. 如果之前有设置其他的biboptions,请替换掉。 3. 确保使用正确的.bst文件。在elsarticle模板中,当我们使用数字引用时,应该使用`elsarticle-num.bst`。但通常模板会自动根据选项选择,所以设置biboptions后,编译时会自动处理。 4. 编译流程:运行LaTeX -> BibTeX -> LaTeX -> LaTeX 注意:如果之前编译过,可能需要删除辅助文件(如.aux, .bbl等)再重新编译,以确保正确。 示例代码: \documentclass[preprint,12pt]{elsarticle} \biboptions{numbers,compress} % 关键设置:数字引用,按引用顺序,压缩连续引用 \begin{document} Here is a citation: \cite{AuthorA}. Then another: \cite{AuthorB}. \bibliographystyle{elsarticle-num} % 明确使用elsarticle-num样式,确保按引用顺序 \bibliography{mybib} \end{document} 但是,在elsarticle文档中,通常不需要显式指定\bibliographystyle,因为文档类会根据选项自动选择。但为了保险,也可以指定为`elsarticle-num`。 另外,根据引用[1]中的说明,我们也可以直接使用`unsrt`样式(但elsarticle模板有自己特定的样式,所以建议使用模板自带的`elsarticle-num`)。 总结:在Elsevier模板中,通过设置`\biboptions{numbers,compress}`并配合使用`elsarticle-num`参考文献样式,即可实现按引用顺序排序。 如果问题仍未解决,请检查是否加载了其他可能影响参考文献的宏包。 相关问题: 1. 如何在LaTeX中生成作者-年份格式的参考文献? 2. 如何压缩连续引用(例如[1-3])? 3. 如何修改参考文献条目的格式(如字体、缩进等)? 4. 如何解决参考文献编译错误(如缺少.bbl文件)?</think>在LaTeX中使用`natbib`宏包实现参考文献按引用顺序(出现顺序)排序,而非作者字母顺序排序,需按以下步骤操作: ### 解决方案 1. **加载`natbib`宏包时指定选项** 在导言区添加: ```latex \usepackage[numbers, sort&compress]{natbib} ``` - `numbers`:启用数字引用格式(如[1], [2]) - `sort`:**禁用**字母排序(省略此选项即按引用顺序) - `compress`:启用引用压缩(如[5-7]) 2. **选择`unsrt`参考文献样式** 在`\bibliographystyle`中指定: ```latex \bibliographystyle{unsrt} % 按引用顺序排序的样式 ``` 3. **完整配置示例** ```latex \documentclass{article} \usepackage[numbers, sort&compress]{natbib} % 关键配置 \begin{document} \cite{paper1} % 先引用文献A \cite{paper2} % 后引用文献B \bibliographystyle{unsrt} % 按引用顺序的样式 \bibliography{refs} % 引用BibTeX数据库 \end{document} ``` ### 关键说明 1. **排序逻辑**: - `unsrt`样式会严格按文本中**首次引用出现的顺序**生成参考文献列表[^1][^2] - 与作者姓名、出版年份无关 2. **Elsevier模板特殊处理**: 若使用Elsevier模板(如`elsarticle`),需添加: ```latex \biboptions{numbers,compress} % 覆盖默认排序设置 ``` 3. **编译流程**: ```bash pdflatex main.tex # 生成.aux引用记录 bibtex main.aux # 按引用顺序生成.bbl pdflatex main.tex # 插入参考文献 pdflatex main.tex # 最终格式化 ``` ### 效果验证 - **输入**:先引用`AuthorB (2023)`,后引用`AuthorA (2020)` - **输出**: ```latex 正文引用: [1], [2] 参考文献列表: 1. AuthorB. TitleB. 2023. 2. AuthorA. TitleA. 2020. % 按引用顺序而非字母顺序 ``` ### 常见问题排查 - **仍按字母排序**:检查是否误用`plain`或`alpha`样式而非`unsrt` - **压缩失效**:确保`compress`选项已启用,连续引用需用逗号分隔:`\cite{key1,key2,key3}` - **编译错误**:删除所有`.aux`, `.bbl`, `.blg`文件后重新编译 --- ### 相关问题 1. 如何在`natbib`中实现作者-年份引用格式(如[Smith, 2020])? 2. 参考文献条目格式如何自定义(如字体、缩进)? 3. 如何解决BibTeX编译时的"undefined citations"错误? 4. 多文献引用压缩(如[5-8])失效时如何修复? [^1]: 【LaTeX】参考文献的使用 [^2]: LaTeX参考文献分类及设置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值