莫斯电码:
示例输入输出:(有关莫斯电码部分请自行百度)
1 #include2 using namespace std; 3 int main() 4 { 5 string s[]= { "*-","-***","-*-*","-**","*","**-*","--*","****","**","*---","-*-","*-**","--","-*","---","*--*","--*-","*-*","***","-","**-","***-","*--","-**-","-*--","--**"}; 6 string s1; 7 while(cin>>s1) 8 { 9 for(int i=0; i >a;)13 {14 for(int i=0; i<26; i++)15 {16 if(a==s[i])printf("%c",i+97);17 }18 }19 cout<
文本加密:
按照下述规则将给定的字符c转化(加密)为新的字符:"A"转化"B","B"转化为"C",... ..."Z"转化为"a","a"转化为"b",... ..., "z"转化为"A",其它字符不加密。编写程序,加密给定字符串。
1 #include2 using namespace std; 3 int main() 4 { 5 string s; 6 while(cin>>s) 7 { 8 for(int i=0; i ='a'&&s[i]<'z')||(s[i]>='A'&&s[i]<'Z'))11 {12 s[i]+=1;13 }14 if(s[i]=='z')s[i]='A';15 if(s[i]=='Z')s[i]='a';16 }17 cout< <
判断九宫格(最简单的幻方):只进行了行和列的判断未判断对角
1 #include2 using namespace std; 3 int iar[3][3]; 4 int check() 5 { 6 int flag=iar[0][0]+iar[0][1]+iar[0][2]; 7 8 for(int i=0; i<3; i++) 9 {10 int cell=0;11 int row=0;12 for(int j=0; j<3; j++)13 {14 cell+=iar[j][i];15 row+=iar[i][j];16 }17 if(flag!=row||flag!=cell)return 0;18 }19 return 1;20 }21 int main()22 {23 for(int i=0; i<3; i++)24 {25 for(int j=0; j<3; j++)26 {27 scanf("%d",&iar[i][j]);28 }29 }30 if(check())31 {32 printf("1\n");33 }34 else35 {36 printf("0\n");37 }38 39 return 0;40 }
因式分解:
用户输入合数,程序输出若个素数的乘积。例如,输入6,输出2*3。输入20,输出2*2*5。
1 #include2 using namespace std; 3 int isPrime(int n) 4 { 5 for(int i=2; i<=sqrt(n); i++) 6 { 7 if(n%i==0)return 0; 8 } 9 return 1;10 }11 int prime[10000]= { 2};12 void init()13 {14 for(int i=3,k=1; i<=10000; i+=2)15 {16 if(isPrime(i))prime[k++]=i;17 }18 }19 int main()20 {21 int n;22 init();23 while(scanf("%d",&n)!=EOF)24 {25 if(isPrime(n))printf("%d\n",n);26 else27 {28 int flag=1,point=0;29 while(flag)30 {31 while((n%prime[point])==0)32 {33 n/=prime[point];34 if(n!=1)35 printf("%d*",prime[point]);36 else printf("%d\n",prime[point]),flag=0;37 }38 point++;39 }40 }41 }42 return 0;43 }