博文
字符串排序(2007-10-14 13:02:00)
摘要:输入一组字符串,请你将它们按字典序输出。
此题由多组输入,每组输入的格式如下: n string1 string2 . . . stringn 其中0<=n<=100,任何一个字符串的长度不会超过100,字符串中只含有小写英文字母。当n=0时,程序即可结束。
#include "stdafx.h"#include <iostream>using namespace std;int main(){int n;char aa[101][101];char pp[101];cin>>n;while(n!=0){for(int i1=0;i1<n;i1++)cin>>aa[i1];
for(int i2=0;i2<n;i2++){if(strcmp(aa[i2], aa[i2+1]) >= 0){strcpy(pp,aa[i2]);strcpy(aa[i2],aa[i2+1]);strcpy(aa[i2+1],pp);}}cout<<n<<endl;for(int i3=0;i3<n;i3++)cout<<aa[i3]<<endl;cin>>n;}return 0;}
运行结果正确,但是也是超时!还没想到更好的办法提高效率!......
水仙花数(2007-10-14 12:59:00)
摘要:所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=13+53+33。本题要求打印出所有的水仙花数。#include "iostream.h"
int main()
{int i,n,j,k;
for(n=100;n<1000;n++)
{i=n/100;
j=n/10-i*10;
k=n%10;
if(n==i*i*i+j*j*j+k*k*k)
cout<<n<<" " ;
}
cout<<endl;
return 0;
}......
Sorting by Swapping(2007-10-14 12:58:00)
摘要:题目:Given a permutation of numbers from 1 to n, we can always get the sequence 1, 2, 3, ..., n by swapping pairs of numbers. For example, if the initial sequence is 2, 3, 5, 4, 1, we can sort them in the following way: 2 3 5 4 1 1 3 5 4 2 1 3 2 4 5 1 2 3 4 5 Here three swaps have been used. The problem is, given a specific permutation, how many swaps we needs to take at least. 我写的程序:
#include "stdafx.h"#include <iostream>using namespace std;int main(){int n,i=1,j,m,count,temp;int s[10001];cin>>n;for(;n!=0;n--){ cin>>m; count=0; for(j=1;j<=m;j++) cin>>s[j]; for(i=1;i<=m;i++) while(s[i]!=i) {j=s[i];temp=s[i];s[i]=s[j];s[j]=temp;count++;} cout<<count<<endl;}return 0;}
运行结果虽然正确,但是老超时,效率不高!在网上看到别人的程序,效率要提高很多:
#include<stdio.h>int main(){ int n,m,i,j,k,t; short p[10......
N!末尾一共有多少个0(2007-10-14 12:54:00)
摘要:#include "stdafx.h"#include <iostream>#include <math.h>using namespace std;int main(){int n,i;cin>>n;while(n){i=n/5+n/25+n/125+n/625;cout<<i<<endl;cin>>n;}return 0;}......
