PTA——洗牌
程序:
#include<stdio.h>
int main() {
int i,n,*result;
scanf("%d",&n);
int shuffle[],card1[],card2[];
result = n%==?card1:card2;
for(i=; i<; i++) {
scanf("%d",&shuffle[i]);
}
//初始化
for(i=; i<; i++) {
card1[i] = i;
}
//洗牌
/*
while(n) {
if(n%2==1) {
for(i=0; i<54; i++) {
card2[shuffle[i]-1] = card1[i];
}
} else {
for(i=0; i<54; i++) {
card1[shuffle[i]-1] = card2[i];
}
}
n--;
}
*/
while(n){
for(i=; i<; i++){
card2[shuffle[i]-] = card1[i];
}
n--;
if(n==){
break;
}
for(i=; i<; i++){
card1[shuffle[i]-] = card2[i];
}
n--;
} for(i=; i<; i++) {
if(result[i]/==) {
printf("%c%d",'S',result[i]%+);
} else if(result[i]/==) {
printf("%c%d",'H',result[i]%+);
} else if(result[i]/==) {
printf("%c%d",'C',result[i]%+);
} else if(result[i]/==) {
printf("%c%d",'D',result[i]%+);
} else {
printf("%c%d",'J',result[i]%+);
}
if(i<) {
printf(" ");
}
}
}
分析:
此题用到的知识点有
1、数组:不定义字符数组,而是在输出时做判断,提高了程序效率
2、指针:最终输出的结果与洗牌次数有关,先定义一个指针,后期直接访问结果数组即可,避免了判断操作
3、三元运算符:节省代码量
4、循环语句:两个数组交替存储每次洗牌结果(注释中的程序是我一开始写的,有逻辑错误),不论最后洗多少次,都是1-2-1-2交替
PTA——洗牌的更多相关文章
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 洗牌算法Fisher_Yates原理
1.算法 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle 简单的原理如下图所示: 2.原理 总结下,洗牌算法Fisher_Yates ...
- C# 洗牌算法
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 C#洗牌算法如下: class Program { ...
- [转]完美洗牌(Perfect Shuffle)问题
[转]原博文地址:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/02.09.md ...
- PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌
-自测1. 打印沙漏() 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号 ...
- Js实现简单的洗牌
基础篇 洗牌采用的是,每一张牌,与后面随机一张牌来交换位置. 扑克牌采用编码制(如,0代表红桃A,依次类推)为了编码方便,扑克牌不含大小王,故52张. 一.扑克牌的了解 扑克(英文:Poker) 一副 ...
- Java集合List模拟“洗牌”操作
Collection工具类为操作List集合提供了几个有用的方法: reverse().shuffle().sort().swap().rotate(). 小例子: 使用shuffle(),方法模拟洗 ...
- Baraja演示15种不同的洗牌特效
实例演示 下载地址 实例代码 实例演示 实例代码 <div class="container"> <header class="clearfix&q ...
- [CareerCup] 18.2 Shuffle Cards 洗牌
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of ...
随机推荐
- eclipse的springboot插件
eclipse的springboot插件官网下载地址还喜欢捉迷藏,正确的下载路径修改方法: 点击zip,然后复制出官网路径如下 http://download.springsource.com/rel ...
- 再谈git和github-深入理解-2
github中的 sloc是什么意思? sloc: source lines of code: 代码行数, 源代码行. 要向仓库中 create file/upload file/edit file等 ...
- Redis setnx命令 分布式缓存
setnx命令 将 key 的值设为 value,当且仅当 key 不存在. 若给定的 key 已经存在,则 SETNX 不做任何动作. SETNX 是SET if Not eXists的简写. re ...
- (转) Ensemble Methods for Deep Learning Neural Networks to Reduce Variance and Improve Performance
Ensemble Methods for Deep Learning Neural Networks to Reduce Variance and Improve Performance 2018-1 ...
- Navicat Premium 12激活
大自然的搬运工:https://www.jianshu.com/p/5f693b4c9468 声明:本文所提供的所有软件均来自于互联网,个人存放在此作为备用,以备将来不时之需,同时作为大家的分享和学习 ...
- Spring NoSuchBeanDefinitionException六大原因总结
1. Overview In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBea ...
- MyEclipse运行Java出错:could not find the main class:test.program will exit(导入项目)
自己新建的项目运行没有任何问题.但是我导入的很早以前别人写的项目,然后run就会弹框could not find the main class:test.program will exit 请确认JA ...
- Apache Solr入门教程(初学者之旅)
Apache Solr入门教程(初学者之旅) 写在前面:本文涉及solr入门的各方面,建议边思考边实践,相信能帮助你对solr有个清晰全面的了解并能简单实用. 在Apache Solr初学者教程的这个 ...
- leecode第一百四十八题(排序链表)
class Solution { public: void sort_list(ListNode* head1, ListNode* head2,int len)//在原链表上进行排序 { ListN ...
- java8中的时间处理
java8中关于时间的处理整理 package com.xb.utils; import java.time.*; import java.time.format.DateTimeFormatter; ...